4

我正在尝试使用Video4Linux2 API,但是我应该用来更改给定相机上的各种控件的结构发生了一些奇怪的事情。出于某种原因,一些成员没有在分配时报告更改。我编写了以下代码来简化问题:

#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <linux/videodev2.h>
#include <stdlib.h>
#include <errno.h>
#include <stdbool.h>

int setExtendedControl(int id, int value);
int fd = 0;

int main()
{
    setExtendedControl(20, 30);
    return 0;
}

int setExtendedControl(int id, int value)
{
    struct v4l2_ext_control extControl;
    struct v4l2_ext_controls extControls;

    extControl.id = id;
    extControl.value = value;
    extControl.value64 = value;
    extControl.reserved2[0] = 0;
    extControl.reserved2[1] = 0;
    extControl.reserved2[2] = 0;

    //Put the individual control structure into the
    //multi-control container structure and initialize the container
    //as well
    extControls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
    extControls.count = 1;
    extControls.controls = &extControl;
    printf("extControls.controls = %i, extControl = %i\n", (extControls).controls, &extControl);



    extControls.reserved[0] = 0;
    extControls.reserved[1] = 0;
    extControls.reserved[2] = 0;
    extControls.error_idx = 0;

    printf("Verifying settings:\n");
    printf("extControl.id = %i, id = %i\n", extControl.id, id);
    printf("extControl.value = %i, value = %i\n", extControl.value, value);
    printf("extControl.value64 = %i, value = %i\n", extControl.value64, value);
    printf("extControl.reserved2[0] = %i, set to 0\n", extControl.reserved2[0]);
    printf("extControl.reserved2[1] = %i, set to 0\n", extControl.reserved2[1]);
    printf("extControl.reserved2[2] = %i, set to 0\n\n", extControl.reserved2[2]);

    printf("extControls.ctrl_class = %i, V4L2_CTRL_CLASS_MPEG = %i\n", extControls.ctrl_class,
            V4L2_CTRL_CLASS_MPEG);
    printf("extControls.count = %i, set to 1\n", extControls.count);
    printf("extControls.reserved[0] = %i, set to 0\n", extControls.reserved[0]);
    printf("extControls.reserved[1] = %i, set to 0\n", extControls.reserved[1]);
    printf("extControls.reserved[2] = %i, set to 0\n", extControls.reserved[2]);
    printf("extControls.error_idx = %i, set to 0\n", extControls.error_idx);

    printf ("\nRunning secondary check..\n\n");


    int rval;

    //Set up the individual control structure



    //Try to change the control and return the
    //value reporting the outcome.
    rval = ioctl(fd, VIDIOC_S_EXT_CTRLS, &extControls);

    if (extControls.controls != &extControl)
    {
        printf("\n\nLost the pointer on initial set!\n");
    }
    //printf("error_idx after initial set %i\n", extControls.error_idx);

    //printf("extControl = %i, extControls = %i\n");

    //freeStructs(extControl, extControls);

    return rval;
}

当我运行它时,没有设置“value”和“value64”成员。以下是 printf 输出:

extControls.controls = -1954893344, extControl = -1954893344
Verifying settings:
extControl.id = 20, id = 20
extControl.value = 0, value = 30
extControl.value64 = 0, value = 30
extControl.reserved2[0] = 0, set to 0
extControl.reserved2[1] = 0, set to 0
extControl.reserved2[2] = 0, set to 0

extControls.ctrl_class = 10027008, V4L2_CTRL_CLASS_MPEG = 10027008
extControls.count = 1, set to 1
extControls.reserved[0] = 0, set to 0
extControls.reserved[1] = 0, set to 0
extControls.reserved[2] = 0, set to 0
extControls.error_idx = 0, set to 0

这是来自 gdb 的一个小片段:

(gdb) step
printf (__fmt=0x400880 "extControls.controls = %i, extControl = %i\n") at /usr/include/x86_64-linux-gnu/bits/stdio2.h:105
105       return __printf_chk (__USE_FORTIFY_LEVEL - 1, __fmt, __va_arg_pack ());
(gdb) step
setExtendedControl (id=20, value=30) at ioctlTest.c:30
30          extControl.reserved2[0] = 0;
(gdb) step
Hardware watchpoint 2: extControl

Old value =
    {id = 4294960502, size = 32767, reserved2 = {4196309}, {value = 0, value64 = 67750802696962048, string = 0xf0b2ff00000000 <Address 0xf0b2ff00000000 out of bounds>}}
New value =
    {id = 20, size = 32767, reserved2 = {0}, {value = 0, value64 = 67750802696962048, string = 0xf0b2ff00000000 <Address 0xf0b2ff00000000 out of bounds>}}

这里没有发生,但是在更强大的代码版本中还有其他实例,其中 extControls.controls 的指针分配和成员 extControl.id 的 int 分配都失败了。什么会导致这种性质的结构成员分配失败?

提前致谢!

4

2 回答 2

3

有关结构信息,请参阅内核文档

该结构的reserved[]数组v4l2_ext_control长度仅为 2 ...因此您正在修改不应该使用该行的内存:

extControl.reserved2[2] = 0;

于 2012-06-13T22:51:14.033 回答
3

我自己无法检查,但查看(表 1)的这个规范struct v4l2_ext_control,它说:

表格1。 struct v4l2_ext_control

__u32   id      Identifies the control, set by the application.
__u32   reserved2[2]        Reserved for future extensions. Drivers and applications must set the array to zero.

union   (anonymous)      
    __s32   value       New value or current value.
    __s64   value64     New value or current value.
    void *  reserved    Reserved for future pointer-type controls. Currently unused.

这里注意三点:

  • value并且value64是联合的一部分,它们在内存中占据相同的位置
  • reserved2 的大小为 2,所以只有 , reserved2[0], reserved2[1]no reserved2[2]
  • 根据这个规范,reserved内存地址在结构内的联合之前

这意味着当你设置

extControl.reserved2[2] = 0;

您实际上是在分配给该数组的内存之外写入,覆盖联合的数据,从而同时改变“两者”valuevalue64成员。

于 2012-06-13T22:52:51.713 回答