From 5714063eff61701955e4006cc1ba86fa3a967337 Mon Sep 17 00:00:00 2001 From: David Kelly Date: Mon, 31 Oct 2016 16:38:01 -0700 Subject: [PATCH] Make more reliable Need to be sure we don't change the _isAvatar or _isListeningToAudioStream from one thread while processing audio in another. Also, fixed issue sending NLPacketList. --- assignment-client/src/Agent.cpp | 35 +++++++++++++++++++++++++++++++-- assignment-client/src/Agent.h | 2 +- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/assignment-client/src/Agent.cpp b/assignment-client/src/Agent.cpp index 7515dda13d..02d86b5e56 100644 --- a/assignment-client/src/Agent.cpp +++ b/assignment-client/src/Agent.cpp @@ -404,8 +404,38 @@ QUuid Agent::getSessionUUID() const { return DependencyManager::get()->getSessionUUID(); } +void Agent::setIsListeningToAudioStream(bool isListeningToAudioStream) { + // this must happen on Agent's main thread + if (QThread::currentThread() != thread()) { + QMetaObject::invokeMethod(this, "setIsListeningToAudioStream", Q_ARG(bool, isListeningToAudioStream)); + return; + } + if (_isListeningToAudioStream) { + // have to tell just the audio mixer to KillAvatar. + + auto nodeList = DependencyManager::get(); + nodeList->eachMatchingNode( + [&](const SharedNodePointer& node)->bool { + return (node->getType() == NodeType::AudioMixer) + && node->getActiveSocket(); + }, + [&](const SharedNodePointer& node) { + qDebug() << "sending KillAvatar message to Avatar and Audio Mixers"; + auto packetList = NLPacketList::create(PacketType::KillAvatar, QByteArray(), true, true); + packetList->write(getSessionUUID().toRfc4122()); + nodeList->sendPacketList(std::move(packetList), *node); + }); + + } + _isListeningToAudioStream = isListeningToAudioStream; +} void Agent::setIsAvatar(bool isAvatar) { + // this must happen on Agent's main thread + if (QThread::currentThread() != thread()) { + QMetaObject::invokeMethod(this, "setIsAvatar", Q_ARG(bool, isAvatar)); + return; + } _isAvatar = isAvatar; if (_isAvatar && !_avatarIdentityTimer) { @@ -435,14 +465,15 @@ void Agent::setIsAvatar(bool isAvatar) { // when we stop sending identity, but then get woken up again by the mixer itself, which sends // identity packets to everyone. Here we explicitly tell the mixer to kill the entry for us. auto nodeList = DependencyManager::get(); - auto packetList = NLPacketList::create(PacketType::KillAvatar, QByteArray(), true, true); - packetList->write(getSessionUUID().toRfc4122()); nodeList->eachMatchingNode( [&](const SharedNodePointer& node)->bool { return (node->getType() == NodeType::AvatarMixer || node->getType() == NodeType::AudioMixer) && node->getActiveSocket(); }, [&](const SharedNodePointer& node) { + qDebug() << "sending KillAvatar message to Avatar and Audio Mixers"; + auto packetList = NLPacketList::create(PacketType::KillAvatar, QByteArray(), true, true); + packetList->write(getSessionUUID().toRfc4122()); nodeList->sendPacketList(std::move(packetList), *node); }); } diff --git a/assignment-client/src/Agent.h b/assignment-client/src/Agent.h index 939b51625a..c9b1707101 100644 --- a/assignment-client/src/Agent.h +++ b/assignment-client/src/Agent.h @@ -49,7 +49,7 @@ public: bool isPlayingAvatarSound() const { return _avatarSound != NULL; } bool isListeningToAudioStream() const { return _isListeningToAudioStream; } - void setIsListeningToAudioStream(bool isListeningToAudioStream) { _isListeningToAudioStream = isListeningToAudioStream; } + void setIsListeningToAudioStream(bool isListeningToAudioStream); float getLastReceivedAudioLoudness() const { return _lastReceivedAudioLoudness; } QUuid getSessionUUID() const;