虚拟机调用摄像头设备一直 select timeout问题的解决

news2024/11/17 11:47:20

在VMware里面调用v4l2-ctl捕获图像,或者opencv的VideoCapture(0)捕获图像,或者直接调用v4l2的函数,在streamon后,调用select读取数据,均会一直提示select timeout的问题,大概率是由于USB版本的兼容性造成的,做如下设置基本能够解决。

另外,附上一个完整的例子:

/*
 *  V4L2 video capture example
 *
 *
 * This program is a plus version of program provided with the V4L2 API
 * 
 * dotphoenix@qq.com
 * 
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include <getopt.h>             /* getopt_long() */

#include <fcntl.h>              /* low-level i/o */
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <unistd.h>
#include <time.h>
#include <linux/videodev2.h>

//gcc -g -Wall -o capture_v4l2 capture_v4l2.c

logger//Synsynchronization is not considered///

typedef struct {
        FILE * (*fopen)(char const * const filename, char const * const mode);
        int (*fprintf)(FILE *fp, char const * const fmt, ...);
        int (*vfprintf)(FILE *fp, char const * const fmt, va_list argp);
        int (*fclose)(FILE* fp);
} std_file_t;

void std_file_init(std_file_t * const std_file) {
        std_file->fopen = fopen;
        std_file->fprintf = fprintf;
        std_file->vfprintf = vfprintf;
        std_file->fclose = fclose;
}

typedef enum {
        OFF,
        DEBUG,
        INFO,
        WARNING,
        ERROR
} logger_level_t;

typedef struct {
        logger_level_t level;
        int write_to_file ;
        FILE *fp;
        std_file_t *std_file;
} logger_t;

    
static logger_t* logger;

static void logger_format_current_time(char* ts) {
        time_t rawtime;
        struct tm *info;
        time( &rawtime );
        info = localtime( &rawtime );
        strftime(ts, 80, "%Y-%m-%d %H:%M:%S", info);
}

static const char* logger_format_level(int level) {
        switch (level)
        {
        case DEBUG:
                return "[DEBUG]";
        case INFO:
                return "[INFO]";
        case WARNING:
                return "[WARNING]";
        case ERROR:
                return "[ERROR]";                                        
        default:
                return "[UNKNOWN]";
        }
}


void logger_log(logger_level_t const level, char const * const file, size_t const line, char const * const fmt, ...) {

        if (level < logger->level) {
                return;
        }

        char ts[64] = {0};
        logger_format_current_time(ts);
        logger->std_file->fprintf(logger->fp, "%s %s %s: %lu: ", logger_format_level(level), ts, file, line);
        va_list argp;
        va_start(argp, fmt);
        logger->std_file->vfprintf(logger->fp, fmt, argp);
        va_end(argp);    
        logger->std_file->fprintf(logger->fp, "\n");
}

#define log_debug(...) logger_log(DEBUG, __FILE__, __LINE__, __VA_ARGS__)
#define log_info(...)  logger_log(INFO, __FILE__, __LINE__, __VA_ARGS__)
#define log_warn(...)  logger_log(WARNING, __FILE__, __LINE__, __VA_ARGS__)
#define log_error(...) logger_log(ERROR, __FILE__, __LINE__, __VA_ARGS__)


static int logger_init(
    logger_level_t const level, 
    char const * const filename, 
    int const write_to_file
) {
        std_file_t* std_file = (std_file_t*)malloc(sizeof(std_file_t));
        logger = (logger_t*)malloc(sizeof(logger_t));
        std_file_init(std_file);
        logger->level = level;
        logger->write_to_file = write_to_file;
        logger->std_file = std_file;
        if (logger->write_to_file) {
                logger->fp = logger->std_file->fopen(filename, "a");
                if (logger->fp == NULL) {
                        fprintf(stderr, "append log file %s failed, error %d, %s\n", filename, errno, strerror(errno));
                        return -1;
                }
        } else {
                logger->fp = stderr;
        }
        char ts[128] = {0};
        logger_format_current_time(ts);
        strcat(ts, "   start new logger -------------------\n");
        log_warn(ts);
        return 0;
}

void logger_uninit() {
        if(logger == NULL) {
                return;
        }
        if(logger->write_to_file) {
                if(logger->fp != NULL) {
                        logger->std_file->fclose(logger->fp);
                        logger->fp = NULL;
                }
        }
        free(logger->std_file);
        free(logger);
}




//
#define CLEAR(x) memset(&(x), 0, sizeof(x))

enum io_method_t {
        IO_METHOD_READ,
        IO_METHOD_MMAP,
        IO_METHOD_USERPTR,
};

struct buffer_t {
        void   *start;
        size_t  length;
};

static char            *dev_name;
static enum io_method_t   io_method =  IO_METHOD_MMAP;
static int              fd = -1;
struct buffer_t          *buffers;
static unsigned int     n_buffers;
static int              save_to_file = 1; //save to file
static int              force_format_yuyv = 1; //force to use yuyv format
static int              force_format_mjpeg = 0;//force to use mjpeg 
static int              frame_count = 30 * 2; //total save frame count
static int req_width = 640;
static int req_height = 480;
static int req_rate_numerator   = 30;
static int req_rate_denominator = 1;


static int              frame_index = 0;



static void errno_exit(const char *s)
{
        fprintf(stderr, "%s error %d, %s\n", s, errno, strerror(errno));
        log_error("%s error %d, %s\n", s, errno, strerror(errno));

        logger_uninit();
        exit(EXIT_FAILURE);
}

static void general_exit_v(char const * const fmt, ...)
{
        char msg[512] = {0};
        
        va_list argp;
        va_start(argp, fmt);
        vsprintf(msg, fmt, argp);
        va_end(argp); 

        strcat(msg, "\n");

        fprintf(stderr, "%s", msg);
        log_error(msg);

        logger_uninit();
        exit(EXIT_FAILURE);      
}

static int xioctl(int fh, int request, void *arg)
{
        int r;

        do {
                r = ioctl(fh, request, arg);
        } while (-1 == r && EINTR == errno);

        return r;
}


static void generate_save_to_fullpath(char* fullpath, const char*postfix, int file_index)
{
        char cwd[256] = {0};
        getcwd(cwd, 255);
        sprintf(fullpath, "%s/pictures/%06d.%s", cwd, file_index, postfix);
}

static void process_image(const void *p, int size)
{
        frame_index++;

        //mark one frame on the console
        fprintf(stderr, ".");
        fflush(stderr);

        if(save_to_file == 1) {
                char fullpath[128] = {0};
                if(force_format_mjpeg == 1) {
                        generate_save_to_fullpath(fullpath, "jpg", frame_index);
                }
                else {
                        generate_save_to_fullpath(fullpath, "yuyv", frame_index);
                }
                
                FILE *fp = fopen(fullpath, "wb");
                if(fp == NULL) {
                        fprintf(stderr, "create file failed, file %s, error %d, %s\n", fullpath, errno, strerror(errno));
                        log_error("create file failed, file %s, error %d, %s\n", fullpath, errno, strerror(errno));
                        return;   
                } else {
                        fwrite(p, size, 1, fp);
                        fflush(fp);
                        fclose(fp);
                        log_debug("one frame coming and save to file, size=%d, path=%s \n", size, fullpath);
                }
        }
        else {
                log_debug("one frame coming and not save to file, size=%d \n", size);
        }

}

