Skip to content

Incorrect clock used for set_input_skip_frame switch_time #363

@iskunk

Description

@iskunk

I am experimenting with Motion on a Debian GNU/Linux "stretch" system with an 8-input, 2-chip Bt878A PCI card.

I've configured Motion to read from all eight inputs, but when I run it, I get a constant stream of these log messages:

[3:ml3] [NTC] [VID] [Mar 29 23:42:21] v4l2_set_input: set_input_skip_frame switch_time=1490845341:530332
[3:ml3] [NTC] [VID] [Mar 29 23:42:21] v4l2_set_input: got frame before  switch timestamp=176707:108998
[3:ml3] [NTC] [VID] [Mar 29 23:42:21] v4l2_set_input: got frame before  switch timestamp=176707:143630

The thing to note is that the 1490845341:530332 and 176707:108998 represent timestamps that are supposed to be fairly close to each other, but here are way, way apart.

These two articles should explain what is going on:

https://stackoverflow.com/questions/10266451/where-does-v4l2-buffer-timestamp-value-starts-counting

http://linux-media.vger.kernel.narkive.com/d7SgCPur/v4l-camera-frame-timestamp-question

The fix here, then, should be to replace the call to gettimeofday() with a call to clock_gettime(CLOCK_MONOTONIC, ...). However, there are a couple things to consider:

  • When this code was originally added back in 2007, the V4L driver was returning frame timestamps from the realtime clock, matching up with gettimeofday(). That is obviously not the case with the bttv driver I am using today, but it is possible that some far-flung V4L drivers still do this. This should be considered a bug and the drivers fixed, but it may be something to deal with in the present.
  • It may be preferable to use the CLOCK_MONOTONIC_RAW clock instead of CLOCK_MONOTONIC here (see the clock_gettime(2) man page for details), but this is Linux-only, so the use would need to be conditional. Of course, we would need to see what the driver actually uses, since our best bet is likely to use the same clock source.
  • On some systems, clock_gettime() is in the C library; on others, it is in -lrt. (It is not consistent even on Linux---the library is needed on an RHEL6 system, but not on Debian jessie or newer.)

I am attaching a small convenience program that will print out the current values of all the clocks accessible with clock_gettime(). This can be used to check the source of the timestamps in the Motion log file.

Closely related to the above:

  • The timestamps are being printed to the log in a format that obscures their meaning. The two numbers are the .tv_sec and .tv_usec fields of a struct timeval, which is just seconds and microseconds. These should be printed with a format of "%ld.%06ld".
  • There are currently two spaces in the "got frame before switch timestamp" message, between "before" and "switch".

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions