some cleanup, proper memory allocation/deallocation

This commit is contained in:
Brad Hefta-Gaub 2016-07-10 17:40:58 -07:00
parent ba6bb24595
commit ed9715ae5f
6 changed files with 81 additions and 35 deletions

View file

@ -478,7 +478,7 @@ void AudioMixer::handleNegotiateAudioFormat(QSharedPointer<ReceivedMessage> mess
qDebug() << "No Codecs available..."; qDebug() << "No Codecs available...";
} }
CodecPluginPointer selectedCoded; CodecPluginPointer selectedCodec;
QString selectedCodecName; QString selectedCodecName;
QStringList codecPreferenceList = _codecPreferenceOrder.split(","); QStringList codecPreferenceList = _codecPreferenceOrder.split(",");
@ -513,7 +513,7 @@ void AudioMixer::handleNegotiateAudioFormat(QSharedPointer<ReceivedMessage> mess
for (auto& plugin : codecPlugins) { for (auto& plugin : codecPlugins) {
if (selectedCodecName == plugin->getName()) { if (selectedCodecName == plugin->getName()) {
qDebug() << "Selecting codec:" << selectedCodecName; qDebug() << "Selecting codec:" << selectedCodecName;
selectedCoded = plugin; selectedCodec = plugin;
break; break;
} }
} }
@ -531,18 +531,13 @@ void AudioMixer::handleNegotiateAudioFormat(QSharedPointer<ReceivedMessage> mess
connect(clientData, &AudioMixerClientData::injectorStreamFinished, this, &AudioMixer::removeHRTFsForFinishedInjector); connect(clientData, &AudioMixerClientData::injectorStreamFinished, this, &AudioMixer::removeHRTFsForFinishedInjector);
} }
clientData->_codec = selectedCoded; clientData->setupCodec(selectedCodec, selectedCodecName);
clientData->_selectedCodecName = selectedCodecName;
clientData->_encoder = selectedCoded->createEncoder(AudioConstants::SAMPLE_RATE, AudioConstants::STEREO);
clientData->_decoder = selectedCoded->createDecoder(AudioConstants::SAMPLE_RATE, AudioConstants::MONO);
qDebug() << "selectedCodecName:" << selectedCodecName; qDebug() << "selectedCodecName:" << selectedCodecName;
auto avatarAudioStream = clientData->getAvatarAudioStream(); auto avatarAudioStream = clientData->getAvatarAudioStream();
if (avatarAudioStream) { if (avatarAudioStream) {
avatarAudioStream->_codec = selectedCoded; avatarAudioStream->setupCodec(selectedCodec, selectedCodecName, AudioConstants::MONO);
avatarAudioStream->_selectedCodecName = selectedCodecName;
avatarAudioStream->_decoder = selectedCoded->createDecoder(AudioConstants::SAMPLE_RATE, AudioConstants::MONO);
} }
auto replyPacket = NLPacket::create(PacketType::SelectedAudioFormat); auto replyPacket = NLPacket::create(PacketType::SelectedAudioFormat);
@ -777,14 +772,9 @@ void AudioMixer::broadcastMixes() {
quint16 sequence = nodeData->getOutgoingSequenceNumber(); quint16 sequence = nodeData->getOutgoingSequenceNumber();
mixPacket->writePrimitive(sequence); mixPacket->writePrimitive(sequence);
// TODO - codec encode goes here QByteArray decodedBuffer(reinterpret_cast<char*>(_clampedSamples), AudioConstants::NETWORK_FRAME_BYTES_STEREO);
QByteArray decocedBuffer(reinterpret_cast<char*>(_clampedSamples), AudioConstants::NETWORK_FRAME_BYTES_STEREO);
QByteArray encodedBuffer; QByteArray encodedBuffer;
if (nodeData->_codec) { nodeData->encode(decodedBuffer, encodedBuffer);
nodeData->_encoder->encode(decocedBuffer, encodedBuffer);
} else {
encodedBuffer = decocedBuffer;
}
// pack mixed audio samples // pack mixed audio samples
mixPacket->write(encodedBuffer.constData(), encodedBuffer.size()); mixPacket->write(encodedBuffer.constData(), encodedBuffer.size());

View file

@ -110,12 +110,8 @@ int AudioMixerClientData::parseData(ReceivedMessage& message) {
bool isStereo = channelFlag == 1; bool isStereo = channelFlag == 1;
auto avatarAudioStream = new AvatarAudioStream(isStereo, AudioMixer::getStreamSettings()); auto avatarAudioStream = new AvatarAudioStream(isStereo, AudioMixer::getStreamSettings());
avatarAudioStream->_codec = _codec; avatarAudioStream->setupCodec(_codec, _selectedCodecName, AudioConstants::MONO);
avatarAudioStream->_selectedCodecName = _selectedCodecName; qDebug() << "creating new AvatarAudioStream... codec:" << _selectedCodecName;
if (_codec) {
avatarAudioStream->_decoder = _codec->createDecoder(AudioConstants::SAMPLE_RATE, AudioConstants::MONO);
}
qDebug() << "creating new AvatarAudioStream... codec:" << avatarAudioStream->_selectedCodecName;
auto emplaced = _audioStreams.emplace( auto emplaced = _audioStreams.emplace(
QUuid(), QUuid(),
@ -340,3 +336,25 @@ QJsonObject AudioMixerClientData::getAudioStreamStats() {
return result; return result;
} }
void AudioMixerClientData::setupCodec(CodecPluginPointer codec, const QString& codecName) {
cleanupCodec(); // cleanup any previously allocated coders first
_codec = codec;
_selectedCodecName = codecName;
_encoder = codec->createEncoder(AudioConstants::SAMPLE_RATE, AudioConstants::STEREO);
_decoder = codec->createDecoder(AudioConstants::SAMPLE_RATE, AudioConstants::MONO);
}
void AudioMixerClientData::cleanupCodec() {
// release any old codec encoder/decoder first...
if (_codec) {
if (_decoder) {
_codec->releaseDecoder(_decoder);
_decoder = nullptr;
}
if (_encoder) {
_codec->releaseEncoder(_encoder);
_encoder = nullptr;
}
}
}

View file

@ -68,12 +68,15 @@ public:
AudioLimiter audioLimiter; AudioLimiter audioLimiter;
// FIXME -- maybe make these private void setupCodec(CodecPluginPointer codec, const QString& codecName);
CodecPluginPointer _codec; void cleanupCodec();
QString _selectedCodecName; void encode(const QByteArray& decodedBuffer, QByteArray& encodedBuffer) {
Encoder* _encoder { nullptr }; // for outbound mixed stream if (_encoder) {
Decoder* _decoder { nullptr }; // for mic stream _encoder->encode(decodedBuffer, encodedBuffer);
} else {
encodedBuffer = decodedBuffer;
}
}
signals: signals:
void injectorStreamFinished(const QUuid& streamIdentifier); void injectorStreamFinished(const QUuid& streamIdentifier);
@ -91,6 +94,11 @@ private:
AudioStreamStats _downstreamAudioStreamStats; AudioStreamStats _downstreamAudioStreamStats;
int _frameToSendStats { 0 }; int _frameToSendStats { 0 };
CodecPluginPointer _codec;
QString _selectedCodecName;
Encoder* _encoder{ nullptr }; // for outbound mixed stream
Decoder* _decoder{ nullptr }; // for mic stream
}; };
#endif // hifi_AudioMixerClientData_h #endif // hifi_AudioMixerClientData_h

