From 3b86b6dd606baf1437bc22dd4419098ac1013bf9 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Wed, 12 Mar 2025 11:23:14 -0400 Subject: [PATCH] MT#59962 core/MultiPartyMixer: use smart pointers Automatically manage allocated storage and objects. Change-Id: I1b2f3d3dfc08e3b461fdc35badd520045783e0f5 --- core/AmMultiPartyMixer.cpp | 44 +++++--------------------------------- core/AmMultiPartyMixer.h | 10 ++++----- 2 files changed, 9 insertions(+), 45 deletions(-) diff --git a/core/AmMultiPartyMixer.cpp b/core/AmMultiPartyMixer.cpp index 4d829143..59428f32 100644 --- a/core/AmMultiPartyMixer.cpp +++ b/core/AmMultiPartyMixer.cpp @@ -56,14 +56,6 @@ AmMultiPartyMixer::AmMultiPartyMixer() { } -AmMultiPartyMixer::~AmMultiPartyMixer() -{ - for (std::deque::iterator it = buffer_state.begin(); - it != buffer_state.end(); it++) { - it->free_channels(); - } -} - unsigned int AmMultiPartyMixer::addChannel(unsigned int external_sample_rate) { unsigned int cur_channel_id = 0; @@ -254,7 +246,7 @@ std::deque::iterator AmMultiPartyMixer::findOrCreateBufferStat } //DBG("XXDebugMixerXX: Creating buffer state (from PutChannelPacket)"); - buffer_state.push_back(MixerBufferState(sample_rate, channelids)); + buffer_state.emplace_back(sample_rate, channelids); std::deque::reverse_iterator rit = buffer_state.rbegin(); //DEBUG_MIXER_BUFFER_STATE(*((rit + 1).base()), "returned to PutChannelPacket"); return (rit + 1).base(); @@ -277,7 +269,7 @@ AmMultiPartyMixer::findBufferStateForReading(unsigned int sample_rate, if (buffer_state.size() < MAX_BUFFER_STATES) { // DBG("XXDebugMixerXX: Creating buffer state (from GetChannelPacket)\n"); - buffer_state.push_back(MixerBufferState(sample_rate, channelids)); + buffer_state.emplace_back(sample_rate, channelids); } // else just reuse the last buffer - conference without a speaker std::deque::reverse_iterator rit = buffer_state.rbegin(); //DEBUG_MIXER_BUFFER_STATE(*((rit + 1).base()), "returned to PutChannelPacket"); @@ -291,7 +283,6 @@ void AmMultiPartyMixer::cleanupBufferStates(unsigned int last_ts) && (unsigned int)GetCurrentSampleRate() != buffer_state.front().sample_rate) { //DEBUG_MIXER_BUFFER_STATE(buffer_state.front(), "freed in cleanupBufferStates"); - buffer_state.front().free_channels(); buffer_state.pop_front(); } } @@ -307,24 +298,13 @@ void AmMultiPartyMixer::unlock() } MixerBufferState::MixerBufferState(unsigned int sample_rate, std::set& channelids) - : sample_rate(sample_rate), last_ts(0), channels(), mixed_channel(NULL) + : sample_rate(sample_rate), last_ts(0), channels() { for (std::set::iterator it = channelids.begin(); it != channelids.end(); it++) { channels.insert(std::make_pair(*it,new SampleArrayShort())); } - mixed_channel = new SampleArrayInt(); -} - -MixerBufferState::MixerBufferState(const MixerBufferState& other) - : sample_rate(other.sample_rate), last_ts(other.last_ts), - channels(other.channels), mixed_channel(other.mixed_channel) -{ -} - -MixerBufferState::~MixerBufferState() -{ - free_channels(); + mixed_channel.reset(new SampleArrayInt()); } void MixerBufferState::add_channel(unsigned int channel_id) @@ -337,7 +317,6 @@ void MixerBufferState::remove_channel(unsigned int channel_id) { ChannelMap::iterator channel_it = channels.find(channel_id); if (channel_it != channels.end()) { - delete channel_it->second; channels.erase(channel_it); } } @@ -350,7 +329,7 @@ SampleArrayShort* MixerBufferState::get_channel(unsigned int channel_id) return NULL; } - return channel_it->second; + return channel_it->second.get(); } void MixerBufferState::fix_channels(std::set& curchannelids) @@ -362,16 +341,3 @@ void MixerBufferState::fix_channels(std::set& curchannelids) } } } - -void MixerBufferState::free_channels() -{ - for (ChannelMap::iterator it = channels.begin(); it != channels.end(); it++) { - if (it->second != NULL) - delete it->second; - } - - if (mixed_channel) { - delete mixed_channel; - mixed_channel = NULL; - } -} diff --git a/core/AmMultiPartyMixer.h b/core/AmMultiPartyMixer.h index 2aa2b55c..99e704a3 100644 --- a/core/AmMultiPartyMixer.h +++ b/core/AmMultiPartyMixer.h @@ -41,22 +41,20 @@ struct MixerBufferState { - typedef std::map ChannelMap; + typedef std::map> ChannelMap; unsigned int sample_rate; unsigned int last_ts; ChannelMap channels; - SampleArrayInt *mixed_channel; + std::unique_ptr mixed_channel; MixerBufferState(unsigned int sample_rate, std::set& channelids); - MixerBufferState(const MixerBufferState& other); - ~MixerBufferState(); + ~MixerBufferState() {} void add_channel(unsigned int channel_id); void remove_channel(unsigned int channel_id); SampleArrayShort* get_channel(unsigned int channel_id); void fix_channels(std::set& curchannelids); - void free_channels(); }; /** @@ -91,7 +89,7 @@ class AmMultiPartyMixer public: AmMultiPartyMixer(); - ~AmMultiPartyMixer(); + ~AmMultiPartyMixer() {} unsigned int addChannel(unsigned int external_sample_rate); void removeChannel(unsigned int channel_id);