summaryrefslogtreecommitdiffstats
path: root/pica-client/audiovideocallcontroller.cpp
diff options
context:
space:
mode:
authorAnton <anton@picapica.im>2026-08-24 05:06:23 +0400
committerAnton <anton@picapica.im>2026-08-24 05:06:23 +0400
commita4ddbc49c82e31ee980ea1096cc737db438c012e (patch)
tree045ae5a4031f928dedb772a8998b220f0d5aa3e2 /pica-client/audiovideocallcontroller.cpp
parent5c5fe2b41da2f4f1da32b12909a9b982e5dec70a (diff)
downloadpica-pica-a4ddbc49c82e31ee980ea1096cc737db438c012e.tar.gz
pica-pica-a4ddbc49c82e31ee980ea1096cc737db438c012e.zip
pica-client: echo cancellation, and native audio on Windows and macOS
PlatformDriverName() now takes a direction, and answers with one of two kinds of name. A bare one - "alsa", "pulse", "oss" - is an FFmpeg libavdevice driver opened the way everything else here is opened. One prefixed "platform-" is not an FFmpeg driver at all: what follows names a platform audio API the caller drives itself through NativeAudioBackend. Windows and macOS take the second route, because both provide acoustic echo cancellation but only to streams that declare themselves part of a voice call - AudioCategory_Communications on WASAPI, the VoiceProcessingIO audio unit on CoreAudio - and libavdevice's wrappers have no way to ask for either. Doing our own cancelling on platforms that would have done it better below us is not a trade worth making. Where the platform will not do it, speexdsp does: one EchoCanceller per call, shared by both directions through a QSharedPointer, since the call is torn down without joining either audio thread. Playback feeds it the far end as it hands it to the device; capture subtracts before encoding. Alignment is left to the FIFO - in steady state its backlog settles at the sound card's buffer depth, which is also how far ahead of the speaker the playback side runs, leaving only the room for the filter tail to model. Measured 27 dB ERLE on a 60 ms echo with 1.6 dB of loss on speech when nothing is playing. On macOS the canceller is a single audio unit that renders and captures at once, so both directions share one refcounted instance. Ringtones deliberately get a plain HALOutput unit instead - opening a voice unit to play a bell would switch the microphone on and prompt for permission. Linux gains a choice of alsa or pulse, with PulseAudio device enumeration through libpulse when configure finds it. Info.plist gains NSMicrophoneUsageDescription and NSCameraUsageDescription. Without them macOS 10.14 and later kills the process on first use of the microphone or camera rather than asking, so calls did not merely fail, they took the application with them. Two things fixed along the way that were not about echo at all: the settings dialog's microphone test paced playback by counting timer ticks, which on Windows meant a 20 ms packet every ~31 ms because the system timer granularity is 15.6 ms - the recording came out slowed down and chopped up. It now paces against a QElapsedTimer. And chooseEncoderSampleFmt() uses avcodec_get_supported_config() where available, the AVCodec format arrays it replaced having been removed in FFmpeg 9. Only the Linux paths are verified here; the Windows and macOS backends compile to nothing on this machine and have been reviewed by hand. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CBqDgvq3Ty6C6BjLFH16bw
Diffstat (limited to 'pica-client/audiovideocallcontroller.cpp')
-rw-r--r--pica-client/audiovideocallcontroller.cpp28
1 files changed, 28 insertions, 0 deletions
diff --git a/pica-client/audiovideocallcontroller.cpp b/pica-client/audiovideocallcontroller.cpp
index 4e91507..1f6e5ce 100644
--- a/pica-client/audiovideocallcontroller.cpp
+++ b/pica-client/audiovideocallcontroller.cpp
@@ -155,6 +155,29 @@ void AudioVideoCallController::startAudioPipeline()
m_audioSeq = 0;
m_callClock.start();
+ // One canceller for the call, shared by both directions: the playback
+ // side feeds it what it is about to play, the capture side subtracts the
+ // echo of that from the microphone. Ownership is shared because
+ // stopAudioPipeline() does not join either thread, so this has to outlive
+ // whichever of them returns last.
+ //
+ // It is created even on the platforms that cancel for us, because whether
+ // they really will is not known until the capture stream has been opened
+ // - see AudioDevice::captureNative(), which drops it once the backend
+ // says the platform has the job in hand.
+ if (st.loadValue("audio.echo_cancel", 1).toBool())
+ m_echoCanceller = EchoCancellerPtr(new EchoCanceller(48000));
+ else
+ m_echoCanceller.clear();
+
+ if (m_echoCanceller && !m_echoCanceller->isValid())
+ m_echoCanceller.clear();
+
+ // Set before the blocking loops are started, so the queued Capture()/
+ // Play() calls below are what publishes it to those threads.
+ microphone->setEchoCanceller(m_echoCanceller);
+ output->setEchoCanceller(m_echoCanceller);
+
// Declare our outgoing codec to the peer, then start capturing and
// encoding right away - the peer's decoder is only ready once it has
// processed this 0x74 message, but since the call is carried over TCP,
@@ -182,6 +205,11 @@ void AudioVideoCallController::stopAudioPipeline()
// exit after a call).
microphone->Close();
output->Close();
+
+ // Drop our reference. The two audio threads still hold theirs until they
+ // come out of Capture()/Play(), and the canceller goes away with the last
+ // of them.
+ m_echoCanceller.clear();
}
void AudioVideoCallController::startVideoPipeline()