Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

AVCaptureSession video and audio out of sync

I'm using an AVCaptureSession to send video and audio samples to an AVAssetWriter. When I play back the resultant video, sometimes there is a significant lag between the audio compared with the video, so they're just not in sync. But sometimes they are, with the same code.

If I look at the very first presentation time stamps of the buffers being sent to the delegate, via

  func captureOutput(_: AVCaptureOutput, didOutput sampleBuffer:   CMSampleBuffer, from connection: AVCaptureConnection)

I see something like this:

Adding audio samples for pts time 227711.0855328798, 
Adding video samples for pts time 227710.778785374

That is, the clock for audio vs video is behind: the first audio sample I receive is at 11.08 something, while the video video sample is earlier in time, at 10.778 something. The times are the presentation time stamps of the buffer, and the outputPresentationTimeStamp is the exact same number.

It feels like "video" vs the "audio" clock are just mismatched.

This doesn't always happen: sometimes they're synced. Sometimes they're not.

Any ideas? The device I'm recording is a webcam, on iPadOS, connected via the usb-c port.

Accepted Answer

This turns out to have been specific to the webcam I was using (Insta360 Link 2). That particular webcam delivers out-of-sync video/audio streams, though curiously, if you let the capture session run, within about 5 minutes it usually sorts itself out and the delay is gone. Other webcams don't seem to have this issue.

Nothing to see here folks, just keep moving along...

Davidbaraff2, Out of curiosity, were you using AVCaptureAudioDataOutput for your audio sample acquisition, or some other audio service?

Yes, I was using AVCaptureAudioDataOutput.

But as I remarked, the problem is not present on two other webcams. I further discovered that the problematic webcam gets back into sync if you just run the capture session for about 5 minutes.

There are posts on the web that the Insta360 Link 2 Webcam has known issues with audio/video sync when outputing h264 (but not, apparently, mjpeg). So this appears to really be an issue on Insta360's end.

But thanks for the reply.

AVCaptureSession video and audio out of sync
 
 
Q