[FFmpeg-user] Bad A/V Sync Using ffmpeg on V4L2 Input Device

Olivier Langlois olivier at trillion01.com
Sat Mar 29 07:09:32 CET 2014


On Fri, 2014-03-28 at 23:24 -0400, Olivier Langlois wrote:
> On Tue, 2014-03-25 at 01:32 -0700, Leo L. Schwab wrote:
> > LANGLOIS Olivier PIS wrote:
> > > Notice the start times of each input. For alsa, it uses abs time and by
> > > default, webcam uses monotonic time. Something that I was about to try and
> > > perhaps you try it out to see if it helps is to use ts abs for the v4l2
> > > input.
> > >
> > 	As illustrated in my response to Lou's message, adding '-ts abs' to
> > the command line doesn't help; I still see the sync issue.
> > 
> > > However, it might not be enough. Check if your webcam is using the
> > > uvcvideo driver.  [ ... ]
> > 
> > 	Nope.  This one uses the em28xx driver.
> > 
> > 					Schwab
> how about -ts mono2abs ?
> 
> BTW, I tried out the uvcvideo clock=realtime param and it didn't work
> before applying a patch that I have submitted.
> 
> uvcvideo maintainer did bring a very good point. Why ffmpeg does not use
> monotonic clock for ALSA audio packets instead of doing the opposite
> conversion with v4l2 ts?
> 
> If curious, you can see the discussion at:
> https://lkml.org/lkml/2014/3/28/12
> 
> Besides portability concerns, in general should not monotonic ts be a
> superior choice for a multimedia application?
> 
> What would happen to ffmpeg if the time goes backward after a NTP
> update?

Schwab,

If you want to have monotonic ts all over ffmpeg, you can give it a try
with this experimental patch below.

I haven't noticed any ill effect from it so far.

If it still does not fix your problem, I still see some options.

1. There a certainly options to add some delay to one of your stream to
correct the sync issue.
2. report the problem to EM28XX maintainer:

Mauro Carvalho Chehab <m.chehab at samsung.com> (maintainer:EM28XX
VIDEO4LINU...)
linux-media at vger.kernel.org (open list:EM28XX VIDEO4LINU...)

3. get yourself a device using a different driver.

--- ffmpeg-2.2/libavutil/time.c.orig	2014-03-29 01:46:13.496164829 -0400
+++ ffmpeg-2.2/libavutil/time.c	2014-03-29 01:50:55.947143330 -0400
@@ -38,7 +38,15 @@
 
 int64_t av_gettime(void)
 {
-#if HAVE_GETTIMEOFDAY
+#if HAVE_CLOCK_GETTIME
+    struct timespec ts;
+#ifdef CLOCK_MONOTONIC
+    clock_gettime(CLOCK_MONOTONIC, &ts);
+#else
+    clock_gettime(CLOCK_REALTIME, &ts);
+#endif
+    return (int64_t)ts.tv_sec * 1000000 + ts.tv_nsec/1000;
+#elif HAVE_GETTIMEOFDAY
     struct timeval tv;
     gettimeofday(&tv, NULL);
     return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;





More information about the ffmpeg-user mailing list