-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
252 lines (224 loc) · 7.16 KB
/
index.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* eslint-disable */
const ipcRenderer = require('electron').ipcRenderer;
const ipcMain = require('electron').ipcMain;
const remote = require('electron').remote;
const events = require('events');
/**
* The main process peer connection interface
*/
let main = {
/**
* Register a client to the peer connection channel.
* @param {string} client - client format: {name: "name", window: windowObj}
*/
addClient: function (client) {
if (!global.clients) {
global.clients = [];
}
global.clients.push(client);
},
/**
* Remove a client from the peer connection channel.
* @param {string} clientName - name of the client window
*/
removeClient: function (targetClientName) {
if (global.clients) {
global.clients = global.clients.filter(
client => client.name !== targetClientName
);
}
},
/**
* Sets the ipc listeners for messages from renderer processes
*/
initChannel: function () {
ipcMain.on('log', logMessage);
ipcMain.on('relay', relayMessage);
},
/**
* Disposes message relay channel
*/
dispose: function () {
ipcMain.removeListener('log', logMessage);
ipcMain.removeListener('relay', relayMessage);
global.clients = null;
}
};
function logMessage(event, message) {
// console.log(message);
}
function relayMessage(event, args) {
const receiverName = args[1];
const message = args[2];
const targetClient = global.clients.find(eachClient => eachClient.name === receiverName);
if (targetClient) {
const targetWindow = targetClient.window;
targetWindow.webContents.send(message, args);
}
}
/**
* Log in main process terminal
*/
function log(message) {
ipcRenderer.send('log', message);
}
/**
* Wrapper class for RTCPeerConnection between Electron windows
* @param {string} windowName - name of the BrowserWindow containing the object
*/
function WindowPeerConnection (windowName) {
this.peerConnection = new webkitRTCPeerConnection(null);
this.windowName = windowName;
this.remoteStream = null;
let thisObj = this;
let clients = remote.getGlobal('clients');
events.EventEmitter.call(this);
log(thisObj.windowName + ": peer connection object");
/**
* RTCPeerConnection event handlers
*/
ipcRenderer.on('offer', (event, args) => {
const senderName = args[0];
const data = args[3];
const offer = JSON.parse(data);
handleOffer(offer, senderName);
});
ipcRenderer.on('answer', (event, args) => {
const data = args[3];
const answer = JSON.parse(data);
handleAnswer(answer);
});
ipcRenderer.on('candidate', (event, args) => {
const data = args[3];
const candidate = JSON.parse(data);
handleCandidate(candidate);
});
ipcRenderer.on('end', () => {
handleLeave();
});
/**
* Sends message from main window to mircro window.
*/
function sendMessage(receiverName, message, data) {
let args = [];
args[0] = thisObj.windowName;
args[1] = receiverName;
args[2] = message;
args[3] = JSON.stringify(data);
ipcRenderer.send('relay', args);
}
/**
* Attaches MediaStream object to send to peers.
*/
this.attachStream = function (stream) {
thisObj.remoteStream = stream;
thisObj.peerConnection.addStream(stream);
};
/**
* Removes MediaStream object attached previously.
*/
this.removeStream = function () {
thisObj.peerConnection.removeStream(thisObj.remoteStream);
};
/**
* On received remote MediaStream, dispatch an event.
*/
this.peerConnection.onaddstream = function (event) {
thisObj.emit('receivedStream', event.stream);
};
/**
* Wrapper for receivedStream event.
*/
this.onReceivedStream = function (callback) {
return thisObj.on('receivedStream', callback);
};
/**
* Once ice candidate created, sends to all clients registered.
*/
this.peerConnection.onicecandidate = function(event) {
log(thisObj.windowName + ": iceCandidate created");
if(event.candidate !== null) {
const newIceCandidate = event.candidate;
clients.forEach(
function(client) {
if ( client.name !== thisObj.windowName ) {
sendMessage(client.name, 'candidate', newIceCandidate);
}
}
);
}
};
/**
* Ice candidate connection state change event.
*/
this.peerConnection.oniceconnectionstatechange = function(event) {
if (thisObj.peerConnection) {
log(thisObj.windowName + ": iceCandidateState change event: " + event.type);
}
};
/**
* Sends the local MediaStream to a registered peer.
* @param {string} receiverName - name of the receiving BrowserWindow
*/
this.sendStream = function (receiverName) {
log(thisObj.windowName + ": createOffer start");
const offerOptions = {
offerToReceiveVideo: 1
};
thisObj.peerConnection.createOffer(function (offer) {
sendMessage(
receiverName,
'offer',
offer
);
thisObj.peerConnection.setLocalDescription(offer);
}, function(error) {
log(thisObj.windowName + ": Error when creating an offer " + error);
},
offerOptions
);
};
/**
* Sends an offer to the target peer.
*/
function handleOffer(offer, senderName) {
log(thisObj.windowName + ": Setting remoteDescription");
thisObj.peerConnection.setRemoteDescription(new RTCSessionDescription(offer));
log(thisObj.windowName + ": remoteDescription set");
//create an answer to an offer
thisObj.peerConnection.createAnswer(function (answer) {
log(thisObj.windowName + ": Creating answer");
thisObj.peerConnection.setLocalDescription(answer);
sendMessage(
senderName,
'answer',
answer
);
}, function (error) {
alert(thisObj.windowName + ": Error when creating an answer " + error);
});
}
/**
* Sends an answer to the target peer.
*/
function handleAnswer(answer) {
thisObj.peerConnection.setRemoteDescription(new RTCSessionDescription(answer));
}
/**
* Adds ice candidate received to the RTCPeerConnection object.
*/
function handleCandidate(candidate) {
thisObj.peerConnection.addIceCandidate(new RTCIceCandidate(candidate));
}
/**
* Close connection and nullify handlers.
*/
function handleLeave() {
thisObj.peerConection.close();
thisObj.peerConnection.onicecandidate = null;
thisObj.peerConnection.onaddstream = null;
}
}
WindowPeerConnection.prototype.__proto__ = events.EventEmitter.prototype;
module.exports.main = main;
module.exports.WindowPeerConnection = WindowPeerConnection;