Unverified 提交 295dbf6e authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo 提交者: GitHub

ActiveSpeakerObserver: fix memory leak and more improvements (#942)

上级 0284e0ff
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
* Transport: Remove duplicate call to method (PR #931). * Transport: Remove duplicate call to method (PR #931).
* RTCP: Adjust maximum compound packet size (PR #934). * RTCP: Adjust maximum compound packet size (PR #934).
* Fix `bufferedAmount` type to be a number again (PR #936). * Fix `bufferedAmount` type to be a number again (PR #936).
* `ActiveSpeakerObserver`: Fix memory leak (PR #942).
### 3.10.12 ### 3.10.12
......
...@@ -9,10 +9,9 @@ ...@@ -9,10 +9,9 @@
#include <vector> #include <vector>
// Implementation of Dominant Speaker Identification for Multipoint // Implementation of Dominant Speaker Identification for Multipoint
// Videoconferencing by Ilana Volfin and Israel Cohen. This // Videoconferencing by Ilana Volfin and Israel Cohen. This implementation uses
// implementation uses the RTP Audio Level extension from RFC-6464 // the RTP Audio Level extension from RFC-6464 for the input signal. This has
// for the input signal. // been ported from DominantSpeakerIdentification.java in Jitsi:
// This has been ported from DominantSpeakerIdentification.java in Jitsi.
// https://github.com/jitsi/jitsi-utils/blob/master/src/main/java/org/jitsi/utils/dsi/DominantSpeakerIdentification.java // https://github.com/jitsi/jitsi-utils/blob/master/src/main/java/org/jitsi/utils/dsi/DominantSpeakerIdentification.java
namespace RTC namespace RTC
{ {
...@@ -24,7 +23,7 @@ namespace RTC ...@@ -24,7 +23,7 @@ namespace RTC
public: public:
Speaker(); Speaker();
void EvalActivityScores(); void EvalActivityScores();
double GetActivityScore(int32_t interval); double GetActivityScore(uint8_t interval);
void LevelChanged(uint32_t level, uint64_t now); void LevelChanged(uint32_t level, uint64_t now);
void LevelTimedOut(uint64_t now); void LevelTimedOut(uint64_t now);
...@@ -39,28 +38,36 @@ namespace RTC ...@@ -39,28 +38,36 @@ namespace RTC
public: public:
bool paused{ false }; bool paused{ false };
double immediateActivityScore; double immediateActivityScore{ 0 };
double mediumActivityScore; double mediumActivityScore{ 0 };
double longActivityScore; double longActivityScore{ 0 };
uint64_t lastLevelChangeTime{ 0 }; uint64_t lastLevelChangeTime{ 0 };
private: private:
uint8_t minLevel; uint8_t minLevel{ 0u };
uint8_t nextMinLevel; uint8_t nextMinLevel{ 0u };
uint32_t nextMinLevelWindowLen{ 0 }; uint32_t nextMinLevelWindowLen{ 0u };
std::vector<uint8_t> immediates; std::vector<uint8_t> immediates;
std::vector<uint8_t> mediums; std::vector<uint8_t> mediums;
std::vector<uint8_t> longs; std::vector<uint8_t> longs;
std::vector<uint8_t> levels; std::vector<uint8_t> levels;
size_t nextLevelIndex; size_t nextLevelIndex{ 0u };
}; };
struct ProducerSpeaker class ProducerSpeaker
{ {
public:
explicit ProducerSpeaker(RTC::Producer* producer);
~ProducerSpeaker();
public:
RTC::Producer* producer; RTC::Producer* producer;
Speaker* speaker; Speaker* speaker;
}; };
private:
static const size_t RelativeSpeachActivitiesLen{ 3u };
public: public:
ActiveSpeakerObserver(const std::string& id, RTC::RtpObserver::Listener* listener, json& data); ActiveSpeakerObserver(const std::string& id, RTC::RtpObserver::Listener* listener, json& data);
~ActiveSpeakerObserver() override; ~ActiveSpeakerObserver() override;
...@@ -84,13 +91,13 @@ namespace RTC ...@@ -84,13 +91,13 @@ namespace RTC
void OnTimer(Timer* timer) override; void OnTimer(Timer* timer) override;
private: private:
static constexpr int relativeSpeachActivitiesLen{ 3 }; double relativeSpeachActivities[RelativeSpeachActivitiesLen];
double relativeSpeachActivities[relativeSpeachActivitiesLen]; std::string dominantId;
std::string dominantId{ "" };
Timer* periodicTimer{ nullptr }; Timer* periodicTimer{ nullptr };
uint16_t interval{ 300u }; uint16_t interval{ 300u };
absl::flat_hash_map<std::string, struct ProducerSpeaker> mapProducerSpeaker; // Map of ProducerSpeakers indexed by Producer id.
uint64_t lastLevelIdleTime{ 0 }; absl::flat_hash_map<std::string, ProducerSpeaker*> mapProducerSpeakers;
uint64_t lastLevelIdleTime{ 0u };
}; };
} // namespace RTC } // namespace RTC
......
...@@ -10,26 +10,28 @@ ...@@ -10,26 +10,28 @@
namespace RTC namespace RTC
{ {
constexpr uint32_t C1{ 3 }; /* Static. */
constexpr uint32_t C2{ 2 };
constexpr uint32_t C3{ 0 }; static constexpr uint32_t C1{ 3u };
constexpr uint32_t N1{ 13 }; static constexpr uint32_t C2{ 2u };
constexpr uint32_t N2{ 5 }; static constexpr uint32_t C3{ 0u };
constexpr uint32_t N3{ 10 }; static constexpr uint32_t N1{ 13u };
constexpr uint32_t LongCount{ 1 }; static constexpr uint32_t N2{ 5u };
constexpr uint32_t LevelIdleTimeout{ 40 }; static constexpr uint32_t N3{ 10u };
constexpr uint64_t SpeakerIdleTimeout{ 60 * 60 * 1000 }; static constexpr uint32_t LongCount{ 1u };
constexpr uint32_t LongThreashold{ 4 }; static constexpr uint32_t LevelIdleTimeout{ 40u };
constexpr uint32_t MaxLevel{ 127 }; static constexpr uint64_t SpeakerIdleTimeout{ 60 * 60 * 1000 };
constexpr uint32_t MinLevel{ 0 }; static constexpr uint32_t LongThreashold{ 4u };
constexpr uint32_t MinLevelWindowLen{ 15 * 1000 / 20 }; static constexpr uint32_t MaxLevel{ 127u };
constexpr uint32_t MediumThreshold{ 7 }; static constexpr uint32_t MinLevel{ 0u };
constexpr uint32_t SubunitLengthN1{ (MaxLevel - MinLevel + N1 - 1) / N1 }; static constexpr uint32_t MinLevelWindowLen{ 15 * 1000 / 20 };
constexpr uint32_t ImmediateBuffLen{ LongCount * N3 * N2 }; static constexpr uint32_t MediumThreshold{ 7u };
constexpr uint32_t MediumsBuffLen{ LongCount * N3 }; static constexpr uint32_t SubunitLengthN1{ (MaxLevel - MinLevel + N1 - 1) / N1 };
constexpr uint32_t LongsBuffLen{ LongCount }; static constexpr uint32_t ImmediateBuffLen{ LongCount * N3 * N2 };
constexpr uint32_t LevelsBuffLen{ LongCount * N3 * N2 }; static constexpr uint32_t MediumsBuffLen{ LongCount * N3 };
constexpr double MinActivityScore{ 0.0000000001 }; static constexpr uint32_t LongsBuffLen{ LongCount };
static constexpr uint32_t LevelsBuffLen{ LongCount * N3 * N2 };
static constexpr double MinActivityScore{ 0.0000000001 };
inline int64_t BinomialCoefficient(int32_t n, int32_t r) inline int64_t BinomialCoefficient(int32_t n, int32_t r)
{ {
...@@ -40,7 +42,8 @@ namespace RTC ...@@ -40,7 +42,8 @@ namespace RTC
r = m; r = m;
} }
int64_t t = 1; int64_t t{ 1 };
for (int64_t i = n, j = 1; i > r; i--, ++j) for (int64_t i = n, j = 1; i > r; i--, ++j)
{ {
t = t * i / j; t = t * i / j;
...@@ -69,11 +72,11 @@ namespace RTC ...@@ -69,11 +72,11 @@ namespace RTC
uint32_t littleLen = littles.size(); uint32_t littleLen = littles.size();
uint32_t bigLen = bigs.size(); uint32_t bigLen = bigs.size();
uint32_t littleLenPerBig = littleLen / bigLen; uint32_t littleLenPerBig = littleLen / bigLen;
bool changed = false; bool changed{ false };
for (uint32_t b = 0, l = 0; b < bigLen; b++) for (uint32_t b = 0u, l = 0u; b < bigLen; ++b)
{ {
uint8_t sum = 0; uint8_t sum{ 0u };
for (uint32_t lEnd = l + littleLenPerBig; l < lEnd; ++l) for (uint32_t lEnd = l + littleLenPerBig; l < lEnd; ++l)
{ {
...@@ -132,6 +135,16 @@ namespace RTC ...@@ -132,6 +135,16 @@ namespace RTC
ChannelMessageHandlers::UnregisterHandler(this->id); ChannelMessageHandlers::UnregisterHandler(this->id);
delete this->periodicTimer; delete this->periodicTimer;
// Must clear all entries in this->mapProducerSpeakers since RemoveProducer()
// won't be called for each existing Producer if the ActiveSpeakerObserver or
// its parent Router are directly closed.
for (auto& kv : this->mapProducerSpeakers)
{
auto* producerSpeaker = kv.second;
delete producerSpeaker;
}
this->mapProducerSpeakers.clear();
} }
void ActiveSpeakerObserver::AddProducer(RTC::Producer* producer) void ActiveSpeakerObserver::AddProducer(RTC::Producer* producer)
...@@ -141,64 +154,62 @@ namespace RTC ...@@ -141,64 +154,62 @@ namespace RTC
if (producer->GetKind() != RTC::Media::Kind::AUDIO) if (producer->GetKind() != RTC::Media::Kind::AUDIO)
MS_THROW_TYPE_ERROR("not an audio Producer"); MS_THROW_TYPE_ERROR("not an audio Producer");
if (this->mapProducerSpeaker.find(producer->id) != this->mapProducerSpeaker.end()) if (this->mapProducerSpeakers.find(producer->id) != this->mapProducerSpeakers.end())
MS_THROW_ERROR("Producer already in map"); MS_THROW_ERROR("Producer already in map");
this->mapProducerSpeaker[producer->id].producer = producer; this->mapProducerSpeakers[producer->id] = new ProducerSpeaker(producer);
this->mapProducerSpeaker[producer->id].speaker = new Speaker();
} }
void ActiveSpeakerObserver::RemoveProducer(RTC::Producer* producer) void ActiveSpeakerObserver::RemoveProducer(RTC::Producer* producer)
{ {
MS_TRACE(); MS_TRACE();
auto it = this->mapProducerSpeaker.find(producer->id); auto it = this->mapProducerSpeakers.find(producer->id);
if (it == this->mapProducerSpeaker.end()) if (it == this->mapProducerSpeakers.end())
{ {
return; return;
} }
if (it->second.speaker != nullptr) auto* producerSpeaker = it->second;
{
delete it->second.speaker; delete producerSpeaker;
it->second.speaker = nullptr;
}
this->mapProducerSpeaker.erase(producer->id); this->mapProducerSpeakers.erase(producer->id);
if (producer->id == this->dominantId) if (producer->id == this->dominantId)
{ {
this->dominantId.erase(); this->dominantId.erase();
Update(); Update();
} }
} }
void ActiveSpeakerObserver::ProducerResumed(RTC::Producer* producer) void ActiveSpeakerObserver::ProducerPaused(RTC::Producer* producer)
{ {
MS_TRACE(); MS_TRACE();
auto it = this->mapProducerSpeaker.find(producer->id); auto it = this->mapProducerSpeakers.find(producer->id);
if (it != this->mapProducerSpeaker.end()) if (it != this->mapProducerSpeakers.end())
{ {
auto& rtpObserver = it->second; auto* producerSpeaker = it->second;
rtpObserver.speaker->paused = false; producerSpeaker->speaker->paused = true;
} }
} }
void ActiveSpeakerObserver::ProducerPaused(RTC::Producer* producer) void ActiveSpeakerObserver::ProducerResumed(RTC::Producer* producer)
{ {
MS_TRACE(); MS_TRACE();
auto it = this->mapProducerSpeaker.find(producer->id); auto it = this->mapProducerSpeakers.find(producer->id);
if (it != this->mapProducerSpeaker.end()) if (it != this->mapProducerSpeakers.end())
{ {
auto& rtpObserver = it->second; auto* producerSpeaker = it->second;
rtpObserver.speaker->paused = true; producerSpeaker->speaker->paused = false;
} }
} }
...@@ -216,14 +227,14 @@ namespace RTC ...@@ -216,14 +227,14 @@ namespace RTC
return; return;
uint8_t volume = 127 - level; uint8_t volume = 127 - level;
auto it = this->mapProducerSpeaker.find(producer->id); auto it = this->mapProducerSpeakers.find(producer->id);
if (it != this->mapProducerSpeaker.end()) if (it != this->mapProducerSpeakers.end())
{ {
auto& rtpObserver = it->second; auto* producerSpeaker = it->second;
uint64_t now = DepLibUV::GetTimeMs(); uint64_t now = DepLibUV::GetTimeMs();
rtpObserver.speaker->LevelChanged(volume, now); producerSpeaker->speaker->LevelChanged(volume, now);
} }
} }
...@@ -260,10 +271,11 @@ namespace RTC ...@@ -260,10 +271,11 @@ namespace RTC
{ {
TimeoutIdleLevels(now); TimeoutIdleLevels(now);
} }
this->lastLevelIdleTime = now; this->lastLevelIdleTime = now;
} }
if (!this->mapProducerSpeaker.empty() && CalculateActiveSpeaker()) if (!this->mapProducerSpeakers.empty() && CalculateActiveSpeaker())
{ {
json data = json::object(); json data = json::object();
data["producerId"] = this->dominantId; data["producerId"] = this->dominantId;
...@@ -277,7 +289,7 @@ namespace RTC ...@@ -277,7 +289,7 @@ namespace RTC
MS_TRACE(); MS_TRACE();
std::string newDominantId; std::string newDominantId;
int32_t speakerCount = this->mapProducerSpeaker.size(); int32_t speakerCount = this->mapProducerSpeakers.size();
if (speakerCount == 0) if (speakerCount == 0)
{ {
...@@ -285,20 +297,22 @@ namespace RTC ...@@ -285,20 +297,22 @@ namespace RTC
} }
else if (speakerCount == 1) else if (speakerCount == 1)
{ {
auto it = this->mapProducerSpeaker.begin(); auto it = this->mapProducerSpeakers.begin();
auto* producerSpeaker = it->second;
newDominantId = it->second.producer->id; newDominantId = producerSpeaker->producer->id;
} }
else else
{ {
Speaker* dominantSpeaker = Speaker* dominantSpeaker = (this->dominantId.empty())
(this->dominantId.empty()) ? nullptr : this->mapProducerSpeaker[this->dominantId].speaker; ? nullptr
: this->mapProducerSpeakers.at(this->dominantId)->speaker;
if (dominantSpeaker == nullptr) if (dominantSpeaker == nullptr)
{ {
auto item = this->mapProducerSpeaker.begin(); auto it = this->mapProducerSpeakers.begin();
newDominantId = item->first; newDominantId = it->first;
dominantSpeaker = item->second.speaker; auto* producerSpeaker = it->second;
dominantSpeaker = producerSpeaker->speaker;
} }
else else
{ {
...@@ -308,10 +322,11 @@ namespace RTC ...@@ -308,10 +322,11 @@ namespace RTC
dominantSpeaker->EvalActivityScores(); dominantSpeaker->EvalActivityScores();
double newDominantC2 = C2; double newDominantC2 = C2;
for (const auto& it : this->mapProducerSpeaker) for (auto& kv : this->mapProducerSpeakers)
{ {
Speaker* speaker = it.second.speaker; auto* producerSpeaker = kv.second;
const std::string& id = it.second.producer->id; auto* speaker = producerSpeaker->speaker;
auto& id = producerSpeaker->producer->id;
if (id == this->dominantId || speaker->paused) if (id == this->dominantId || speaker->paused)
{ {
...@@ -320,7 +335,8 @@ namespace RTC ...@@ -320,7 +335,8 @@ namespace RTC
speaker->EvalActivityScores(); speaker->EvalActivityScores();
for (int interval = 0; interval < this->relativeSpeachActivitiesLen; ++interval) for (uint8_t interval = 0u; interval < ActiveSpeakerObserver::RelativeSpeachActivitiesLen;
++interval)
{ {
this->relativeSpeachActivities[interval] = std::log( this->relativeSpeachActivities[interval] = std::log(
speaker->GetActivityScore(interval) / dominantSpeaker->GetActivityScore(interval)); speaker->GetActivityScore(interval) / dominantSpeaker->GetActivityScore(interval));
...@@ -329,6 +345,7 @@ namespace RTC ...@@ -329,6 +345,7 @@ namespace RTC
double c1 = this->relativeSpeachActivities[0]; double c1 = this->relativeSpeachActivities[0];
double c2 = this->relativeSpeachActivities[1]; double c2 = this->relativeSpeachActivities[1];
double c3 = this->relativeSpeachActivities[2]; double c3 = this->relativeSpeachActivities[2];
if ((c1 > C1) && (c2 > C2) && (c3 > C3) && (c2 > newDominantC2)) if ((c1 > C1) && (c2 > C2) && (c3 > C3) && (c2 > newDominantC2))
{ {
newDominantC2 = c2; newDominantC2 = c2;
...@@ -351,10 +368,11 @@ namespace RTC ...@@ -351,10 +368,11 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
for (const auto& it : this->mapProducerSpeaker) for (auto& kv : this->mapProducerSpeakers)
{ {
Speaker* speaker = it.second.speaker; auto* producerSpeaker = kv.second;
const std::string& id = it.second.producer->id; auto* speaker = producerSpeaker->speaker;
auto& id = producerSpeaker->producer->id;
uint64_t idle = now - speaker->lastLevelChangeTime; uint64_t idle = now - speaker->lastLevelChangeTime;
if (SpeakerIdleTimeout < idle && (this->dominantId.empty() || id != this->dominantId)) if (SpeakerIdleTimeout < idle && (this->dominantId.empty() || id != this->dominantId))
...@@ -368,6 +386,22 @@ namespace RTC ...@@ -368,6 +386,22 @@ namespace RTC
} }
} }
ActiveSpeakerObserver::ProducerSpeaker::ProducerSpeaker(RTC::Producer* producer)
: producer(producer)
{
MS_TRACE();
this->speaker = new Speaker();
this->speaker->paused = producer->IsPaused();
}
ActiveSpeakerObserver::ProducerSpeaker::~ProducerSpeaker()
{
MS_TRACE();
delete this->speaker;
}
ActiveSpeakerObserver::Speaker::Speaker() ActiveSpeakerObserver::Speaker::Speaker()
: immediateActivityScore(MinActivityScore), mediumActivityScore(MinActivityScore), : immediateActivityScore(MinActivityScore), mediumActivityScore(MinActivityScore),
longActivityScore(MinActivityScore), lastLevelChangeTime(DepLibUV::GetTimeMs()), longActivityScore(MinActivityScore), lastLevelChangeTime(DepLibUV::GetTimeMs()),
...@@ -397,17 +431,17 @@ namespace RTC ...@@ -397,17 +431,17 @@ namespace RTC
} }
} }
double ActiveSpeakerObserver::Speaker::GetActivityScore(int32_t interval) double ActiveSpeakerObserver::Speaker::GetActivityScore(uint8_t interval)
{ {
MS_TRACE(); MS_TRACE();
switch (interval) switch (interval)
{ {
case 0: case 0u:
return this->immediateActivityScore; return this->immediateActivityScore;
case 1: case 1u:
return this->mediumActivityScore; return this->mediumActivityScore;
case 2: case 2u:
return this->longActivityScore; return this->longActivityScore;
default: default:
MS_ABORT("interval is invalid"); MS_ABORT("interval is invalid");
...@@ -424,7 +458,7 @@ namespace RTC ...@@ -424,7 +458,7 @@ namespace RTC
this->lastLevelChangeTime = now; this->lastLevelChangeTime = now;
int8_t b = 0; int8_t b{ 0 };
if (level < MinLevel) if (level < MinLevel)
{ {
...@@ -439,12 +473,13 @@ namespace RTC ...@@ -439,12 +473,13 @@ namespace RTC
b = level; b = level;
} }
// The algorithm expect to have an update every 20 milliseconds. If the producer is paused, // The algorithm expect to have an update every 20 milliseconds. If the
// using a different packetization time or using DTX we need to update more than one sample // Producer is paused, using a different packetization time or using DTX
// when receiving an audio packet. // we need to update more than one sample when receiving an audio packet.
uint32_t intervalsUpdated = uint32_t intervalsUpdated =
std::min(std::max(static_cast<uint32_t>(elapsed / 20), 1U), LevelsBuffLen); std::min(std::max(static_cast<uint32_t>(elapsed / 20), 1U), LevelsBuffLen);
for (uint32_t i = 0; i < intervalsUpdated; i++)
for (uint32_t i{ 0u }; i < intervalsUpdated; ++i)
{ {
this->levels[this->nextLevelIndex] = b; this->levels[this->nextLevelIndex] = b;
this->nextLevelIndex = (this->nextLevelIndex + 1) % LevelsBuffLen; this->nextLevelIndex = (this->nextLevelIndex + 1) % LevelsBuffLen;
...@@ -558,6 +593,7 @@ namespace RTC ...@@ -558,6 +593,7 @@ namespace RTC
{ {
this->nextMinLevel = level; this->nextMinLevel = level;
} }
this->nextMinLevelWindowLen++; this->nextMinLevelWindowLen++;
if (this->nextMinLevelWindowLen >= MinLevelWindowLen) if (this->nextMinLevelWindowLen >= MinLevelWindowLen)
...@@ -574,7 +610,6 @@ namespace RTC ...@@ -574,7 +610,6 @@ namespace RTC
} }
this->minLevel = static_cast<int8_t>(newMinLevel); this->minLevel = static_cast<int8_t>(newMinLevel);
this->nextMinLevel = MinLevel; this->nextMinLevel = MinLevel;
this->nextMinLevelWindowLen = 0; this->nextMinLevelWindowLen = 0;
} }
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论