Skip to content
This repository has been archived by the owner on Mar 16, 2022. It is now read-only.

Commit

Permalink
Merge pull request #3 from peat-psuwit/xenial_-_fix-av-desync
Browse files Browse the repository at this point in the history
(1/2) Fix camera video A/V desync
  • Loading branch information
Flohack74 committed Jun 28, 2019
2 parents 21d3dfe + 673d248 commit 17e228e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
9 changes: 9 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
qtubuntu-camera (0.4.1+ubports0) xenial; urgency=medium

* Prevent AudioCapture from sending late samples into /dev/socket/micshm,
which will cause A/V desync because micshm doesn't accept timing.
This is done by limiting maximum PA buffer size & flush buffer before
first read.

-- Ratchanan Srirattanamet <peathot@hotmail.com> Fri, 28 Jun 2019 01:12:17 +0700

qtubuntu-camera (0.4.0+ubports0) xenial; urgency=medium

* Correctly report camera orientation by inverting the value given from
Expand Down
33 changes: 32 additions & 1 deletion src/audiocapture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <pulse/simple.h>
#include <pulse/error.h>
#include <pulse/sample.h>

#include <errno.h>
#include <fcntl.h>
Expand Down Expand Up @@ -82,6 +83,16 @@ void AudioCapture::run()
return;
}

// To reduce latency, try to flush samples not read before this function is called.
{
int ret = 0, error = 0;
ret = pa_simple_flush(m_paStream, &error);
if (ret < 0) {
qWarning() << "Failed to flush sample not read before run(): " << pa_strerror(error)
<< " (but continuing anyway).";
}
}

do {
bytesRead = readMicrophone();
if (bytesRead > 0)
Expand Down Expand Up @@ -127,9 +138,29 @@ int AudioCapture::setupMicrophoneStream()
.rate = 48000,
.channels = 1
};

/*
* On some device, it can take some time between pa_simple_new() below and
* first pa_simple_read(). By setting these buffer attributes, we try to
* make PulseAudio drop (initially) unread samples. Otherwise, we'll get
* those samples and write them into /dev/socket/micshm, which expects
* (roughly) realtime audio. This causes A/V desync as those extra samples
* will advance /dev/socket/micshm's internal timestamp ahead.
*
* I actually want to set PA_STREAM_ADJUST_LATENCY to the stream, but it
* seems to be impossible with PA's simple API.
*/
static const pa_buffer_attr buf_attr = {
.maxlength = pa_usec_to_bytes(100000 /* 100 msec */, &ss),
.tlength = (uint32_t) -1,
.prebuf = (uint32_t) -1,
.minreq = (uint32_t) -1,
.fragsize = pa_usec_to_bytes(100000 /* 100 msec */, &ss)
};

int error = 0;

m_paStream = pa_simple_new(NULL, "qtubuntu-camera", PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error);
m_paStream = pa_simple_new(NULL, "qtubuntu-camera", PA_STREAM_RECORD, NULL, "record", &ss, NULL, &buf_attr, &error);
if (m_paStream == NULL)
{
qWarning() << "Failed to open a PulseAudio channel to read the microphone: " << pa_strerror(error);
Expand Down

0 comments on commit 17e228e

Please sign in to comment.