提交 862b7be7 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Make enableStats() immediately produce stats

上级 587cdfc2
......@@ -5,6 +5,7 @@ const EnhancedEventEmitter = require('./EnhancedEventEmitter');
const errors = require('./errors');
const PROFILES = new Set([ 'default', 'low', 'medium', 'high' ]);
const DEFAULT_STATS_INTERVAL = 1;
const logger = new Logger('Consumer');
......@@ -700,12 +701,10 @@ class Consumer extends EnhancedEventEmitter
/**
* Enables periodic stats retrieval.
*/
enableStats(options)
enableStats(interval = DEFAULT_STATS_INTERVAL)
{
logger.debug('enableStats()');
const { interval } = options;
if (this._closed)
{
logger.error('enableStats() | Consumer closed');
......@@ -713,14 +712,42 @@ class Consumer extends EnhancedEventEmitter
return;
}
if (!this.hasPeer())
{
logger.error(
'enableStats() | cannot enable stats on a Consumer without Peer');
return;
}
if (this._statsEnabled)
return;
this._statsEnabled = true;
if (this.enabled && !this.paused)
{
this.getStats()
.then((stats) =>
{
const data =
{
id : this.id,
peerName : this.peer.name,
stats
};
this.emit('@notify', 'consumerStats', data);
})
.catch((error) =>
{
logger.error('"getStats" request failed: %s', String(error));
});
}
this._statsInterval = setInterval(() =>
{
if (this.paused || !this.enabled)
if (!this.enabled || this.paused)
return;
this.getStats()
......
......@@ -509,26 +509,26 @@ class Peer extends EnhancedEventEmitter
case 'enableTransportStats':
{
const { id, appData } = notification;
const { id, interval } = notification;
const transport = this._transports.get(id);
if (!transport)
throw new Error('Transport not found');
transport.enableStats(appData);
transport.enableStats(interval);
break;
}
case 'disableTransportStats':
{
const { id, appData } = notification;
const { id } = notification;
const transport = this._transports.get(id);
if (!transport)
throw new Error('Transport not found');
transport.disableStats(appData);
transport.disableStats();
break;
}
......@@ -588,26 +588,26 @@ class Peer extends EnhancedEventEmitter
case 'enableProducerStats':
{
const { id, appData } = notification;
const { id, interval } = notification;
const producer = this._producers.get(id);
if (!producer)
throw new Error('Producer not found');
producer.enableStats(appData);
producer.enableStats(interval);
break;
}
case 'disableProducerStats':
{
const { id, appData } = notification;
const { id } = notification;
const producer = this._producers.get(id);
if (!producer)
throw new Error('Producer not found');
producer.disableStats(appData);
producer.disableStats();
break;
}
......@@ -653,26 +653,26 @@ class Peer extends EnhancedEventEmitter
case 'enableConsumerStats':
{
const { id, appData } = notification;
const { id, interval } = notification;
const consumer = this._consumers.get(id);
if (!consumer)
throw new Error('Consumer not found');
consumer.enableStats(appData);
consumer.enableStats(interval);
break;
}
case 'disableConsumerStats':
{
const { id, appData } = notification;
const { id } = notification;
const consumer = this._consumers.get(id);
if (!consumer)
throw new Error('Consumer not found');
consumer.disableStats(appData);
consumer.disableStats();
break;
}
......
......@@ -5,6 +5,7 @@ const EnhancedEventEmitter = require('./EnhancedEventEmitter');
const errors = require('./errors');
const PROFILES = new Set([ 'default', 'low', 'medium', 'high' ]);
const DEFAULT_STATS_INTERVAL = 1;
const logger = new Logger('Producer');
......@@ -531,12 +532,10 @@ class Producer extends EnhancedEventEmitter
/**
* Enables periodic stats retrieval.
*/
enableStats(options)
enableStats(interval = DEFAULT_STATS_INTERVAL)
{
logger.debug('enableStats()');
const { interval } = options;
if (this._closed)
{
logger.error('enableStats() | Producer closed');
......@@ -549,6 +548,19 @@ class Producer extends EnhancedEventEmitter
this._statsEnabled = true;
if (!this.paused)
{
this.getStats()
.then((stats) =>
{
this.emit('@notify', 'producerStats', { id: this.id, stats });
})
.catch((error) =>
{
logger.error('"getStats" failed: %s', String(error));
});
}
this._statsInterval = setInterval(() =>
{
if (this.paused)
......
......@@ -4,6 +4,8 @@ const Logger = require('./Logger');
const EnhancedEventEmitter = require('./EnhancedEventEmitter');
const errors = require('./errors');
const DEFAULT_STATS_INTERVAL = 1;
const logger = new Logger('WebRtcTransport');
class WebRtcTransport extends EnhancedEventEmitter
......@@ -381,12 +383,10 @@ class WebRtcTransport extends EnhancedEventEmitter
/**
* Enables periodic stats retrieval.
*/
enableStats(options)
enableStats(interval = DEFAULT_STATS_INTERVAL)
{
logger.debug('enableStats()');
const { interval } = options;
if (this._closed)
{
logger.error('enableStats() | WebRtcTransport closed');
......@@ -399,6 +399,16 @@ class WebRtcTransport extends EnhancedEventEmitter
this._statsEnabled = true;
this.getStats()
.then((stats) =>
{
this.emit('@notify', 'transportStats', { id: this.id, stats });
})
.catch((error) =>
{
logger.error('"getStats" request failed: %s', String(error));
});
this._statsInterval = setInterval(() =>
{
this.getStats()
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论