v4l2_buffer-> timestamp值从哪里开始计数?

时间:2012-04-22 08:36:08

标签: v4l2

我正在尝试使用v4l2_buffer的时间戳值(类型timeval)将从 UVC 网络摄像头捕获的图像同步到外部事件。

但是时间戳与系统时间或正常运行时间等不同:

printf("image captured at %ld, %ld\n",
   buffer->timestamp.tv_sec, 
   buffer->timestamp.tv_usec);

struct timeval tv;
gettimeofday(&tv, 0);
printf("current time %ld, %ld\n", tv.tv_sec, tv.tv_usec);

结果

image captured at 367746, 476270
current time 1335083395, 11225

我的正常运行时间是10天。

2 个答案:

答案 0 :(得分:5)

根据http://comments.gmane.org/gmane.linux.drivers.video-input-infrastructure/39892一些v4l2驱动程序(包括UVC驱动程序)不使用实时时钟(挂起时间),而是使用从非指定时间点开始计数的单调时钟。在Linux上,这是启动时间(即正常运行时间),但是(我怀疑这是导致您不匹配的原因)只是计算机实际运行的时间(即计算机暂停时此时钟不运行)。 / p>

答案 1 :(得分:2)

如果您遇到OP的问题,并且您尝试获取每个帧的纪元时间戳,则可以使用下面的代码段来执行此操作。

#include <time.h>
#include <math.h>

//////////////////////
//setup: 

    long getEpochTimeShift(){
        struct timeval epochtime;
        struct timespec  vsTime;

        gettimeofday(&epochtime, NULL);
        clock_gettime(CLOCK_MONOTONIC, &vsTime);

        long uptime_ms = vsTime.tv_sec* 1000 + (long)  round( vsTime.tv_nsec/ 1000000.0);
        long epoch_ms =  epochtime.tv_sec * 1000  + (long) round( epochtime.tv_usec/1000.0);
        return epoch_ms - uptime_ms;
    }

    //stick this somewhere so that it runs once, on the startup of your capture process
    //  noting, if you hibernate a laptop, you might need to recalc this if you don't restart 
    //  the process after dehibernation
    long toEpochOffset_ms = getEpochTimeShift();


//////////////////////
//...somewhere in your capture loop: 

    struct v4l2_buffer buf;

    //make the v4l call to  xioctl(fd, VIDIOC_DQBUF, &buf)

    //then: 
    long temp_ms = 1000 * buf.timestamp.tv_sec + (long) round(  buf.timestamp.tv_usec / 1000.0);
    long epochTimeStamp_ms = temp_ms + toEpochOffset_ms ;

    printf( "the frame's timestamp in epoch ms is: %ld", epochTimeStamp_ms);