From 126e5c29264566657ef727e6858a992b0ecec759 Mon Sep 17 00:00:00 2001
From: Brad Hefta-Gaub <brad@highfidelity.io>
Date: Fri, 8 Jul 2016 14:34:44 -0700
Subject: [PATCH] codec pipeline working, zlib example

---
 assignment-client/src/audio/AudioMixer.cpp       |  8 ++++++++
 libraries/audio-client/src/AudioClient.cpp       | 16 +++-------------
 libraries/audio/src/InboundAudioStream.cpp       | 11 ++++++++++-
 .../audio/src/MixedProcessedAudioStream.cpp      | 15 +++++++++++++--
 plugins/pcmCodec/src/PCMCodecManager.cpp         | 16 +++++-----------
 5 files changed, 39 insertions(+), 27 deletions(-)

diff --git a/assignment-client/src/audio/AudioMixer.cpp b/assignment-client/src/audio/AudioMixer.cpp
index 9d9194aef6..b419939912 100644
--- a/assignment-client/src/audio/AudioMixer.cpp
+++ b/assignment-client/src/audio/AudioMixer.cpp
@@ -496,6 +496,14 @@ void AudioMixer::handleNegotiateAudioFormat(QSharedPointer<ReceivedMessage> mess
 
     auto clientData = dynamic_cast<AudioMixerClientData*>(sendingNode->getLinkedData());
 
+    // FIXME - why would we not have client data at this point??
+    if (!clientData) {
+        qDebug() << "UNEXPECTED -- didn't have node linked data in " << __FUNCTION__;
+        sendingNode->setLinkedData(std::unique_ptr<NodeData> { new AudioMixerClientData(sendingNode->getUUID()) });
+        clientData = dynamic_cast<AudioMixerClientData*>(sendingNode->getLinkedData());
+        connect(clientData, &AudioMixerClientData::injectorStreamFinished, this, &AudioMixer::removeHRTFsForFinishedInjector);
+    }
+
     clientData->_codec = selectedCoded;
     clientData->_selectedCodecName = selectedCodecName;
     qDebug() << "selectedCodecName:" << selectedCodecName;
diff --git a/libraries/audio-client/src/AudioClient.cpp b/libraries/audio-client/src/AudioClient.cpp
index 9dc096eefd..a076b1b290 100644
--- a/libraries/audio-client/src/AudioClient.cpp
+++ b/libraries/audio-client/src/AudioClient.cpp
@@ -534,14 +534,13 @@ void AudioClient::negotiateAudioFormat() {
 void AudioClient::handleSelectedAudioFormat(QSharedPointer<ReceivedMessage> message) {
     qDebug() << __FUNCTION__;
 
-    // write them to our packet
-    _selectedCodecName = message->readString();
+    _receivedAudioStream._selectedCodecName = _selectedCodecName = message->readString();
 
     qDebug() << "Selected Codec:" << _selectedCodecName;
     auto codecPlugins = PluginManager::getInstance()->getCodecPlugins();
     for (auto& plugin : codecPlugins) {
         if (_selectedCodecName == plugin->getName()) {
-            _codec = plugin;
+            _receivedAudioStream._codec = _codec = plugin;
             qDebug() << "Selected Codec Plugin:" << _codec.get();
             break;
         }
@@ -849,16 +848,7 @@ void AudioClient::handleRecordedAudioInput(const QByteArray& audio) {
     emitAudioPacket(encodedBuffer.data(), encodedBuffer.size(), _outgoingAvatarAudioSequenceNumber, audioTransform, PacketType::MicrophoneAudioWithEcho);
 }
 
-void AudioClient::processReceivedSamples(const QByteArray& networkBuffer, QByteArray& outputBuffer) {
-
-    // TODO - codec decode goes here
-    QByteArray decodedBuffer;
-    if (_codec) {
-        _codec->decode(networkBuffer, decodedBuffer);
-    } else {
-        decodedBuffer = networkBuffer;
-    }
-
+void AudioClient::processReceivedSamples(const QByteArray& decodedBuffer, QByteArray& outputBuffer) {
     const int numDecodecSamples = decodedBuffer.size() / sizeof(int16_t);
     const int numDeviceOutputSamples = _outputFrameSize;
 
diff --git a/libraries/audio/src/InboundAudioStream.cpp b/libraries/audio/src/InboundAudioStream.cpp
index c19eb0c161..5d4dbd9b94 100644
--- a/libraries/audio/src/InboundAudioStream.cpp
+++ b/libraries/audio/src/InboundAudioStream.cpp
@@ -187,7 +187,16 @@ int InboundAudioStream::parseAudioData(PacketType type, const QByteArray& packet
         decodedBuffer = packetAfterStreamProperties;
     }
 
-    return _ringBuffer.writeData(decodedBuffer.data(), numAudioSamples * sizeof(int16_t)); // FIXME?
+    auto actualSize = decodedBuffer.size();
+
+    /*
+    auto expectedSize = numAudioSamples * sizeof(int16_t);
+    if (expectedSize != actualSize) {
+        qDebug() << "DECODED SIZE NOT EXPECTED!!!! ----- buffer size:" << actualSize << "expected:" << expectedSize;
+    }
+    */
+
+    return _ringBuffer.writeData(decodedBuffer.data(), actualSize);
 }
 
 int InboundAudioStream::writeDroppableSilentSamples(int silentSamples) {
diff --git a/libraries/audio/src/MixedProcessedAudioStream.cpp b/libraries/audio/src/MixedProcessedAudioStream.cpp
index d236ac7aad..220b2bd9ee 100644
--- a/libraries/audio/src/MixedProcessedAudioStream.cpp
+++ b/libraries/audio/src/MixedProcessedAudioStream.cpp
@@ -44,10 +44,21 @@ int MixedProcessedAudioStream::writeLastFrameRepeatedWithFade(int samples) {
 
 int MixedProcessedAudioStream::parseAudioData(PacketType type, const QByteArray& packetAfterStreamProperties, int networkSamples) {
 
-    emit addedStereoSamples(packetAfterStreamProperties);
+    // TODO - codec decode goes here
+    QByteArray decodedBuffer;
+    if (_codec) {
+        _codec->decode(packetAfterStreamProperties, decodedBuffer);
+    } else {
+        decodedBuffer = packetAfterStreamProperties;
+    }
+
+    qDebug() << __FUNCTION__ << "packetAfterStreamProperties:" << packetAfterStreamProperties.size() << "networkSamples:" << networkSamples << "decodedBuffer:" << decodedBuffer.size();
+
+
+    emit addedStereoSamples(decodedBuffer);
 
     QByteArray outputBuffer;
-    emit processSamples(packetAfterStreamProperties, outputBuffer);
+    emit processSamples(decodedBuffer, outputBuffer);
 
     _ringBuffer.writeData(outputBuffer.data(), outputBuffer.size());
     
diff --git a/plugins/pcmCodec/src/PCMCodecManager.cpp b/plugins/pcmCodec/src/PCMCodecManager.cpp
index d204fb1100..9d55cafedf 100644
--- a/plugins/pcmCodec/src/PCMCodecManager.cpp
+++ b/plugins/pcmCodec/src/PCMCodecManager.cpp
@@ -15,7 +15,7 @@
 
 #include "PCMCodecManager.h"
 
-const QString PCMCodecManager::NAME = "PCM Codec";
+const QString PCMCodecManager::NAME = "zlib";
 
 void PCMCodecManager::init() {
 }
@@ -39,17 +39,11 @@ bool PCMCodecManager::isSupported() const {
 
 
 void PCMCodecManager::decode(const QByteArray& encodedBuffer, QByteArray& decodedBuffer) {
-    // this codec doesn't actually do anything....
-    decodedBuffer = encodedBuffer;
-
-    //decodedBuffer = qUncompress(encodedBuffer);
-    //qDebug() << __FUNCTION__ << "from:" << encodedBuffer.size() << " to:" << decodedBuffer.size();
+    //decodedBuffer = encodedBuffer;
+    decodedBuffer = qUncompress(encodedBuffer);
 }
 
 void PCMCodecManager::encode(const QByteArray& decodedBuffer, QByteArray& encodedBuffer) {
-    // this codec doesn't actually do anything....
-    encodedBuffer = decodedBuffer;
-
-    //encodedBuffer = qCompress(decodedBuffer);
-    //qDebug() << __FUNCTION__ << "from:" << decodedBuffer.size() << " to:" << encodedBuffer.size();
+    //encodedBuffer = decodedBuffer;
+    encodedBuffer = qCompress(decodedBuffer);
 }