static int read_frame(void)
{
        struct v4l2_buffer buf;
        unsigned int i;

        switch (io_method) {
        case IO_METHOD_READ:
                if (-1 == read(fd, buffers[0].start, buffers[0].length)) {
                        switch (errno) {
                        case EAGAIN:
                                return 0;

                        case EIO:
                                /* Could ignore EIO, see spec. */

                                /* fall through */

                        default:
                                errno_exit("read");
                        }
                }

                process_image(buffers[0].start, buffers[0].length);
                break;

        case IO_METHOD_MMAP:
                CLEAR(buf);

                buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
                buf.memory = V4L2_MEMORY_MMAP;

                if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) {
                        switch (errno) {
                        case EAGAIN:
                                return 0;

                        case EIO:
                                /* Could ignore EIO, see spec. */

                                /* fall through */

                        default:
                                errno_exit("VIDIOC_DQBUF");
                        }
                }

                assert(buf.index < n_buffers);

                process_image(buffers[buf.index].start, buf.bytesused);

                if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
                        errno_exit("VIDIOC_QBUF");
                break;

        case IO_METHOD_USERPTR:
                CLEAR(buf);

                buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
                buf.memory = V4L2_MEMORY_USERPTR;

                if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) {
                        switch (errno) {
                        case EAGAIN:
                                return 0;

                        case EIO:
                                /* Could ignore EIO, see spec. */

                                /* fall through */

                        default:
                                errno_exit("VIDIOC_DQBUF");
                        }
                }

                for (i = 0; i < n_buffers; ++i)
                        if (buf.m.userptr == (unsigned long)buffers[i].start
                            && buf.length == buffers[i].length)
                                break;

                assert(i < n_buffers);

                process_image((void *)buf.m.userptr, buf.bytesused);

                if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
                        errno_exit("VIDIOC_QBUF");
                break;
        }

        return 1;
}

static void mainloop(void)
{
        unsigned int count;

        count = frame_count;

        while (count-- > 0) {
                for (;;) {
                        fd_set fds;
                        struct timeval tv;
                        int r;

                        FD_ZERO(&fds);
                        FD_SET(fd, &fds);

                        /* Timeout. */
                        tv.tv_sec = 30;
                        tv.tv_usec = 0;

                        r = select(fd + 1, &fds, NULL, NULL, &tv);

                        if (-1 == r) {
                                if (EINTR == errno)
                                        continue;
                                errno_exit("select");
                        }

                        if (0 == r) {
                                general_exit_v("select timeout");
                        }

                        if (read_frame())
                                break;
                        /* EAGAIN - continue select loop. */
                }
        }
}

static void stop_capturing(void)
{
        enum v4l2_buf_type type;

        switch (io_method) {
        case IO_METHOD_READ:
                /* Nothing to do. */
                break;

        case IO_METHOD_MMAP:
        case IO_METHOD_USERPTR:
                type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
                if (-1 == xioctl(fd, VIDIOC_STREAMOFF, &type))
                        errno_exit("VIDIOC_STREAMOFF");
                break;
        }
}

static void start_capturing(void)
{
        unsigned int i;
        enum v4l2_buf_type type;

        switch (io_method) {
        case IO_METHOD_READ:
                /* Nothing to do. */
                break;

        case IO_METHOD_MMAP:
                for (i = 0; i < n_buffers; ++i) {
                        struct v4l2_buffer buf;

                        CLEAR(buf);
                        buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
                        buf.memory = V4L2_MEMORY_MMAP;
                        buf.index = i;

                        if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
                                errno_exit("VIDIOC_QBUF");
                }
                type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
                if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
                        errno_exit("VIDIOC_STREAMON");
                
                log_info("%s stream on", dev_name);

                break;

        case IO_METHOD_USERPTR:
                for (i = 0; i < n_buffers; ++i) {
                        struct v4l2_buffer buf;

                        CLEAR(buf);
                        buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
                        buf.memory = V4L2_MEMORY_USERPTR;
                        buf.index = i;
                        buf.m.userptr = (unsigned long)buffers[i].start;
                        buf.length = buffers[i].length;

                        if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
                                errno_exit("VIDIOC_QBUF");
                }
                type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
                if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
                        errno_exit("VIDIOC_STREAMON");

                log_info("%s stream on", dev_name);
                break;
        }
}

static void uninit_device(void)
{
        unsigned int i;

        switch (io_method) {
        case IO_METHOD_READ:
                free(buffers[0].start);
                break;

        case IO_METHOD_MMAP:
                for (i = 0; i < n_buffers; ++i)
                        if (-1 == munmap(buffers[i].start, buffers[i].length))
                                errno_exit("munmap");
                break;

        case IO_METHOD_USERPTR:
                for (i = 0; i < n_buffers; ++i)
                        free(buffers[i].start);
                break;
        }

        free(buffers);
}

