From 03a8d7b8c849547ed7f3be7dc5ecef4437da5d46 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 12 Jun 2017 14:56:28 -0700 Subject: [PATCH] only replicate packets for agents being replicated --- .../src/audio/AudioMixerClientData.cpp | 77 +++++++++++-------- .../src/audio/AudioMixerClientData.h | 2 +- 2 files changed, 45 insertions(+), 34 deletions(-) diff --git a/assignment-client/src/audio/AudioMixerClientData.cpp b/assignment-client/src/audio/AudioMixerClientData.cpp index 44c51c4ae2..359c7e1b9e 100644 --- a/assignment-client/src/audio/AudioMixerClientData.cpp +++ b/assignment-client/src/audio/AudioMixerClientData.cpp @@ -80,8 +80,8 @@ void AudioMixerClientData::processPackets() { QMutexLocker lock(&getMutex()); parseData(*packet); - - replicatePacket(*packet); + + optionallyReplicatePacket(*packet, *node); break; } @@ -109,41 +109,52 @@ void AudioMixerClientData::processPackets() { assert(_packetQueue.empty()); } -void AudioMixerClientData::replicatePacket(ReceivedMessage& message) { - auto nodeList = DependencyManager::get(); +void AudioMixerClientData::optionallyReplicatePacket(ReceivedMessage& message, const Node& node) { - PacketType mirroredType; + // first, make sure that this is a packet from a node we are supposed to replicate + if (node.isReplicated()) { + auto nodeList = DependencyManager::get(); - if (message.getType() == PacketType::MicrophoneAudioNoEcho) { - mirroredType = PacketType::ReplicatedMicrophoneAudioNoEcho; - } else if (message.getType() == PacketType::MicrophoneAudioWithEcho) { - mirroredType = PacketType::ReplicatedMicrophoneAudioNoEcho; - } else if (message.getType() == PacketType::InjectAudio) { - mirroredType = PacketType::ReplicatedInjectAudio; - } else if (message.getType() == PacketType::SilentAudioFrame) { - mirroredType = PacketType::ReplicatedSilentAudioFrame; - } else { - return; + // now make sure it's a packet type that we want to replicate + PacketType mirroredType; + + if (message.getType() == PacketType::MicrophoneAudioNoEcho) { + mirroredType = PacketType::ReplicatedMicrophoneAudioNoEcho; + } else if (message.getType() == PacketType::MicrophoneAudioWithEcho) { + mirroredType = PacketType::ReplicatedMicrophoneAudioNoEcho; + } else if (message.getType() == PacketType::InjectAudio) { + mirroredType = PacketType::ReplicatedInjectAudio; + } else if (message.getType() == PacketType::SilentAudioFrame) { + mirroredType = PacketType::ReplicatedSilentAudioFrame; + } else { + return; + } + + std::unique_ptr packet; + + // enumerate the replicant audio mixers and send them the replicated version of this packet + nodeList->eachMatchingNode([&](const SharedNodePointer& node)->bool { + return node->getType() == NodeType::DownstreamAudioMixer; + }, [&](const SharedNodePointer& node) { + // construct the packet only once, if we have any downstream audio mixers to send to + if (!packet) { + // construct an NLPacket to send to the replicant that has the contents of the received packet + packet = NLPacket::create(mirroredType); + + // since this packet will be non-sourced, we add the replicated node's ID here + packet->write(message.getSourceID().toRfc4122()); + + // we won't negotiate an audio format with the replicant, because we aren't a listener + // so pack the codec string here so that it can statelessly setup a decoder for this string when it needs + packet->writeString(_selectedCodecName); + + packet->write(message.getMessage()); + } + + nodeList->sendUnreliablePacket(*packet, node->getPublicSocket()); + }); } - // construct an NLPacket to send to the replicant that has the contents of the received packet - auto packet = NLPacket::create(mirroredType); - - // since this packet will be non-sourced, we add the replicated node's ID here - packet->write(message.getSourceID().toRfc4122()); - - // we won't negotiate an audio format with the replicant, because we aren't a listener - // so pack the codec string here so that it can statelessly setup a decoder for this string when it needs - packet->writeString(_selectedCodecName); - - packet->write(message.getMessage()); - - // enumerate the replicant audio mixers and send them the replicated version of this packet - nodeList->eachMatchingNode([&](const SharedNodePointer& node)->bool { - return node->getType() == NodeType::DownstreamAudioMixer; - }, [&](const SharedNodePointer& node) { - nodeList->sendUnreliablePacket(*packet, node->getPublicSocket()); - }); } void AudioMixerClientData::negotiateAudioFormat(ReceivedMessage& message, const SharedNodePointer& node) { diff --git a/assignment-client/src/audio/AudioMixerClientData.h b/assignment-client/src/audio/AudioMixerClientData.h index 0f9e4c4c18..b397ab0b8e 100644 --- a/assignment-client/src/audio/AudioMixerClientData.h +++ b/assignment-client/src/audio/AudioMixerClientData.h @@ -126,7 +126,7 @@ private: QReadWriteLock _streamsLock; AudioStreamMap _audioStreams; // microphone stream from avatar is stored under key of null UUID - void replicatePacket(ReceivedMessage& packet); + void optionallyReplicatePacket(ReceivedMessage& packet, const Node& node); using IgnoreZone = AABox; class IgnoreZoneMemo {