rtc-tools
Version:
Cross-browser WebRTC helpers
69 lines (55 loc) • 1.48 kB
JavaScript
/* jshint node: true */
;
var debug = require('cog/logger')('rtc/cleanup');
var CANNOT_CLOSE_STATES = [
'closed'
];
var EVENTS_DECOUPLE_BC = [
'addstream',
'datachannel',
'icecandidate',
'negotiationneeded',
'removestream',
'signalingstatechange'
];
var EVENTS_DECOUPLE_AC = [
'iceconnectionstatechange'
];
/**
### rtc-tools/cleanup
```
cleanup(pc)
```
The `cleanup` function is used to ensure that a peer connection is properly
closed and ready to be cleaned up by the browser.
**/
module.exports = function(pc) {
if (!pc) return;
// see if we can close the connection
var currentState = pc.iceConnectionState;
var currentSignaling = pc.signalingState;
var canClose = CANNOT_CLOSE_STATES.indexOf(currentState) < 0 && CANNOT_CLOSE_STATES.indexOf(currentSignaling) < 0;
function decouple(events) {
events.forEach(function(evtName) {
if (pc['on' + evtName]) {
pc['on' + evtName] = null;
}
});
}
// decouple "before close" events
decouple(EVENTS_DECOUPLE_BC);
if (canClose) {
debug('attempting connection close, current state: '+ pc.iceConnectionState);
try {
pc.close();
} catch (e) {
console.warn('Could not close connection', e);
}
}
// remove the event listeners
// after a short delay giving the connection time to trigger
// close and iceconnectionstatechange events
setTimeout(function() {
decouple(EVENTS_DECOUPLE_AC);
}, 100);
};