View file

@ -140,6 +140,10 @@ AudioClient::AudioClient() :
AudioClient::~AudioClient() { AudioClient::~AudioClient() {
stop(); stop();
if (_codec && _encoder) {
_codec->releaseEncoder(_encoder);
_encoder = nullptr;
}
} }
void AudioClient::reset() { void AudioClient::reset() {
@ -529,14 +533,19 @@ void AudioClient::negotiateAudioFormat() {
void AudioClient::handleSelectedAudioFormat(QSharedPointer<ReceivedMessage> message) { void AudioClient::handleSelectedAudioFormat(QSharedPointer<ReceivedMessage> message) {
qDebug() << __FUNCTION__; qDebug() << __FUNCTION__;
_receivedAudioStream._selectedCodecName = _selectedCodecName = message->readString(); _selectedCodecName = message->readString();
qDebug() << "Selected Codec:" << _selectedCodecName; qDebug() << "Selected Codec:" << _selectedCodecName;
auto codecPlugins = PluginManager::getInstance()->getCodecPlugins(); auto codecPlugins = PluginManager::getInstance()->getCodecPlugins();
for (auto& plugin : codecPlugins) { for (auto& plugin : codecPlugins) {
if (_selectedCodecName == plugin->getName()) { if (_selectedCodecName == plugin->getName()) {
_receivedAudioStream._codec = _codec = plugin; // release any old codec encoder/decoder first...
_receivedAudioStream._decoder = plugin->createDecoder(AudioConstants::SAMPLE_RATE, AudioConstants::STEREO); if (_codec && _encoder) {
_codec->releaseEncoder(_encoder);
_encoder = nullptr;
}
_codec = plugin;
_receivedAudioStream.setupCodec(plugin, _selectedCodecName, AudioConstants::STEREO);
_encoder = plugin->createEncoder(AudioConstants::SAMPLE_RATE, AudioConstants::MONO); _encoder = plugin->createEncoder(AudioConstants::SAMPLE_RATE, AudioConstants::MONO);
qDebug() << "Selected Codec Plugin:" << _codec.get(); qDebug() << "Selected Codec Plugin:" << _codec.get();
break; break;

View file

@ -504,3 +504,21 @@ float calculateRepeatedFrameFadeFactor(int indexOfRepeat) {
return 0.0f; return 0.0f;
} }
void InboundAudioStream::setupCodec(CodecPluginPointer codec, const QString& codecName, int numChannels) {
cleanupCodec(); // cleanup any previously allocated coders first
_codec = codec;
_selectedCodecName = codecName;
if (_codec) {
_decoder = codec->createDecoder(AudioConstants::SAMPLE_RATE, numChannels);
}
}
void InboundAudioStream::cleanupCodec() {
// release any old codec encoder/decoder first...
if (_codec) {
if (_decoder) {
_codec->releaseDecoder(_decoder);
_decoder = nullptr;
}
}
}

View file

@ -105,6 +105,7 @@ public:
public: public:
InboundAudioStream(int numFrameSamples, int numFramesCapacity, const Settings& settings); InboundAudioStream(int numFrameSamples, int numFramesCapacity, const Settings& settings);
~InboundAudioStream() { cleanupCodec(); }
void reset(); void reset();
virtual void resetStats(); virtual void resetStats();
@ -176,10 +177,8 @@ public:
void setReverb(float reverbTime, float wetLevel); void setReverb(float reverbTime, float wetLevel);
void clearReverb() { _hasReverb = false; } void clearReverb() { _hasReverb = false; }
// FIXME -- maybe make these private void setupCodec(CodecPluginPointer codec, const QString& codecName, int numChannels);
CodecPluginPointer _codec; void cleanupCodec();
QString _selectedCodecName;
Decoder* _decoder { nullptr };
public slots: public slots:
/// This function should be called every second for all the stats to function properly. If dynamic jitter buffers /// This function should be called every second for all the stats to function properly. If dynamic jitter buffers
@ -274,6 +273,10 @@ protected:
bool _hasReverb; bool _hasReverb;
float _reverbTime; float _reverbTime;
float _wetLevel; float _wetLevel;
CodecPluginPointer _codec;
QString _selectedCodecName;
Decoder* _decoder{ nullptr };
}; };
float calculateRepeatedFrameFadeFactor(int indexOfRepeat); float calculateRepeatedFrameFadeFactor(int indexOfRepeat);