static void init_read(unsigned int buffer_size)
{
        buffers = calloc(1, sizeof(*buffers));

        if (!buffers) {
                general_exit_v("Out of memory");
        }

        buffers[0].length = buffer_size;
        buffers[0].start = malloc(buffer_size);

        if (!buffers[0].start) {
                general_exit_v("Out of memory");
        }

        log_info("initialize read&write method of %s successfully");
}

static void init_mmap(void)
{
        struct v4l2_requestbuffers req;

        CLEAR(req);

        req.count = 8;
        req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        req.memory = V4L2_MEMORY_MMAP;

        if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) {
                if (EINVAL == errno) {
                        general_exit_v("%s does not support "
                                 "memory mappingn", dev_name);
                } else {
                        errno_exit("VIDIOC_REQBUFS");
                }
        }

        if (req.count < 2) {
                general_exit_v("Insufficient buffer_t memory on %s", dev_name);
        }

        buffers = calloc(req.count, sizeof(*buffers));

        if (!buffers) {
                general_exit_v("Out of memory");
        }

        for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
                struct v4l2_buffer buf;

                CLEAR(buf);

                buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;
                buf.memory      = V4L2_MEMORY_MMAP;
                buf.index       = n_buffers;

                if (-1 == xioctl(fd, VIDIOC_QUERYBUF, &buf))
                        errno_exit("VIDIOC_QUERYBUF");

                buffers[n_buffers].length = buf.length;
                buffers[n_buffers].start =
                        mmap(NULL /* start anywhere */,
                              buf.length,
                              PROT_READ | PROT_WRITE /* required */,
                              MAP_SHARED /* recommended */,
                              fd, buf.m.offset);

                if (MAP_FAILED == buffers[n_buffers].start)
                        errno_exit("mmap");
        }

        log_info("initialize mmap method of %s successfully", dev_name);

}

static void init_userp(unsigned int buffer_size)
{
        struct v4l2_requestbuffers req;

        CLEAR(req);

        req.count  = 4;
        req.type   = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        req.memory = V4L2_MEMORY_USERPTR;

        if (-1 == xioctl(fd, VIDIOC_REQBUFS, &req)) {
                if (EINVAL == errno) {
                        general_exit_v("%s does not support user pointer i/o", dev_name);
                } else {
                        errno_exit("VIDIOC_REQBUFS");
                }
        }

        buffers = calloc(4, sizeof(*buffers));

        if (!buffers) {
                general_exit_v("Out of memory");
        }

        for (n_buffers = 0; n_buffers < 4; ++n_buffers) {
                buffers[n_buffers].length = buffer_size;
                buffers[n_buffers].start = malloc(buffer_size);

                if (!buffers[n_buffers].start) {
                         general_exit_v("Out of memory");
                }
        }

        log_info("initialize userp method of %s successfully", dev_name);

}

static int check_fmt(int fd, __u32 fmt) 
{
        struct v4l2_format v4l2_fmt;
        CLEAR(v4l2_fmt);
        v4l2_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        if (ioctl (fd, VIDIOC_G_FMT, &v4l2_fmt) == -1) {
                log_error("ERROR camera VIDIOC_G_FMT Failed.");
                return -1;
        }
        if (v4l2_fmt.fmt.pix.pixelformat == fmt) {
                return 1;
        }
        else {
                log_error("ERROR camera VIDIOC_S_FMT Failed, %u vs %u.", v4l2_fmt.fmt.pix.pixelformat, fmt);
                return -1;
        }
}


