forked from nterreri/rtcpeerconnection-shim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtcicetransport.js
99 lines (93 loc) · 3.2 KB
/
rtcicetransport.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* 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';
var util = require('./util');
/* a wrapper around Edge's RTCIceTransport that makes it compatible
* with WebRTC 1.0 which merges RTCIceGatherer and RTCIceTransport.
*
* Also hides name changes between Edge and the latest specification.
*/
module.exports = function(window) {
var prototype = window.RTCIceTransport.prototype;
// provide getLocalCandidates from gatherer, adding the transport component.
if (!('getLocalCandidates' in prototype)) {
prototype.getLocalCandidates = function() {
var transport = this;
if (this.iceGatherer) {
return this.iceGatherer.getLocalCandidates().map(function(cand) {
cand.component = transport.component;
return cand;
});
}
return [];
};
}
// provide getLocalParameters from gatherer.
if (!('getLocalParameters' in prototype)) {
prototype.getLocalParameters = function() {
if (this.iceGatherer) {
return this.iceGatherer.getLocalParameters();
}
throw util.makeError('InvalidStateError',
'Can not call getLocalParameters in this state');
};
}
// provide gatheringState and gatheringstatechange from gatherer.
if (!('gatheringState' in prototype)) {
Object.defineProperty(prototype, 'gatheringState', {
get: function() {
return this.iceGatherer ? this.iceGatherer.state : 'new';
}
});
Object.defineProperty(prototype, 'ongatheringstatechange', {
get: function() {
return this._ongatheringstatechange;
},
set: function(cb) {
// TODO: this may loose event subscribes when this.gatherer is null
// throw a JS error for now.
if (this._ongatheringstatechange) {
this.iceGatherer.removeEventListener('statechange',
this._ongatheringstatechange);
delete this._ongatheringstatechange;
}
if (cb) {
this.iceGatherer.addEventListener('statechange',
this._ongatheringstatechange = cb);
}
}
});
// implement addEventListener('gatheringstatechange', cb)
['addEventListener', 'removeEventListener'].forEach(function(method) {
var nativeMethod = prototype[method];
prototype[method] = function(eventName, cb) {
if (eventName === 'gatheringstatechange') {
if (this.iceGatherer) {
return this.iceGatherer[method].apply(this.iceGatherer,
['statechange', cb]);
}
}
return nativeMethod.apply(this, arguments);
};
});
}
// simple name aliases.
if (!('onstatechange' in prototype)) {
util.aliasEventListener(prototype,
'icestatechange', 'statechange');
}
if (!('getSelectedCandidatePair' in prototype)) {
prototype.getSelectedCandidatePair =
prototype.getSelectedCandidatePair = function() {
return this.getNominatedCandidatePair();
};
util.aliasEventListener(prototype,
'candidatepairchange', 'selectedcandidatepairchange');
}
return window.RTCIceTransport;
};