v4l2


Where does v4l2_buffer->timestamp value starts counting?


I am trying to use v4l2_buffer's timestamp value (type timeval) to synchronize images captured from a UVC webcam to external events.
However the timestamp is not the same as the system time, or the up time, etc:
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);
Results in
image captured at 367746, 476270
current time 1335083395, 11225
My uptime is 10 days.
According to http://comments.gmane.org/gmane.linux.drivers.video-input-infrastructure/39892 some v4l2 drivers (including the UVC one) do not use the realtime clock (wall time) but rather a monotonic clock that counts from a not specified point in time. On Linux, this is the boot time (i.e. uptime), however (and I suspect this is the cause of your mismatch) only the time that the computer was actually running (i.e. this clock does not run when the computer is suspended).
If you have the OP's problem, and you're trying to get to epoch timestamps for each frame, you can use the code snippet below to do so.
#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);

Related Links

V4L2 frame rate maxes out at ~10FPS… why?
Any known-to-work example in C using VIDIOC_S_CROP in V4L2?
Getting framesize in V4L2 with VIDIOC_ENUM_FRAMESIZES
A4Tech webcam and avconv (ffmpeg) captures same frame all the time
yavta cause kernel panic (ov5640 with DM3730)
V4L2 preview with callback instead of polling-loop
Function call ioctl with VIDIOC_QUERYCAP request doesn't return
V4L2 difference between JPEG and MJPEG pixel formats
v4l2-ctl not changing from default webcam
Where I can find example in using v4l2 codec driver
Timestamp for v4l2 image capture
Where does v4l2_buffer->timestamp value starts counting?

Categories

HOME
postgresql
network-programming
blob
redhawksdr
crash
datetimepicker
mailchimp
yuv
kendo-asp.net-mvc
promotions
wowza
facebook-sharer
comma
autologin
hl7-fhir
restructuredtext
javaagents
email-client
android-xml
stack-trace
overflow
log4net-configuration
incapsula
minitest
geocomplete
flex3
rtsp
matrix-multiplication
mediastream
rbac
file-descriptor
pyexcel
intervention
automapper-4
evaluation
beanshell
visio-2010
programming-pearls
breadcrumbs
mavlink
symantec
networkstream
require-handlebars
mod
instabug
producer-consumer
mojolicious
cyberduck
autoscaling
avd
unityvs
system.io.packaging
diffmerge
os.walk
go-gin
livescript
fscanf
scjp
mvcgrid
behance-api
reactjs-native
lastinsertid
jfilechooser
dylan
nokogiri
wpa
maven-webstart-plugin
friendly-url
database-restore
invalidoperationexception
inflate
ios6-maps
pdf-reader
diazo
cinder
chars
ffdshow

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile