From 67f2605971cf43093c72f811e4df90ab70544dd6 Mon Sep 17 00:00:00 2001 From: sudden6 Date: Sun, 1 Jul 2018 10:16:21 +0200 Subject: [PATCH] refactor(toxcall): move peer handling to ToxGroupCall --- src/core/coreav.cpp | 18 +++++++----------- src/core/toxcall.cpp | 42 ++++++++++++++++++++++++++++++++++++++++-- src/core/toxcall.h | 5 ++++- 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/src/core/coreav.cpp b/src/core/coreav.cpp index 6335c4a1c..a1fea6306 100644 --- a/src/core/coreav.cpp +++ b/src/core/coreav.cpp @@ -506,12 +506,11 @@ void CoreAV::groupCallCallback(void* tox, uint32_t group, uint32_t peer, const i } Audio& audio = Audio::getInstance(); - if (!call.getPeers()[peer]) { - // FIXME: 0 is a valid sourceId, we shouldn't necessarily re-subscribe just because we have 0 - audio.subscribeOutput(call.getPeers()[peer]); + if(!call.havePeer(peer)) { + call.addPeer(peer); } - audio.playAudioBuffer(call.getPeers()[peer], data, samples, channels, sample_rate); + audio.playAudioBuffer(call.getAlSource(peer), data, samples, channels, sample_rate); } #else void CoreAV::groupCallCallback(void* tox, int group, int peer, const int16_t* data, @@ -536,9 +535,8 @@ void CoreAV::groupCallCallback(void* tox, int group, int peer, const int16_t* da } Audio& audio = Audio::getInstance(); - if (!call.getPeers()[peer]) { - // FIXME: 0 is a valid sourceId, we shouldn't necessarily re-subscribe just because we have 0 - audio.subscribeOutput(call.getPeers()[peer]); + if(!call.havePeer(peer)) { + call.addPeer(peer); } audio.playAudioBuffer(call.getPeers()[peer], data, samples, channels, sample_rate); @@ -556,9 +554,7 @@ void CoreAV::invalidateGroupCallPeerSource(int group, int peer) if (it == groupCalls.end()) { return; } - Audio& audio = Audio::getInstance(); - audio.unsubscribeOutput(it->second.getPeers()[peer]); - it->second.getPeers()[peer] = 0; + it->second.removePeer(peer); } /** @@ -752,7 +748,7 @@ bool CoreAV::isCallOutputMuted(const Friend* f) const void CoreAV::invalidateCallSources() { for (auto& kv : groupCalls) { - kv.second.getPeers().clear(); + kv.second.clearPeers(); } for (auto& kv : calls) { diff --git a/src/core/toxcall.cpp b/src/core/toxcall.cpp index 8d35ccf3d..3daaaeff9 100644 --- a/src/core/toxcall.cpp +++ b/src/core/toxcall.cpp @@ -287,10 +287,48 @@ ToxGroupCall& ToxGroupCall::operator=(ToxGroupCall&& other) noexcept void ToxGroupCall::removePeer(int peerId) { + auto& audio = Audio::getInstance(); + const auto& sourceId = peers.find(peerId); + if(sourceId == peers.constEnd()) { + qDebug() << "Peer:" << peerId << "does not have a source, can't remove"; + return; + } + + audio.unsubscribeOutput(sourceId.value()); peers.remove(peerId); } -QMap& ToxGroupCall::getPeers() +void ToxGroupCall::addPeer(int peerId) { - return peers; + auto& audio = Audio::getInstance(); + uint sourceId = 0; + audio.subscribeOutput(sourceId); + peers.insert(peerId, sourceId); +} + +bool ToxGroupCall::havePeer(int peerId) +{ + const auto& sourceId = peers.find(peerId); + return sourceId != peers.constEnd(); +} + +void ToxGroupCall::clearPeers() +{ + Audio& audio = Audio::getInstance(); + + for (quint32 v : peers) { + audio.unsubscribeOutput(v); + } + + peers.clear(); +} + +quint32 ToxGroupCall::getAlSource(int peer) +{ + if(!havePeer(peer)) { + qWarning() << "Getting alSource for non-existant peer"; + return 0; + } + + return peers[peer]; } diff --git a/src/core/toxcall.h b/src/core/toxcall.h index de8a5b939..6cb740ffe 100644 --- a/src/core/toxcall.h +++ b/src/core/toxcall.h @@ -97,8 +97,11 @@ public: ToxGroupCall& operator=(ToxGroupCall&& other) noexcept; void removePeer(int peerId); + void addPeer(int peerId); + bool havePeer(int peerId); + void clearPeers(); - QMap& getPeers(); + quint32 getAlSource(int peer); private: QMap peers;