From 29842c67cc1705ae6d0446675426cf31ac0e1fc5 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 13 Jun 2017 18:04:59 -0700 Subject: [PATCH] use received message faking for cleaner replication in audio --- assignment-client/src/audio/AudioMixer.cpp | 21 +++++++++++++++- .../src/audio/AudioMixerClientData.cpp | 24 +++++-------------- libraries/audio/src/InboundAudioStream.cpp | 12 +++++++++- 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/assignment-client/src/audio/AudioMixer.cpp b/assignment-client/src/audio/AudioMixer.cpp index f8c2cf86e8..e397254441 100644 --- a/assignment-client/src/audio/AudioMixer.cpp +++ b/assignment-client/src/audio/AudioMixer.cpp @@ -125,7 +125,26 @@ void AudioMixer::queueReplicatedAudioPacket(QSharedPointer mess replicatedNode->setLastHeardMicrostamp(usecTimestampNow()); replicatedNode->setIsUpstream(true); - getOrCreateClientData(replicatedNode.data())->queuePacket(message, replicatedNode); + // construct a "fake" avatar data received message from the byte array and packet list information + auto audioData = message->getMessage().mid(NUM_BYTES_RFC4122_UUID); + + PacketType rewrittenType; + + if (message->getType() == PacketType::ReplicatedMicrophoneAudioNoEcho) { + rewrittenType = PacketType::MicrophoneAudioNoEcho; + } else if (message->getType() == PacketType::ReplicatedMicrophoneAudioWithEcho) { + rewrittenType = PacketType::MicrophoneAudioWithEcho; + } else if (message->getType() == PacketType::ReplicatedInjectAudio) { + rewrittenType = PacketType::InjectAudio; + } else if (message->getType() == PacketType::ReplicatedSilentAudioFrame) { + rewrittenType = PacketType::SilentAudioFrame; + } + + auto replicatedMessage = QSharedPointer::create(audioData, rewrittenType, + versionForPacketType(rewrittenType), + message->getSenderSockAddr(), nodeID); + + getOrCreateClientData(replicatedNode.data())->queuePacket(replicatedMessage, replicatedNode); } void AudioMixer::handleMuteEnvironmentPacket(QSharedPointer message, SharedNodePointer sendingNode) { diff --git a/assignment-client/src/audio/AudioMixerClientData.cpp b/assignment-client/src/audio/AudioMixerClientData.cpp index e976e4176d..d4d098133d 100644 --- a/assignment-client/src/audio/AudioMixerClientData.cpp +++ b/assignment-client/src/audio/AudioMixerClientData.cpp @@ -160,10 +160,6 @@ void AudioMixerClientData::optionallyReplicatePacket(ReceivedMessage& message, c if (!isReplicatedPacket(message.getType())) { // since this packet will be non-sourced, we add the replicated node's ID here packet->write(node.getUUID().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()); @@ -312,6 +308,7 @@ int AudioMixerClientData::parseData(ReceivedMessage& message) { // this is injected audio // grab the stream identifier for this injected audio message.seek(sizeof(quint16)); + QUuid streamIdentifier = QUuid::fromRfc4122(message.readWithoutCopy(NUM_BYTES_RFC4122_UUID)); bool isStereo; @@ -346,18 +343,6 @@ int AudioMixerClientData::parseData(ReceivedMessage& message) { // seek to the beginning of the packet so that the next reader is in the right spot message.seek(0); - if (packetType == PacketType::ReplicatedMicrophoneAudioWithEcho - || packetType == PacketType::ReplicatedMicrophoneAudioNoEcho - || packetType == PacketType::ReplicatedSilentAudioFrame - || packetType == PacketType::ReplicatedInjectAudio) { - - // skip past source ID for the replicated packet - message.seek(NUM_BYTES_RFC4122_UUID); - - // skip past the codec string - message.readString(); - } - // check the overflow count before we parse data auto overflowBefore = matchingStream->getOverflowCount(); auto parseResult = matchingStream->parseData(message); @@ -706,9 +691,9 @@ bool AudioMixerClientData::shouldIgnore(const SharedNodePointer self, const Shar } void AudioMixerClientData::setupCodecForReplicatedAgent(QSharedPointer message) { - // first pull the codec string from the packet + // pull the codec string from the packet + message->seek(sizeof(quint16)); - // read the string for the codec auto codecString = message->readString(); qDebug() << "Manually setting codec for replicated agent" << uuidStringWithoutCurlyBraces(getNodeID()) @@ -718,4 +703,7 @@ void AudioMixerClientData::setupCodecForReplicatedAgent(QSharedPointerseek(0); } diff --git a/libraries/audio/src/InboundAudioStream.cpp b/libraries/audio/src/InboundAudioStream.cpp index 65cccf1fe0..56353e14e3 100644 --- a/libraries/audio/src/InboundAudioStream.cpp +++ b/libraries/audio/src/InboundAudioStream.cpp @@ -127,7 +127,17 @@ int InboundAudioStream::parseData(ReceivedMessage& message) { // parse the info after the seq number and before the audio data (the stream properties) int prePropertyPosition = message.getPosition(); int propertyBytes = parseStreamProperties(message.getType(), message.readWithoutCopy(message.getBytesLeftToRead()), networkFrames); - message.seek(prePropertyPosition + propertyBytes); + + if (message.getType() == PacketType::ReplicatedMicrophoneAudioNoEcho + || message.getType() == PacketType::ReplicatedMicrophoneAudioWithEcho + || message.getType() == PacketType::ReplicatedInjectAudio + || message.getType() == PacketType::ReplicatedSilentAudioFrame) { + message.seek(NUM_BYTES_RFC4122_UUID); + message.readString(); + message.read(sizeof(quint16) + prePropertyPosition + propertyBytes); + } else { + message.seek(prePropertyPosition + propertyBytes); + } // handle this packet based on its arrival status. switch (arrivalInfo._status) {