static void init_device(void)
{
        struct v4l2_capability cap;
        struct v4l2_cropcap cropcap;
        struct v4l2_crop crop;
        struct v4l2_format fmt;
        struct v4l2_streamparm streamparm;

        unsigned int min;

        if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) {
                if (EINVAL == errno) {
                        general_exit_v("%s is no V4L2 device", dev_name);
                } else {
                        errno_exit("VIDIOC_QUERYCAP");
                }
        }

        if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
                general_exit_v("%s is no video capture device",
                         dev_name);
        }


        switch (io_method) {
        case IO_METHOD_READ:
                if (!(cap.capabilities & V4L2_CAP_READWRITE)) {
                        general_exit_v("%s does not support read i/o",
                                 dev_name);
                }
                log_info("%s supports read and write", dev_name);
                break;

        case IO_METHOD_MMAP:
        case IO_METHOD_USERPTR:
                if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
                        general_exit_v("%s does not support streaming i/o",
                                 dev_name);
                }
                log_info("%s supports streaming", dev_name);
                break;
        }


        /* Select video input, video standard and tune here. */


        CLEAR(cropcap);

        cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

        if (0 == xioctl(fd, VIDIOC_CROPCAP, &cropcap)) {
                crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
                crop.c = cropcap.defrect; /* reset to default */

                if (-1 == xioctl(fd, VIDIOC_S_CROP, &crop)) {
                        switch (errno) {
                        case EINVAL:
                                /* Cropping not supported. */
                                break;
                        default:
                                /* Errors ignored. */
                                break;
                        }
                }
        } else {
                /* Errors ignored. */
        }


        CLEAR(streamparm);

        streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

        if (-1 == xioctl(fd, VIDIOC_G_PARM, &streamparm))
        errno_exit("VIDIOC_G_PARM");

        if (streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) {
                struct v4l2_fract *tpf = &(streamparm.parm.capture.timeperframe);

                // Swap numerator and denominator: rate is an inverted frame time :-)
                tpf->numerator = req_rate_denominator;
                tpf->denominator = req_rate_numerator;

                if (-1 == xioctl(fd, VIDIOC_S_PARM, &streamparm))
                        errno_exit("VIDIOC_S_PARM");

                if (tpf->numerator != req_rate_denominator ||
                        tpf->denominator != req_rate_numerator) {
                        log_error( "the driver changed the time per frame from "
                                "%d/%d to %d/%d failed",
                                req_rate_denominator, req_rate_numerator,
                                tpf->numerator, tpf->denominator);
                }
                else {
                        log_info( "the driver changed the time per frame from "
                                "%d/%d to %d/%d successfully",
                                req_rate_denominator, req_rate_numerator,
                                tpf->numerator, tpf->denominator);
                }
        } 
        else {
                log_error("the driver does not allow to change time per frame");
        }

        CLEAR(fmt);

        fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
        if (force_format_yuyv) {
                fmt.fmt.pix.width       = req_width;
                fmt.fmt.pix.height      = req_height;
                fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
                fmt.fmt.pix.field       = V4L2_FIELD_ANY;

                if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt))
                        errno_exit("VIDIOC_S_FMT");
                
                assert(check_fmt(fd, V4L2_PIX_FMT_YUYV) == 1);

                log_info("set format of %s to %u successfully", dev_name, fmt.fmt.pix.pixelformat);
                /* Note VIDIOC_S_FMT may change width and height. */
        } else if (force_format_mjpeg) {
                fmt.fmt.pix.width       = req_width;
                fmt.fmt.pix.height      = req_height;
                fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
                fmt.fmt.pix.field       = V4L2_FIELD_ANY;

                if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt))
                        errno_exit("VIDIOC_S_FMT");

                assert(check_fmt(fd, V4L2_PIX_FMT_MJPEG) == 1);

                log_info("set format of %s to %u successfully", dev_name, fmt.fmt.pix.pixelformat);
                /* Note VIDIOC_S_FMT may change width and height. */
        } else {
                /* Preserve original settings as set by v4l2-ctl for example */
                if (-1 == xioctl(fd, VIDIOC_G_FMT, &fmt))
                        errno_exit("VIDIOC_G_FMT");
                
                log_info("format of %s is %u ", dev_name, fmt.fmt.pix.pixelformat);
        }

        /* Buggy driver paranoia. */
        min = fmt.fmt.pix.width * 2;
        if (fmt.fmt.pix.bytesperline < min)
                fmt.fmt.pix.bytesperline = min;
        min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
        if (fmt.fmt.pix.sizeimage < min)
                fmt.fmt.pix.sizeimage = min;

        switch (io_method) {
        case IO_METHOD_READ:
                init_read(fmt.fmt.pix.sizeimage);
                break;

        case IO_METHOD_MMAP:
                init_mmap();
                break;

        case IO_METHOD_USERPTR:
                init_userp(fmt.fmt.pix.sizeimage);
                break;
        }
}

