提交 27acd105 authored 作者: José Luis Millán's avatar José Luis Millán

Enable/Disable periodic stats retrieval for Transport, Producer and Consumer

上级 df553ad3
......@@ -52,6 +52,14 @@ class Consumer extends EnhancedEventEmitter
// @type {Boolean}
this._sourcePaused = false;
// Periodic stats flag.
// @type {Boolean}
this._statsEnabled = false;
// Periodic stats interval identifier.
// @type {Boolean}
this._statsInterval = null;
// Preferred profile.
// @type {String}
this._preferredProfile = 'default';
......@@ -189,6 +197,12 @@ class Consumer extends EnhancedEventEmitter
this._closed = true;
if (this._statsEnabled)
{
this._statsEnabled = false;
clearInterval(this._statsInterval);
}
// Notify if it has a Peer.
if (this.hasPeer())
{
......@@ -418,6 +432,76 @@ class Consumer extends EnhancedEventEmitter
return this.paused;
}
/**
* Enables periodic stats retrieval.
*
*/
enableStats()
{
logger.debug('enableStats()');
if (this._closed)
{
logger.error('enableStats() | Consumer closed');
return;
}
if (this._statsEnabled)
{
return;
}
this._statsEnabled = true;
this._statsInterval = setInterval(() =>
{
if (this._paused || !this.enabled)
return;
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));
});
}, 1000);
}
/**
* Disables periodic stats retrieval.
*
*/
disableStats()
{
logger.debug('disableStats()');
if (this._closed)
{
logger.error('disableStats() | Consumer closed');
return;
}
if (!this._statsEnabled)
{
return;
}
this._statsEnabled = false;
clearInterval(this._statsInterval);
}
/**
* The remote Consumer was paused.
* Invoked via remote notification.
......
......@@ -505,6 +505,32 @@ class Peer extends EnhancedEventEmitter
break;
}
case 'enableTransportStats':
{
const { id, appData } = notification;
const transport = this._transports.get(id);
if (!transport)
throw new Error('Transport not found');
transport.enableStats(appData);
break;
}
case 'disableTransportStats':
{
const { id, appData } = notification;
const transport = this._transports.get(id);
if (!transport)
throw new Error('Transport not found');
transport.disableStats(appData);
break;
}
case 'updateProducer':
{
const { id, rtpParameters } = notification;
......@@ -558,6 +584,32 @@ class Peer extends EnhancedEventEmitter
break;
}
case 'enableProducerStats':
{
const { id, appData } = notification;
const producer = this._producers.get(id);
if (!producer)
throw new Error('Producer not found');
producer.enableStats(appData);
break;
}
case 'disableProducerStats':
{
const { id, appData } = notification;
const producer = this._producers.get(id);
if (!producer)
throw new Error('Producer not found');
producer.disableStats(appData);
break;
}
case 'pauseConsumer':
{
const { id, appData } = notification;
......@@ -597,6 +649,32 @@ class Peer extends EnhancedEventEmitter
break;
}
case 'enableConsumerStats':
{
const { id, appData } = notification;
const consumer = this._consumers.get(id);
if (!consumer)
throw new Error('Consumer not found');
consumer.enableStats(appData);
break;
}
case 'disableConsumerStats':
{
const { id, appData } = notification;
const consumer = this._consumers.get(id);
if (!consumer)
throw new Error('Consumer not found');
consumer.disableStats(appData);
break;
}
default:
throw new Error(`unknown notification method "${method}"`);
}
......
......@@ -115,6 +115,26 @@ class PlainRtpTransport extends EnhancedEventEmitter
});
}
/**
* Enables periodic stats retrieval.
*
* Not implemented.
*
*/
enableStats()
{
}
/**
* Disables periodic stats retrieval.
*
* Not implemented.
*
*/
disableStats()
{
}
_handleWorkerNotifications()
{
// Subscribe to notifications.
......
......@@ -47,6 +47,14 @@ class Producer extends EnhancedEventEmitter
// @type {Boolean}
this._remotelyPaused = Boolean(options.remotelyPaused);
// Periodic stats flag.
// @type {Boolean}
this._statsEnabled = false;
// Periodic stats interval identifier.
// @type {Boolean}
this._statsInterval = null;
// Preferred profile.
// @type {String}
this._preferredProfile = 'default';
......@@ -156,6 +164,12 @@ class Producer extends EnhancedEventEmitter
this._closed = true;
if (this._statsEnabled)
{
this._statsEnabled = false;
clearInterval(this._statsInterval);
}
// If our Transport is still alive, it means that the app really wants to close
// the client-side Producer. Otherwise, if the Transport is closed the client-side
// Producer will get 'unhandled', so don't close it from here.
......@@ -323,6 +337,69 @@ class Producer extends EnhancedEventEmitter
return this.paused;
}
/**
* Enables periodic stats retrieval.
*
*/
enableStats()
{
logger.debug('enableStats()');
if (this._closed)
{
logger.error('enableStats() | Producer closed');
return;
}
if (this._statsEnabled)
{
return;
}
this._statsEnabled = true;
this._statsInterval = setInterval(() =>
{
if (this._paused || !this.enabled)
return;
this.getStats()
.then((data) =>
{
this.emit('@notify', 'producerStats', data);
})
.catch((error) =>
{
logger.error('"getStats" failed: %s', String(error));
});
}, 1000);
}
/**
* Disables periodic stats retrieval.
*
*/
disableStats()
{
logger.debug('disableStats()');
if (this._closed)
{
logger.error('disableStats() | Producer closed');
return;
}
if (!this._statsEnabled)
{
return;
}
this._statsEnabled = false;
clearInterval(this._statsInterval);
}
/**
* The remote Producer was paused.
* Invoked via remote notification.
......
......@@ -65,6 +65,14 @@ class WebRtcTransport extends EnhancedEventEmitter
// App data.
this._appData = undefined;
// Periodic stats flag.
// @type {Boolean}
this._statsEnabled = false;
// Periodic stats interval identifier.
// @type {Boolean}
this._statsInterval = null;
this._handleWorkerNotifications();
}
......@@ -148,6 +156,12 @@ class WebRtcTransport extends EnhancedEventEmitter
this._closed = true;
if (this._statsEnabled)
{
this._statsEnabled = false;
clearInterval(this._statsInterval);
}
this.emit('@notify', 'transportClosed', { id: this.id, appData });
this.emit('@close');
......@@ -156,6 +170,66 @@ class WebRtcTransport extends EnhancedEventEmitter
this._destroy(notifyChannel);
}
/**
* Enables periodic stats retrieval.
*
*/
enableStats()
{
logger.debug('enableStats()');
if (this._closed)
{
logger.error('enableStats() | WebRtcTransport closed');
return;
}
if (this._statsEnabled)
{
return;
}
this._statsEnabled = true;
this._statsInterval = setInterval(() =>
{
this.getStats()
.then((data) =>
{
this.emit('@notify', 'transportStats', data);
})
.catch((error) =>
{
logger.error('"getStats" request failed: %s', String(error));
});
}, 1000);
}
/**
* Disables periodic stats retrieval.
*
*/
disableStats()
{
logger.debug('disableStats()');
if (this._closed)
{
logger.error('disableStats() | WebRtcTransport closed');
return;
}
if (!this._statsEnabled)
{
return;
}
this._statsEnabled = false;
clearInterval(this._statsInterval);
}
/**
* Remote WebRtcTransport was closed.
* Invoked via remote notification.
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论