forked from otalk/rtcpeerconnection-shim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtcicegatherer.js
43 lines (41 loc) · 1.48 KB
/
rtcicegatherer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* Copyright (c) 2018 rtcpeerconnection-shim authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree.
*/
'use strict';
/* a wrapper around Edge's RTCIceGatherer that adds state and onstatechange
* and hides a bug in getLocalCandidates which throws if called before
* there was at least one candidate gathered.
*/
module.exports = function(window) {
var NativeRTCIceGatherer = window.RTCIceGatherer;
Object.defineProperty(window.RTCIceGatherer.prototype, 'state',
{value: 'new', writable: true});
var RTCIceGatherer = function(options) {
var gatherer = new NativeRTCIceGatherer(options);
gatherer.addEventListener('localcandidate', function(e) {
var candidate = e.candidate;
var end = !candidate || Object.keys(candidate).length === 0;
if (end) {
gatherer.state = 'complete';
gatherer.dispatchEvent(new Event('statechange'));
} else if (gatherer.state === 'new') {
gatherer.state = 'gathering';
gatherer.dispatchEvent(new Event('statechange'));
}
});
return gatherer;
};
RTCIceGatherer.prototype = NativeRTCIceGatherer.prototype;
var origGetLocalCandidates = RTCIceGatherer.prototype.getLocalCandidates;
RTCIceGatherer.prototype.getLocalCandidates = function() {
if (this.state === 'new') {
return [];
}
return origGetLocalCandidates.apply(this);
};
return RTCIceGatherer;
};