// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // KOutNet - Voice call manager (P2P calls, group calls via per-peer jitter buffers) #include "VoiceCallManager.h" #include "../core/audio/AudioEngine.h" #include "NetworkManager.h" namespace koutnet { VoiceCallManager::VoiceCallManager(NetworkManager *net, QObject *parent) : QObject(parent) , m_net(net) { m_audio = new AudioEngine(this); connect(m_audio, &AudioEngine::audioCaptured, this, &VoiceCallManager::onCaptured); connect(m_net, &NetworkManager::voiceDataFrom, this, &VoiceCallManager::onPeerAudio); // connectVoice() dials without waiting, so a refused, timed out or dropped // voice socket is what says a call is over. hangup() ignores IPs it does not // hold; a channel coming up is what makes a call, whether we dialed it or // accepted it. The transport only reports a peer the chat channel pinned. connect(m_net, &NetworkManager::voiceDisconnected, this, &VoiceCallManager::hangup); connect(m_net, &NetworkManager::voiceConnected, this, [this](const QString &ip) { m_dialing.remove(ip); if (m_active.contains(ip)) return; // inbound and outbound raced; one call is enough if (!m_audio->running() && !m_audio->startCapture()) return; // a channel with no capture is not a usable call m_audio->mixer().addPeer(ip); m_active.insert(ip); Q_EMIT callStarted(ip); Q_EMIT activeCallsChanged(); }); } bool VoiceCallManager::call(const QString &ip, const QStringList &candidates) { if (m_active.contains(ip)) return true; // already in a call with this peer if (m_dialing.contains(ip)) return true; // already trying to get a channel up if (!m_audio->running()) { if (!m_audio->startCapture()) return false; // no mic or speaker available } m_audio->mixer().addPeer(ip); const QStringList dialTargets = candidates.isEmpty() ? QStringList{ip} : candidates; if (!m_net->dialVoiceCandidates(ip, dialTargets)) { m_audio->mixer().removePeer(ip); if (m_active.isEmpty() && m_dialing.isEmpty()) m_audio->stopAll(); return false; } // connectVoice() is a look-ahead, so the peer stays a dial until the socket // really comes up and the voiceConnected handler above promotes it to a call. m_dialing.insert(ip); Q_EMIT activeCallsChanged(); return true; } void VoiceCallManager::hangup(const QString &ip) { const bool wasActive = m_active.remove(ip); const bool wasDialing = m_dialing.remove(ip); if (!wasActive && !wasDialing) return; m_audio->mixer().removePeer(ip); m_net->disconnectVoice(ip); if (m_active.isEmpty() && m_dialing.isEmpty()) m_audio->stopAll(); Q_EMIT activeCallsChanged(); // the UI hears about it through NetworkManager::callEnded, which the socket // side emits when the disconnect lands; a remote hangup arrives the same way. } void VoiceCallManager::hangupAll() { const auto ips = m_active + m_dialing; // copy - hangup() mutates both sets for (const auto &ip : ips) hangup(ip); } void VoiceCallManager::setMute(bool muted) { m_muted = muted; // Deafened outranks the mute flag: while it is on the microphone stays shut. m_audio->setMuted(muted || m_deafened); } void VoiceCallManager::setDeafen(bool deafened) { m_deafened = deafened; m_audio->setDeafened(deafened); // Restores the mute the user chose, which is why m_muted was not overwritten. m_audio->setMuted(m_muted || deafened); } void VoiceCallManager::setVad(bool enabled) { m_audio->setVadEnabled(enabled); } void VoiceCallManager::setAudioInputDevice(const QString &id) { m_audio->setInputDeviceId(id); } void VoiceCallManager::setAudioOutputDevice(const QString &id) { m_audio->setOutputDeviceId(id); } void VoiceCallManager::setAudioVolume(qreal volume) { m_audio->setVolume(qBound(0.0, volume, 1.0)); } void VoiceCallManager::onCaptured(const QByteArray &data) { // Media confidentiality is the voice socket's business: raw frames go out // as-is and the TLS 1.3 transport they ride protects them in flight. for (const auto &ip : std::as_const(m_active)) m_net->sendVoice(ip, data); } void VoiceCallManager::onPeerAudio(const QString &ip, const QByteArray &data) { if (!m_active.contains(ip)) return; m_audio->pushPeerAudio(ip, data); } } // namespace koutnet