623 lines
11 KiB
JavaScript
623 lines
11 KiB
JavaScript
const Logger = require('./Logger');
|
|
const EnhancedEventEmitter = require('./EnhancedEventEmitter');
|
|
const errors = require('./errors');
|
|
|
|
const PROFILES = new Set([ 'default', 'low', 'medium', 'high' ]);
|
|
const DEFAULT_STATS_INTERVAL = 1000;
|
|
|
|
const logger = new Logger('Producer');
|
|
|
|
class Producer extends EnhancedEventEmitter
|
|
{
|
|
constructor(internal, data, channel, options)
|
|
{
|
|
super(logger);
|
|
|
|
logger.debug('constructor()');
|
|
|
|
// Closed flag.
|
|
this._closed = false;
|
|
|
|
// Internal data.
|
|
// - .routerId
|
|
// - .producerId
|
|
// - .transportId
|
|
this._internal = internal;
|
|
|
|
// Producer data.
|
|
// - .kind
|
|
// - .peer
|
|
// - .transport
|
|
// - .rtpParameters
|
|
// - .consumableRtpParameters
|
|
this._data = data;
|
|
|
|
// Channel instance.
|
|
this._channel = channel;
|
|
|
|
// App data.
|
|
this._appData = undefined;
|
|
|
|
// Locally paused flag.
|
|
// @type {Boolean}
|
|
this._locallyPaused = false;
|
|
|
|
// Remotely paused flag.
|
|
// @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';
|
|
|
|
this._handleTransportEvents();
|
|
|
|
this._handleWorkerNotifications();
|
|
}
|
|
|
|
get id()
|
|
{
|
|
return this._internal.producerId;
|
|
}
|
|
|
|
get closed()
|
|
{
|
|
return this._closed;
|
|
}
|
|
|
|
get appData()
|
|
{
|
|
return this._appData;
|
|
}
|
|
|
|
set appData(appData)
|
|
{
|
|
this._appData = appData;
|
|
}
|
|
|
|
get kind()
|
|
{
|
|
return this._data.kind;
|
|
}
|
|
|
|
get peer()
|
|
{
|
|
return this._data.peer;
|
|
}
|
|
|
|
get transport()
|
|
{
|
|
return this._data.transport;
|
|
}
|
|
|
|
get rtpParameters()
|
|
{
|
|
return this._data.rtpParameters;
|
|
}
|
|
|
|
get consumableRtpParameters()
|
|
{
|
|
return this._data.consumableRtpParameters;
|
|
}
|
|
|
|
/**
|
|
* Whether the Producer is locally paused.
|
|
*
|
|
* @return {Boolean}
|
|
*/
|
|
get locallyPaused()
|
|
{
|
|
return this._locallyPaused;
|
|
}
|
|
|
|
/**
|
|
* Whether the Producer is remotely paused.
|
|
*
|
|
* @return {Boolean}
|
|
*/
|
|
get remotelyPaused()
|
|
{
|
|
return this._remotelyPaused;
|
|
}
|
|
|
|
/**
|
|
* Whether the Producer is paused.
|
|
*
|
|
* @return {Boolean}
|
|
*/
|
|
get paused()
|
|
{
|
|
return this._locallyPaused || this._remotelyPaused;
|
|
}
|
|
|
|
/**
|
|
* The preferred profile.
|
|
*
|
|
* @type {String}
|
|
*/
|
|
get preferredProfile()
|
|
{
|
|
return this._preferredProfile;
|
|
}
|
|
|
|
/**
|
|
* Close the Producer.
|
|
*
|
|
* @param {Any} [appData] - App custom data.
|
|
* @param {Boolean} [notifyChannel=true] - Private.
|
|
*/
|
|
close(appData, notifyChannel = true)
|
|
{
|
|
logger.debug('close()');
|
|
|
|
if (this._closed)
|
|
return;
|
|
|
|
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.
|
|
if (!this.transport.closed)
|
|
{
|
|
const data =
|
|
{
|
|
id : this.id,
|
|
peerName : this.peer.name,
|
|
appData : appData
|
|
};
|
|
|
|
this.emit('@notify', 'producerClosed', data);
|
|
}
|
|
|
|
this.emit('@close');
|
|
this.safeEmit('close', 'local', appData);
|
|
|
|
this._destroy(notifyChannel);
|
|
}
|
|
|
|
/**
|
|
* The remote Producer was closed.
|
|
* Invoked via remote notification.
|
|
*
|
|
* @private
|
|
*
|
|
* @param {Any} [appData] - App custom data.
|
|
* @param {Boolean} [notifyChannel=true] - Private.
|
|
*/
|
|
remoteClose(appData, notifyChannel = true)
|
|
{
|
|
logger.debug('remoteClose()');
|
|
|
|
if (this.closed)
|
|
return;
|
|
|
|
this._closed = true;
|
|
|
|
if (this._statsEnabled)
|
|
{
|
|
this._statsEnabled = false;
|
|
clearInterval(this._statsInterval);
|
|
}
|
|
|
|
this.emit('@close');
|
|
this.safeEmit('close', 'remote', appData);
|
|
|
|
this._destroy(notifyChannel);
|
|
}
|
|
|
|
_destroy(notifyChannel = true)
|
|
{
|
|
// Remove notification subscriptions.
|
|
this._channel.removeAllListeners(this._internal.producerId);
|
|
|
|
if (notifyChannel)
|
|
{
|
|
this._channel.request('producer.close', this._internal)
|
|
.then(() =>
|
|
{
|
|
logger.debug('"producer.close" request succeeded');
|
|
})
|
|
.catch((error) =>
|
|
{
|
|
logger.error('"producer.close" request failed: %s', String(error));
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Dump the Producer.
|
|
*
|
|
* @private
|
|
*
|
|
* @return {Promise}
|
|
*/
|
|
dump()
|
|
{
|
|
logger.debug('dump()');
|
|
|
|
if (this._closed)
|
|
return Promise.reject(new errors.InvalidStateError('Producer closed'));
|
|
|
|
return this._channel.request('producer.dump', this._internal)
|
|
.then((data) =>
|
|
{
|
|
logger.debug('"producer.dump" request succeeded');
|
|
|
|
return data;
|
|
})
|
|
.catch((error) =>
|
|
{
|
|
logger.error('"producer.dump" request failed: %s', String(error));
|
|
|
|
throw error;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Pauses receiving media.
|
|
*
|
|
* @param {Any} [appData] - App custom data.
|
|
*
|
|
* @return {Boolean} true if paused.
|
|
*/
|
|
pause(appData)
|
|
{
|
|
logger.debug('pause()');
|
|
|
|
if (this._closed)
|
|
{
|
|
logger.error('pause() | Producer closed');
|
|
|
|
return false;
|
|
}
|
|
else if (this._locallyPaused)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
this._locallyPaused = true;
|
|
|
|
const data =
|
|
{
|
|
id : this.id,
|
|
peerName : this.peer.name,
|
|
appData : appData
|
|
};
|
|
|
|
this.emit('@notify', 'producerPaused', data);
|
|
|
|
this._channel.request('producer.pause', this._internal)
|
|
.then(() =>
|
|
{
|
|
logger.debug('"producer.pause" request succeeded');
|
|
})
|
|
.catch((error) =>
|
|
{
|
|
logger.error('"producer.pause" request failed: %s', String(error));
|
|
});
|
|
|
|
this.safeEmit('pause', 'local', appData);
|
|
|
|
// Return true if really paused.
|
|
return this.paused;
|
|
}
|
|
|
|
/**
|
|
* The remote Producer was paused.
|
|
* Invoked via remote notification.
|
|
*
|
|
* @private
|
|
*
|
|
* @param {Any} [appData] - App custom data.
|
|
*/
|
|
remotePause(appData)
|
|
{
|
|
logger.debug('remotePause()');
|
|
|
|
if (this._closed || this._remotelyPaused)
|
|
return;
|
|
|
|
this._remotelyPaused = true;
|
|
|
|
this._channel.request('producer.pause', this._internal)
|
|
.then(() =>
|
|
{
|
|
logger.debug('"producer.pause" request succeeded');
|
|
})
|
|
.catch((error) =>
|
|
{
|
|
logger.error('"producer.pause" request failed: %s', String(error));
|
|
});
|
|
|
|
this.safeEmit('pause', 'remote', appData);
|
|
}
|
|
|
|
/**
|
|
* Resumes receiving media.
|
|
*
|
|
* @param {Any} [appData] - App custom data.
|
|
*
|
|
* @return {Boolean} true if not paused.
|
|
*/
|
|
resume(appData)
|
|
{
|
|
logger.debug('resume()');
|
|
|
|
if (this._closed)
|
|
{
|
|
logger.error('resume() | Producer closed');
|
|
|
|
return false;
|
|
}
|
|
else if (!this._locallyPaused)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
this._locallyPaused = false;
|
|
|
|
const data =
|
|
{
|
|
id : this.id,
|
|
peerName : this.peer.name,
|
|
appData : appData
|
|
};
|
|
|
|
this.emit('@notify', 'producerResumed', data);
|
|
|
|
if (!this._remotelyPaused)
|
|
{
|
|
this._channel.request('producer.resume', this._internal)
|
|
.then(() =>
|
|
{
|
|
logger.debug('"producer.resume" request succeeded');
|
|
})
|
|
.catch((error) =>
|
|
{
|
|
logger.error('"producer.resume" request failed: %s', String(error));
|
|
});
|
|
}
|
|
|
|
this.safeEmit('resume', 'local', appData);
|
|
|
|
// Return true if not paused.
|
|
return !this.paused;
|
|
}
|
|
|
|
/**
|
|
* The remote Producer was resumed.
|
|
* Invoked via remote notification.
|
|
*
|
|
* @private
|
|
*
|
|
* @param {Any} [appData] - App custom data.
|
|
*/
|
|
remoteResume(appData)
|
|
{
|
|
logger.debug('remoteResume()');
|
|
|
|
if (this._closed || !this._remotelyPaused)
|
|
return;
|
|
|
|
this._remotelyPaused = false;
|
|
|
|
if (!this._locallyPaused)
|
|
{
|
|
this._channel.request('producer.resume', this._internal)
|
|
.then(() =>
|
|
{
|
|
logger.debug('"producer.resume" request succeeded');
|
|
})
|
|
.catch((error) =>
|
|
{
|
|
logger.error('"producer.resume" request failed: %s', String(error));
|
|
});
|
|
}
|
|
|
|
this.safeEmit('resume', 'remote', appData);
|
|
}
|
|
|
|
/**
|
|
* Sets the preferred RTP profile.
|
|
*
|
|
* @param {String} profile
|
|
*/
|
|
setPreferredProfile(profile)
|
|
{
|
|
logger.debug('setPreferredProfile() [profile:%s]', profile);
|
|
|
|
if (this._closed)
|
|
{
|
|
logger.error('setPreferredProfile() | Producer closed');
|
|
|
|
return;
|
|
}
|
|
else if (profile === this._preferredProfile)
|
|
{
|
|
return;
|
|
}
|
|
else if (!PROFILES.has(profile))
|
|
{
|
|
logger.error('setPreferredProfile() | invalid profile "%s"', profile);
|
|
|
|
return;
|
|
}
|
|
|
|
this._channel.request(
|
|
'producer.setPreferredProfile', this._internal, { profile })
|
|
.then(() =>
|
|
{
|
|
logger.debug('"producer.setPreferredProfile" request succeeded');
|
|
|
|
this._preferredProfile = profile;
|
|
})
|
|
.catch((error) =>
|
|
{
|
|
logger.error(
|
|
'"producer.setPreferredProfile" request failed: %s', String(error));
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get the Producer stats.
|
|
*
|
|
* @return {Promise}
|
|
*/
|
|
getStats()
|
|
{
|
|
logger.debug('getStats()');
|
|
|
|
if (this._closed)
|
|
return Promise.reject(new errors.InvalidStateError('Producer closed'));
|
|
|
|
return this._channel.request('producer.getStats', this._internal)
|
|
.then((data) =>
|
|
{
|
|
logger.debug('"producer.getStats" request succeeded');
|
|
|
|
return data;
|
|
})
|
|
.catch((error) =>
|
|
{
|
|
logger.error('"producer.getStats" request failed: %s', String(error));
|
|
|
|
throw error;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Enables periodic stats retrieval.
|
|
*
|
|
* @private
|
|
*/
|
|
enableStats(interval = DEFAULT_STATS_INTERVAL)
|
|
{
|
|
logger.debug('enableStats()');
|
|
|
|
if (typeof interval !== 'number')
|
|
{
|
|
logger.error('enableStats() | invalid interval provided');
|
|
|
|
return;
|
|
}
|
|
|
|
if (this._closed)
|
|
{
|
|
logger.error('enableStats() | Producer closed');
|
|
|
|
return;
|
|
}
|
|
|
|
if (this._statsEnabled)
|
|
return;
|
|
|
|
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));
|
|
});
|
|
}
|
|
|
|
// Set minimum interval to DEFAULT_STATS_INTERVAL.
|
|
if (interval < DEFAULT_STATS_INTERVAL)
|
|
interval = DEFAULT_STATS_INTERVAL;
|
|
|
|
this._statsInterval = setInterval(() =>
|
|
{
|
|
if (this.paused)
|
|
return;
|
|
|
|
this.getStats()
|
|
.then((stats) =>
|
|
{
|
|
this.emit('@notify', 'producerStats', { id: this.id, stats });
|
|
})
|
|
.catch((error) =>
|
|
{
|
|
logger.error('"getStats" failed: %s', String(error));
|
|
});
|
|
}, interval);
|
|
}
|
|
|
|
/**
|
|
* Disables periodic stats retrieval.
|
|
*
|
|
* @private
|
|
*/
|
|
disableStats()
|
|
{
|
|
logger.debug('disableStats()');
|
|
|
|
if (this._closed)
|
|
{
|
|
logger.error('disableStats() | Producer closed');
|
|
|
|
return;
|
|
}
|
|
|
|
if (!this._statsEnabled)
|
|
return;
|
|
|
|
this._statsEnabled = false;
|
|
clearInterval(this._statsInterval);
|
|
}
|
|
|
|
_handleTransportEvents()
|
|
{
|
|
// On closure, the worker Transport closes all its Producers and the client
|
|
// side gets producer.on('unhandled').
|
|
this.transport.on('@close', () =>
|
|
{
|
|
if (!this._closed)
|
|
this.close(undefined, false);
|
|
});
|
|
}
|
|
|
|
_handleWorkerNotifications()
|
|
{
|
|
// Subscribe to notifications.
|
|
this._channel.on(this._internal.producerId, (event) =>
|
|
{
|
|
switch (event)
|
|
{
|
|
case 'close':
|
|
{
|
|
this.close(undefined, false);
|
|
|
|
break;
|
|
}
|
|
|
|
default:
|
|
{
|
|
logger.error('ignoring unknown event "%s"', event);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
module.exports = Producer;
|