style(uvc osx): code style improve.

This commit is contained in:
Tiny 2018-12-19 12:41:02 +08:00
parent 6b93ceecee
commit 50571a4a40

View File

@ -50,67 +50,76 @@ struct buffer {
}; };
void printConfig(std::vector<CameraConfig> cfg_list) { void printConfig(std::vector<CameraConfig> cfg_list) {
if(cfg_list.size()==0) return; if (cfg_list.size() == 0) return;
int device = -1; int device = -1;
int width = -1; int width = -1;
int height = -1; int height = -1;
float fps = -1; float fps = -1;
int format = -1; int format = -1;
int frame_mode = -1; int frame_mode = -1;
for (int i=0;i<(int)cfg_list.size();i++) { for (unsigned int i=0; i < cfg_list.size(); i++) {
if (cfg_list[i].device != device) { if (cfg_list[i].device != device) {
if (device>=0) printf("\b fps\n"); if (device >= 0) printf("\b fps\n");
device = cfg_list[i].device; device = cfg_list[i].device;
if(strncmp(cfg_list[i].name, MYNT_DEV_NAME, strlen(MYNT_DEV_NAME)) == 0) { if (strncmp(cfg_list[i].name,
printf("*"); MYNT_DEV_NAME, strlen(MYNT_DEV_NAME)) == 0) {
} else { printf("*");
printf(" "); } else {
} printf(" ");
}
printf(" %d: %s\n", cfg_list[i].device, cfg_list[i].name);
format = -1;
}
printf(" %d: %s\n",cfg_list[i].device,cfg_list[i].name); if ((cfg_list[i].cam_format != format) ||
format = -1; (cfg_list[i].frame_mode != frame_mode)) {
} if (format >= 0) printf("\b fps\n");
format = cfg_list[i].cam_format;
if (cfg_list[i].frame_mode < 0) {
printf(" format: %s", fstr[cfg_list[i].cam_format]);
} else {
printf(" format7_%d: %s", cfg_list[i].frame_mode,
fstr[cfg_list[i].cam_format]);
}
// if(cfg_list[i].compress) printf(" (default)");
width = height = fps = -1;
printf("\n");
}
if ((cfg_list[i].cam_format != format) || (cfg_list[i].frame_mode != frame_mode)) { if ((cfg_list[i].cam_width != width) ||
if (format>=0) printf("\b fps\n"); (cfg_list[i].cam_height != height)) {
format = cfg_list[i].cam_format; if (width > 0) printf("\b fps\n");
if(cfg_list[i].frame_mode<0) printf(" format: %s",fstr[cfg_list[i].cam_format]); printf(" %dx%d ", cfg_list[i].cam_width, cfg_list[i].cam_height);
else printf(" format7_%d: %s",cfg_list[i].frame_mode,fstr[cfg_list[i].cam_format]); width = cfg_list[i].cam_width;
//if(cfg_list[i].compress) printf(" (default)"); height = cfg_list[i].cam_height;
width = height = fps = -1; fps = INT_MAX;
printf("\n"); }
}
if ((cfg_list[i].cam_width != width) || (cfg_list[i].cam_height != height)) { if (cfg_list[i].frame_mode >= 0) {
if (width>0) printf("\b fps\n"); printf("max|");
printf(" %dx%d ",cfg_list[i].cam_width,cfg_list[i].cam_height); } else if (cfg_list[i].cam_fps != fps) {
width = cfg_list[i].cam_width; if (static_cast<int>(cfg_list[i].cam_fps) == cfg_list[i].cam_fps) {
height = cfg_list[i].cam_height; printf("%d|", static_cast<int>(cfg_list[i].cam_fps));
fps = INT_MAX; } else {
} printf("%.1f|", cfg_list[i].cam_fps);
}
if (cfg_list[i].frame_mode>=0) printf("max|"); fps = cfg_list[i].cam_fps;
else if (cfg_list[i].cam_fps != fps) { }
if(int(cfg_list[i].cam_fps)==cfg_list[i].cam_fps) }
printf("%d|",int(cfg_list[i].cam_fps)); printf("\b fps\n");
else printf("%.1f|",cfg_list[i].cam_fps);
fps = cfg_list[i].cam_fps;
}
}
printf("\b fps\n");
} }
std::vector<CameraConfig> findDevicesConfig() { std::vector<CameraConfig> findDevicesConfig() {
std::vector<CameraConfig> dev_list; std::vector<CameraConfig> dev_list;
std::vector<CameraConfig> sys_list = AVfoundationCamera::getCameraConfigs(); std::vector<CameraConfig> sys_list = AVfoundationCamera::getCameraConfigs();
dev_list.insert(dev_list.end(), sys_list.begin(), sys_list.end()); dev_list.insert(dev_list.end(), sys_list.begin(), sys_list.end());
return dev_list; return dev_list;
} }
void pv_sleep(int ms=1) { void pv_sleep(int ms = 1) {
usleep(ms*1000); usleep(ms*1000);
} }
/* /*
@ -171,78 +180,60 @@ struct device;
struct device : public AVfoundationCamera{ struct device : public AVfoundationCamera{
const std::shared_ptr<context> parent; const std::shared_ptr<context> parent;
static std::vector <struct device*> s_devices; static std::vector <struct device*> s_devices;
int _vendor_id = -1; int _vendor_id = -1;
int _product_id = -1; int _product_id = -1;
CameraConfig _config; CameraConfig _config;
video_channel_callback callback = nullptr; video_channel_callback callback = nullptr;
bool is_capturing = false; bool is_capturing = false;
std::vector<buffer> buffers; std::vector<buffer> buffers;
std::thread thread; std::thread thread;
volatile bool pause_ = false; volatile bool pause_ = false;
volatile bool stop = false; volatile bool stop = false;
unsigned char *cameraBuffer = NULL; unsigned char *cameraBuffer = NULL;
unsigned char *cameraWriteBuffer = NULL; unsigned char *cameraWriteBuffer = NULL;
std::mutex _devices_mutex; std::mutex _devices_mutex;
device(std::shared_ptr<context> parent, CameraConfig &config) device(std::shared_ptr<context> parent, const CameraConfig &config)
: AVfoundationCamera(&config), parent(parent), _config(config){ : AVfoundationCamera(& const_cast<CameraConfig&>(config)),
VLOG(2) << __func__; parent(parent), _config(config) {
if(strncmp(config.name, MYNT_DEV_NAME, strlen(MYNT_DEV_NAME)) == 0) { VLOG(2) << __func__;
_vendor_id = MYNTEYE_VID;// 0x04B4 if (strncmp(config.name, MYNT_DEV_NAME, strlen(MYNT_DEV_NAME)) == 0) {
_product_id = MYNTEYE_PID;// 0x00F9 _vendor_id = MYNTEYE_VID; // 0x04B4
open(); _product_id = MYNTEYE_PID; // 0x00F9
} open();
}
std::lock_guard<std::mutex> lock(_devices_mutex); std::lock_guard<std::mutex> lock(_devices_mutex);
s_devices.push_back(this); s_devices.push_back(this);
} }
~device() { ~device() {
VLOG(2) << __func__; VLOG(2) << __func__;
std::lock_guard<std::mutex> lock(_devices_mutex); std::lock_guard<std::mutex> lock(_devices_mutex);
for(unsigned long i = 0 ; i < s_devices.size() ; i++) { for (unsigned int i = 0 ; i < s_devices.size() ; i++) {
if(this == s_devices[i]) { if (this == s_devices[i]) {
s_devices.erase(s_devices.begin()+i); s_devices.erase(s_devices.begin()+i);
} }
} }
pause_ = true; pause_ = true;
} }
void open() { void open() {
setup_camera(); setup_camera();
} }
// void set_format(
// int width, int height, int format, int fps,
// video_channel_callback callback) {
// this->width = width;
// this->height = height;
// this->format = format;
// this->fps = fps;
// this->callback = callback;
// }
// static void uvc_frame_callback (struct uvc_frame *frame, void *user_ptr)
// {
// for(unsigned long i = 0 ; i < s_devices.size() ; i++) {
// if(user_ptr == (void*)s_devices[i]) {
// printf("bingo\n");
// }
// }
// }
void start_capture() { void start_capture() {
if (is_capturing) { if (is_capturing) {
LOG(WARNING) << "Start capture failed, is capturing already"; LOG(WARNING) << "Start capture failed, is capturing already";
return; return;
} }
is_capturing = true; is_capturing = true;
startCamera(); startCamera();
} }
void stop_capture() { void stop_capture() {
@ -251,68 +242,66 @@ struct device : public AVfoundationCamera{
is_capturing = false; is_capturing = false;
} }
std::string get_name() const{ std::string get_name() const {
return std::string(_config.name); return std::string(_config.name);
} }
int get_vendor_id() const{ int get_vendor_id() const {
return _vendor_id; return _vendor_id;
} }
int get_product_id() const{ int get_product_id() const {
return _product_id; return _product_id;
} }
std::string get_video_name() const{ std::string get_video_name() const {
return std::string("video name is not supported on osx."); return std::string("video name is not supported on osx.");
} }
void setup_camera() { void setup_camera() {
if(initCamera()) { if (initCamera()) {
printInfo(); printInfo();
} else { } else {
printf("could not initialize selected camera\n"); LOG(ERROR) << "could not initialize selected camera";
closeCamera(); closeCamera();
return; return;
} }
} }
void poll() { void poll() {
if(is_capturing) { if (is_capturing) {
//long start_time = VisionEngine::currentMicroSeconds(); // long start_time = VisionEngine::currentMicroSeconds();
cameraBuffer = getFrame(); cameraBuffer = getFrame();
if (cameraBuffer!=NULL) { if (cameraBuffer != NULL) {
// cameraWriteBuffer = engine->ringBuffer->getNextBufferToWrite(); // cameraWriteBuffer = engine->ringBuffer->getNextBufferToWrite();
// if (cameraWriteBuffer!=NULL) { // if (cameraWriteBuffer!=NULL) {
// memcpy(cameraWriteBuffer,cameraBuffer,engine->ringBuffer->size()); // memcpy(cameraWriteBuffer,cameraBuffer,engine->ringBuffer->size());
// engine->framenumber_++; // engine->framenumber_++;
// engine->ringBuffer->writeFinished(); // engine->ringBuffer->writeFinished();
// //long driver_time = VisionEngine::currentMicroSeconds() - start_time; // long driver_time = VisionEngine::currentMicroSeconds() - start_time;
// //std::cout << "camera latency: " << driver_time/1000.0f << "ms" << std::endl; // std::cout << "camera latency: " << driver_time/1000.0f << "ms" << std::endl;
// } // }
if (callback) { if (callback) {
callback(cameraBuffer, [this]() mutable { callback(cameraBuffer, [this]() mutable {
// todo // todo
}); });
} }
pv_sleep(); pv_sleep();
} else { } else {
if ((is_capturing) && (!stillRunning())) { if ((is_capturing) && (!stillRunning())) {
printf("error!\n"); LOG(ERROR) << "error status in camera running.";
} else pv_sleep(); } else { pv_sleep(); }
} }
} else pv_sleep(5); } else { pv_sleep(5); }
} }
void pause(bool pause) { void pause(bool pause) {
pause_ = pause; pause_ = pause;
} }
void start_streaming() { void start_streaming() {
if (!callback) { if (!callback) {
LOG(WARNING) << __func__ << " failed: video_channel_callback is empty"; LOG(WARNING) << __func__ << " failed: video_channel_callback is empty";
return; return;
} }
start_capture(); start_capture();
thread = std::thread([this]() { thread = std::thread([this]() {
while (!stop) while (!stop)
poll(); poll();
@ -334,19 +323,19 @@ std::vector <struct device*> device::s_devices;
// Enumerate devices // Enumerate devices
MYNTEYE_API std::shared_ptr<context> create_context() { MYNTEYE_API std::shared_ptr<context> create_context() {
return std::make_shared<context>(); return std::make_shared<context>();
} }
MYNTEYE_API std::vector<std::shared_ptr<device>> query_devices( MYNTEYE_API std::vector<std::shared_ptr<device>> query_devices(
std::shared_ptr<context> context) { std::shared_ptr<context> context) {
std::vector<std::shared_ptr<device>> devices; std::vector<std::shared_ptr<device>> devices;
auto camerasConfig = findDevicesConfig(); auto camerasConfig = findDevicesConfig();
printConfig(camerasConfig); printConfig(camerasConfig);
for (unsigned int i = 0; i < camerasConfig.size(); i++) { for (unsigned int i = 0; i < camerasConfig.size(); i++) {
auto dev = std::make_shared<device>(context, camerasConfig[i]); auto dev = std::make_shared<device>(context, camerasConfig[i]);
devices.push_back(dev); devices.push_back(dev);
} }
return devices; return devices;
} }
// Static device properties // Static device properties
@ -367,34 +356,46 @@ MYNTEYE_API std::string get_video_name(const device &device) {
MYNTEYE_API bool pu_control_range( MYNTEYE_API bool pu_control_range(
const device &device, Option option, int32_t *min, int32_t *max, const device &device, Option option, int32_t *min, int32_t *max,
int32_t *def) { int32_t *def) {
// todo MYNTEYE_UNUSED(device);
MYNTEYE_UNUSED(option);
MYNTEYE_UNUSED(min);
MYNTEYE_UNUSED(max);
} }
MYNTEYE_API bool pu_control_query( MYNTEYE_API bool pu_control_query(
const device &device, Option option, pu_query query, int32_t *value) { const device &device, Option option, pu_query query, int32_t *value) {
// todo MYNTEYE_UNUSED(device);
MYNTEYE_UNUSED(option);
MYNTEYE_UNUSED(query);
MYNTEYE_UNUSED(value);
} }
// Access XU (Extension Unit) controls // Access XU (Extension Unit) controls
MYNTEYE_API bool xu_control_range( MYNTEYE_API bool xu_control_range(
const device &device, const xu &xu, uint8_t selector, uint8_t id, const device &/*device*/, const xu &/*xu*/,
int32_t *min, int32_t *max, int32_t *def) { uint8_t /*selector*/, uint8_t /*id*/,
int32_t * /*min*/, int32_t * /*max*/, int32_t * /*def*/) {
// not supported on osx // not supported on osx
LOG(WARNING) << __func__ << " failed: this API is not supported on osx";
} }
MYNTEYE_API bool xu_control_query( // XU_QUERY_SET, XU_QUERY_GET MYNTEYE_API bool xu_control_query( // XU_QUERY_SET, XU_QUERY_GET
const device &device, const xu &xu, uint8_t selector, xu_query query, const device &/*device*/, const xu &/*xu*/,
uint16_t size, uint8_t *data) { uint8_t /*selector*/, xu_query /*query*/,
uint16_t /*size*/, uint8_t * /*data*/) {
// not supported on osx // not supported on osx
LOG(WARNING) << __func__ << " failed: this API is not supported on osx";
} }
MYNTEYE_API void set_device_mode( MYNTEYE_API void set_device_mode(
device &device, int width, int height, int fourcc, int fps, // NOLINT device &device, int /*width*/, int /*height*/, int fourcc, int fps, // NOLINT
video_channel_callback callback) { video_channel_callback callback) {
MYNTEYE_UNUSED(fourcc);
MYNTEYE_UNUSED(fps);
device.callback = callback; device.callback = callback;
} }
MYNTEYE_API void start_streaming(device &device, int num_transfer_bufs) { MYNTEYE_API void start_streaming(device &device, int num_transfer_bufs) { // NOLINT
device.start_streaming(); device.start_streaming();
} }
MYNTEYE_API void stop_streaming(device &device) { MYNTEYE_API void stop_streaming(device &device) { // NOLINT
device.stop_streaming(); device.stop_streaming();
} }