Add libuvc on macOS
This commit is contained in:
102
src/uvc/uvc-libuvc.cc
Normal file
102
src/uvc/uvc-libuvc.cc
Normal file
@@ -0,0 +1,102 @@
|
||||
#include "uvc.h" // NOLINT
|
||||
|
||||
#include <glog/logging.h>
|
||||
#include <libuvc/libuvc.h>
|
||||
|
||||
// #define ENABLE_DEBUG_SPAM
|
||||
|
||||
MYNTEYE_BEGIN_NAMESPACE
|
||||
|
||||
namespace uvc {
|
||||
|
||||
static void check(const char *call, uvc_error_t status) {
|
||||
LOG_IF(FATAL, status < 0)
|
||||
<< call << "(...) returned " << uvc_strerror(status);
|
||||
}
|
||||
#define CALL_UVC(name, ...) check(#name, name(__VA_ARGS__))
|
||||
|
||||
struct context {
|
||||
uvc_context_t *ctx;
|
||||
|
||||
context() : ctx() {
|
||||
CALL_UVC(uvc_init, &ctx, nullptr);
|
||||
}
|
||||
|
||||
~context() {
|
||||
if (ctx)
|
||||
uvc_exit(ctx);
|
||||
}
|
||||
};
|
||||
|
||||
struct device {
|
||||
const std::shared_ptr<context> parent;
|
||||
|
||||
uvc_device_t *uvcdevice;
|
||||
uvc_device_handle_t *handle = nullptr;
|
||||
|
||||
int vid, pid;
|
||||
|
||||
// uvc_stream_ctrl_t ctrl;
|
||||
// uint8_t unit;
|
||||
// video_channel_callback callback = nullptr;
|
||||
|
||||
device(std::shared_ptr<context> parent, uvc_device_t *uvcdevice)
|
||||
: parent(parent), uvcdevice(uvcdevice) {
|
||||
open();
|
||||
|
||||
uvc_device_descriptor_t *desc;
|
||||
CALL_UVC(uvc_get_device_descriptor, uvcdevice, &desc);
|
||||
vid = desc->idVendor;
|
||||
pid = desc->idProduct;
|
||||
uvc_free_device_descriptor(desc);
|
||||
}
|
||||
|
||||
~device() {
|
||||
if (handle)
|
||||
uvc_close(handle);
|
||||
if (uvcdevice)
|
||||
uvc_unref_device(uvcdevice);
|
||||
}
|
||||
|
||||
void open() {
|
||||
if (!handle)
|
||||
CALL_UVC(uvc_open, uvcdevice, &handle);
|
||||
}
|
||||
};
|
||||
|
||||
std::shared_ptr<context> create_context() {
|
||||
return std::make_shared<context>();
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<device>> query_devices(
|
||||
std::shared_ptr<context> context) {
|
||||
std::vector<std::shared_ptr<device>> devices;
|
||||
|
||||
uvc_device_t **list;
|
||||
CALL_UVC(uvc_get_device_list, context->ctx, &list);
|
||||
for (auto it = list; *it; ++it) {
|
||||
try {
|
||||
auto dev = std::make_shared<device>(context, *it);
|
||||
devices.push_back(dev);
|
||||
} catch (std::runtime_error &e) {
|
||||
LOG(WARNING) << "usb:" << static_cast<int>(uvc_get_bus_number(*it)) << ':'
|
||||
<< static_cast<int>(uvc_get_device_address(*it)) << ": "
|
||||
<< e.what();
|
||||
}
|
||||
}
|
||||
uvc_free_device_list(list, 1);
|
||||
|
||||
return devices;
|
||||
}
|
||||
|
||||
int get_vendor_id(const device &device) {
|
||||
return device.vid;
|
||||
}
|
||||
|
||||
int get_product_id(const device &device) {
|
||||
return device.pid;
|
||||
}
|
||||
|
||||
} // namespace uvc
|
||||
|
||||
MYNTEYE_END_NAMESPACE
|
||||
34
src/uvc/uvc.h
Normal file
34
src/uvc/uvc.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef MYNTEYE_UVC_H_ // NOLINT
|
||||
#define MYNTEYE_UVC_H_
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "mynteye/mynteye.h"
|
||||
|
||||
#define MYNTEYE_VID 0x04B4
|
||||
#define MYNTEYE_PID 0x00F9
|
||||
|
||||
MYNTEYE_BEGIN_NAMESPACE
|
||||
|
||||
namespace uvc {
|
||||
|
||||
struct context; // Opaque type representing access to the underlying UVC
|
||||
// implementation
|
||||
struct device; // Opaque type representing access to a specific UVC device
|
||||
|
||||
// Enumerate devices
|
||||
std::shared_ptr<context> create_context();
|
||||
std::vector<std::shared_ptr<device>> query_devices(
|
||||
std::shared_ptr<context> context);
|
||||
|
||||
// Static device properties
|
||||
int get_vendor_id(const device &device);
|
||||
int get_product_id(const device &device);
|
||||
|
||||
} // namespace uvc
|
||||
|
||||
MYNTEYE_END_NAMESPACE
|
||||
|
||||
#endif // MYNTEYE_UVC_H_ NOLINT
|
||||
Reference in New Issue
Block a user