提交 9fa46f3e authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

ActiveSpeakerDetector: improve logic

上级 d25bd20d
......@@ -3,7 +3,8 @@
const Logger = require('../Logger');
const EnhancedEventEmitter = require('../EnhancedEventEmitter');
const MIN_AUDIO_LEVEL = -70; // dB.
const REQUIRED_MIN_AUDIO_LEVEL = -70; // dB.
const REQUIRED_MAX_NUM_REPORTS = 4;
const logger = new Logger('ActiveSpeakerDetector');
......@@ -23,9 +24,9 @@ class ActiveSpeakerDetector extends EnhancedEventEmitter
// @type {Room}
this._room = room;
// Latest max audio levels.
// Latest max audio level reports.
// @type {Array}
this._latestMaxAudioLevels = [];
this._reports = [];
// Current active Producer.
// @type {Producer}
......@@ -74,83 +75,114 @@ class ActiveSpeakerDetector extends EnhancedEventEmitter
if (this._closed)
return;
const times = 2;
const latest = this._latestMaxAudioLevels;
let previousActiveProducer = this._activeProducer;
let requiredNumReports;
// Insert the loudest audio level entry into the latest array.
if (
previousActiveProducer &&
!previousActiveProducer.closed &&
!previousActiveProducer.paused
)
{
requiredNumReports = REQUIRED_MAX_NUM_REPORTS;
}
else
{
previousActiveProducer = null;
requiredNumReports = 1;
this._reports = [];
}
const reports = this._reports;
// Insert the loudest audio level report into the reports array.
// NOTE: This may be undefined, meaning that nobody is producing audio.
latest.push(levels[0]);
reports.push(levels[0]);
// No more than REQUIRED_MAX_NUM_REPORTS.
if (reports.length > REQUIRED_MAX_NUM_REPORTS)
{
reports.shift();
}
// No less than requiredNumReports.
else if (reports.length < requiredNumReports)
{
this._mayUpdateActiveSpeaker(previousActiveProducer);
// No more than times.
if (latest.length > times)
latest.shift();
// No less than times.
else if (latest.length < times)
return;
}
// If the same Producer was the loudest speaker in the last 4 reports, make
// If the same Producer was the loudest speaker in the last N reports, make
// it the active speaker.
let newActiveProducer;
let newAudioLevel;
const latestLen = latest.length;
let currentActiveProducer;
const numReports = reports.length;
for (let i = latestLen - 1; i >= latestLen - times; --i)
for (let i = numReports - 1; i >= numReports - requiredNumReports; --i)
{
const entry = latest[i];
const report = reports[i];
if (!entry || entry.audioLevel < MIN_AUDIO_LEVEL)
if (!report)
{
newActiveProducer = undefined;
currentActiveProducer = null;
break;
}
const { producer, audioLevel } = entry;
const { producer, audioLevel } = report;
if (producer.closed || producer.paused)
if (producer.closed || producer.paused || audioLevel < REQUIRED_MIN_AUDIO_LEVEL)
{
newActiveProducer = undefined;
currentActiveProducer = null;
break;
}
if (newActiveProducer && producer !== newActiveProducer)
if (currentActiveProducer && producer !== currentActiveProducer)
{
newActiveProducer = undefined;
currentActiveProducer = null;
break;
}
newActiveProducer = producer;
newAudioLevel = audioLevel;
currentActiveProducer = producer;
}
// If there is an effective new audio Producer, make it the active one.
if (newActiveProducer && newActiveProducer !== this._activeProducer)
if (currentActiveProducer)
{
this._activeProducer = newActiveProducer;
this._mayUpdateActiveSpeaker(currentActiveProducer);
}
// Otherwise check the previous active speaker.
else
{
this._mayUpdateActiveSpeaker(previousActiveProducer);
}
}
_mayUpdateActiveSpeaker(activeProducer)
{
if (this._activeProducer === activeProducer)
return;
this._activeProducer = activeProducer;
if (this._activeProducer)
{
const peer = this._activeProducer.peer;
logger.debug(
'emitting "activespeakerchange" [peerName:"%s"]', peer.name);
this.safeEmit(
'activespeakerchange', peer, this._activeProducer, newAudioLevel);
'activespeakerchange', peer, this._activeProducer);
}
// Otherwise, if the previous active speaker (if any) is closed or paused,
// unset it.
else if (
this._activeProducer &&
(this._activeProducer.closed || this._activeProducer.paused))
else
{
this._activeProducer = undefined;
logger.debug(
'emitting "activespeakerchange" [peerName:null]');
this.safeEmit('activespeakerchange', null, null);
this.safeEmit('activespeakerchange', undefined, undefined);
}
}
}
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论