static void close_device(void)
{
        if (-1 == close(fd))
                errno_exit("close");

        fd = -1;
}

static void open_device(void)
{
        struct stat st;

        if (-1 == stat(dev_name, &st)) {
                general_exit_v("Cannot identify device '%s': %d, %s",
                         dev_name, errno, strerror(errno));
        }

        if (!S_ISCHR(st.st_mode)) {
                general_exit_v("%s is no device", dev_name);
        }

        fd = open(dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);

        if (-1 == fd) {
                general_exit_v("Cannot open '%s': %d, %s",
                         dev_name, errno, strerror(errno));
        }
        else {
                log_info("Open device %s successfully.", dev_name);
        }
}

static void usage(FILE *fp, int argc, char **argv)
{
        fprintf(fp,
                 "Usage: %s [options]\\n\\n"
                 "Version 1.3\\n"
                 "Options:\\n"
                 "-d | --device name   Video device name [%s]n"
                 "-h | --help          Print this messagen"
                 "-m | --mmap          Use memory mapped buffers [default]n"
                 "-r | --read          Use read() callsn"
                 "-u | --userp         Use application allocated buffersn"
                 "-s | --save          Save stream to file"
                 "-f | --format        Force format to 640x480 YUYVn"
                 "-c | --count         Number of frames to grab [%i]n"
                 "",
                 argv[0], dev_name, frame_count);
}

static const char short_options[] = "d:hmruofc:";

static const struct option
long_options[] = {
        { "device", required_argument, NULL, 'd' },
        { "help",   no_argument,       NULL, 'h' },
        { "mmap",   no_argument,       NULL, 'm' },
        { "read",   no_argument,       NULL, 'r' },
        { "userp",  no_argument,       NULL, 'u' },
        { "save", no_argument,         NULL, 's' },
        { "format", no_argument,       NULL, 'f' },
        { "count",  required_argument, NULL, 'c' },
        { 0, 0, 0, 0 }
};

