Skip to content
Permalink
Browse files

Merge pull request #3 from peat-psuwit/xenial_-_fix-av-desync

(1/2) Fix camera video A/V desync
  • Loading branch information...
Flohack74 committed Jun 28, 2019
2 parents 21d3dfe + 673d248 commit 17e228ecec632d36f2a537e920fcbf660f6a5d89
Showing with 41 additions and 1 deletion.
  1. +9 −0 debian/changelog
  2. +32 −1 src/audiocapture.cpp
@@ -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
@@ -20,6 +20,7 @@

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

#include <errno.h>
#include <fcntl.h>
@@ -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)
@@ -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);

0 comments on commit 17e228e

Please sign in to comment.
You can’t perform that action at this time.