int main(int argc, char **argv)
{
        dev_name = "/dev/video0";
        char log_filename[256] = {0};
        char cwd[256] = {0};
        getcwd(cwd, 255);
        sprintf(log_filename, "%s/v4l2_log.txt", cwd);
        logger_init(DEBUG, log_filename, 1);
        for (;;) {
                int idx;
                int c;

                c = getopt_long(argc, argv,
                                short_options, long_options, &idx);

                if (-1 == c)
                        break;

                switch (c) {
                case 0: /* getopt_long() flag */
                        break;

                case 'd':
                        dev_name = optarg;
                        break;

                case 'h':
                        usage(stdout, argc, argv);
                        exit(EXIT_SUCCESS);

                case 'm':
                        io_method = IO_METHOD_MMAP;
                        break;

                case 'r':
                        io_method = IO_METHOD_READ;
                        break;

                case 'u':
                        io_method = IO_METHOD_USERPTR;
                        break;

                case 's':
                        save_to_file = 1;
                        break;

                case 'j':
                        force_format_mjpeg = 1;
                        break;
                case 'n':
                        force_format_yuyv = 1;
                        break;
                case 'y':
                        errno = 0;
                        frame_count = strtol(optarg, NULL, 0);
                        if (errno)
                                errno_exit(optarg);
                        break;

                default:
                        usage(stderr, argc, argv);
                        exit(EXIT_FAILURE);
                }
        }

        open_device();
        init_device();
        start_capturing();
        mainloop();
        stop_capturing();
        uninit_device();
        close_device();
        logger_uninit(&logger);
        return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1809145.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【氵】Archlinux+KDE Plasma 6+Wayland 安装nvidia驱动 / 开启HDR

参考: NVIDIA - Arch Linux 中文维基 &#xff08;其实就是把 wiki 简化了一下 注&#xff1a;本教程适用 GeForce 930 起、10 系至 20 系、 Quadro / Tesla / Tegra K-系列以及更新的显卡&#xff08;NV110 以及更新的显卡家族&#xff09;&#xff0c;此处以 RTX3060 为例 …

PHP 寿光蔬菜大棚宣传平台-计算机毕业设计源码88288

摘 要 随着科学技术的飞速发展&#xff0c;各行各业都在努力与现代先进技术接轨&#xff0c;通过科技手段提高自身的优势&#xff1b;对于寿光蔬菜大棚宣传平台当然也不能排除在外&#xff0c;随着网络技术的不断成熟&#xff0c;带动了寿光蔬菜大棚宣传平台&#xff0c;它彻底…

连续状态方程的离散化例子

连续状态方程的离散化 在控制系统中,连续状态方程的离散化是一个重要的步骤,用于将连续时间系统转换为离散时间系统,以便在数字控制器中实现。这通常涉及将连续时间的微分方程转换为离散时间的差分方程。常用的离散化方法 前向欧拉法(Forward Euler)简单易实现,但精度较…

详解python中的pandas.read_csv()函数

&#x1f60e; 作者介绍&#xff1a;我是程序员洲洲&#xff0c;一个热爱写作的非著名程序员。CSDN全栈优质领域创作者、华为云博客社区云享专家、阿里云博客社区专家博主。 &#x1f913; 同时欢迎大家关注其他专栏&#xff0c;我将分享Web前后端开发、人工智能、机器学习、深…

OpenGL绘制简单图形

绘制了一个紫色矩形和一个三角形&#xff0c;代码如下&#xff1a; #include <Windows.h> #include <gl/glut.h> void display(void) {glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //设置清屏颜色glClear(GL_COLOR_BUFFER_BIT); //刷新颜色缓冲区&#xff1b;glColor3f…

“程序员职业素养全解析:技能、态度与价值观的融合“

文章目录 每日一句正能量前言专业精神专业精神的重要性技术执着追求的故事结论 沟通能力沟通能力的重要性团队合作意识实际工作中的沟通案例结论 持续学习持续学习的重要性学习方法进步经验结论 后记 每日一句正能量 梦不是为想象&#xff0c;而是让我们继续前往。 前言 在数字…

Policy-Based Reinforcement Learning(1)

之前提到过Discount Return&#xff1a; Action-value Function &#xff1a; State-value Function: &#xff08;这里将action A积分掉&#xff09;这里如果策略函数很好&#xff0c;就会很大&#xff1b;反之策略函数不好&#xff0c;就会很小。 对于离散类型&#xff1a; …

Qt中解决编译中文乱码和编译失败的问题

解决方法 1.使用#pragma execution_character_set(“utf-8”) QT5中在cpp中使用#pragma execution_character_set(“utf-8”)解决中文乱码&#xff0c;不过这里要求该源代码必须保存成带Bom的utf-8格式&#xff0c;这也是有些在网上下载的代码&#xff0c;加上这句源代码后还…

QPS,平均时延和并发数

我们当前有两个服务A和B&#xff0c;想要知道哪个服务的性能更好&#xff0c;该用什么指标来衡量呢&#xff1f; 1. 单次请求时延 一种最简单的方法就是使用同一请求体同时请求两个服务&#xff0c;性能越好的服务时延越短&#xff0c;即 R T 返回结果的时刻 − 发送请求的…

对猫毛过敏?怎么有效的缓解过敏症状,宠物空气净化器有用吗?

猫过敏是一种常见的过敏反应&#xff0c;由猫的皮屑、唾液或尿液中的蛋白质引起。这些蛋白质被称为过敏原&#xff0c;它们可以通过空气传播&#xff0c;被人体吸入后&#xff0c;会触发免疫系统的过度反应。猫过敏是宠物过敏中最常见的类型之一&#xff0c;对许多人来说&#…

C语言学习系列:初识C语言

前言&#xff0c;C语言是什么 语言&#xff0c;比如中文、英语、法语、德语等&#xff0c;是人与人交流的工具。 C语言也是语言&#xff0c;不过是一种特殊的语言&#xff0c;是人与计算机交流的工具。 为什么叫C语言呢&#xff1f; 这就要从C语言的历史说起了。 一&#…

11. MySQL 备份、恢复

文章目录 【 1. MySQL 备份类型 】【 2. 备份数据库 mysqldump 】2.1 备份单个数据表2.2 备份多个数据库2.3 备份所有数据库2.4 备份文件解析 【 3. 恢复数据库 mysql 】【 4. 导出表数据 OUTFILE 】【 5. 恢复表数据 INFILE 】 问题背景 尽管采取了一些管理措施来保证数据库的…

14. RTCP 协议

RTCP 协议概述 RTCP&#xff08;Real-time Transport Control Protocol 或 RTP Control Protocol 或简写 RTCP&#xff09;&#xff0c;实时传输控制协议&#xff0c;是实时传输协议&#xff08;RTP&#xff09;的一个姐妹协议。 注&#xff1a;RTP 协议和 RTP 控制协议&#…

Oracle数据库连接并访问Microsoft SQL Server数据库

Oracle数据库连接并访问Microsoft SQL Server数据库 说明&#xff1a;  1.实际开发中&#xff0c;Oracle数据库与SQLServer数据库之间可能需要相互进行访问&#xff0c;方便业务数据抽取&#xff0c;编写视图及表等操作。  2.SQLServer访问Oracle数据库配置相对较为简单&…

Python数据分析与机器学习在电子商务推荐系统中的应用

文章目录 &#x1f4d1;引言一、推荐系统的类型二、数据收集与预处理2.1 数据收集2.2 数据预处理 三、基于内容的推荐3.1 特征提取3.2 计算相似度3.3 推荐物品 四、协同过滤推荐4.1 基于用户的协同过滤4.2 基于物品的协同过滤 五、混合推荐与评估推荐系统5.1 结合推荐结果5.2 评…

抓包工具 HttpAnalyzerFull_V7.6.4 的下载、安装、使用

目录 一、简介二、下载和安装三、如何注册四、使用介绍4.1 开始、停止、清空监控内容4.2 筛选监控内容4.3 监控内容显示 一、简介 Http Analyzer 是一款功能强大的数据包分析工具&#xff0c;它可以实时监控服务器返回的消息&#xff0c;支持64位Windows系统&#xff0c;可以同…

go语言后端开发学习(三)——基于validator包实现接口校验

前言 在我们开发模块的时候,有一个问题是我们必须要去考虑的&#xff0c;它就是如何进行入参校验&#xff0c;在gin框架的博客中我就介绍过一些常见的参数校验&#xff0c;大家可以参考gin框架学习笔记(四) ——参数绑定与参数验证&#xff0c;而这个其实也不是能够完全应对我…

MYSQL 索引下推 45讲

刘老师群里,看到一位小友 问<MYSQL 45讲>林晓斌的回答 大意是一个组合索引 (a,b,c) 条件 a > 5 and a <10 and b123, 这样的情况下是如何? 林老师给的回答是 A>5 ,然后下推B123 小友 问 "为什么不是先 进行范围查询,然后在索引下推 b123?" 然后就…

python数据分析-Matplotlib绘图实例以及金融数据分析应用

通过学习扩展库matplotlib及背后的理论知识进行数据分析和可视化&#xff0c;重点以案例分析为主&#xff0c;通过实际案例演示相关理论和Python语言的应用。 读取文件countries-aggregated.csv数据&#xff0c;其中Date&#xff1a;日期, Country&#xff1a;国家, Confirmed…

格式化后硬盘数据能恢复吗?硬盘数据恢复这样做!

硬盘是电脑中必备的数据存储设备&#xff0c;另外还有移动硬盘。移动硬盘存储空间非常大、性价比高、便于携带&#xff0c;给我们带来和很多便利。但是和其他存储设备一样&#xff0c;各种硬盘也会出现各种问题&#xff0c;比如常见的格式化硬盘导致数据丢失的问题。 怎么样恢复…