-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
245 lines (197 loc) · 6 KB
/
index.js
File metadata and controls
245 lines (197 loc) · 6 KB
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
import MRTC from 'mrtc';
module.exports = class MultiRTC {
constructor(options={}) {
this.CHUNK_SIZE = 1024 * 4;
this.blobs = {};
this.buffers = {};
this.events = [];
this.peers = {};
// Shortcuts
options.dataChannel = options.channel || options.dataChannel;
this.options = options;
if (options.wrtc) {
this.File = MultiRTC;
this.atob = options.atob;
this.btoa = options.btoa;
return;
}
this.File = window.File;
this.atob = window.atob.bind(window);
this.btoa = window.btoa.bind(window);
}
/*
* Private API
*/
_sendChunk(chunk, blobId, id, size) {
let message = {
type: '__multi-rtc-blob-chunk__',
id: blobId,
content: this.btoa(chunk),
};
if (size) {
message.size = size;
}
this.send(message, id);
}
_sendBlob(blob, id, offset) {
let chunk = blob.content.slice(offset, offset + this.CHUNK_SIZE);
this._sendChunk(chunk, blob.id, id, offset === 0 ? blob.content.length : false);
}
_sendFile(blob, id, offset) {
let reader = new FileReader();
reader.onload = () => {
this._sendChunk(reader.result, blob.id, id, offset === 0 ? blob.content.size : false);
};
let chunk = blob.content.slice(offset, offset + this.CHUNK_SIZE);
reader.readAsBinaryString(chunk);
}
/*
* Public API
*/
add(id, signal=null) {
if (!this.peers[id]) {
// When we have no instance from the given peer, add him/her.
this.peers[id] = new MRTC({offerer: signal === null, ...this.options});
// Base
this.peers[id].on('signal', this.onSignal.bind(this, id));
// MediaStream
this.peers[id].on('add-stream', this.onAddStream.bind(this, id));
// DataChannel
this.peers[id].on('channel-open', this.onOpen.bind(this, id));
this.peers[id].on('channel-message', this.onData.bind(this, id));
this.peers[id].on('channel-close', this.onClose.bind(this, id));
this.peers[id].on('channel-error', this.onError.bind(this, id));
this.peers[id].on('channel-buffered-amount-low', this.onBufferedAmountLow.bind(this, id));
// Monkey Patch a smoothier send method into MRTC
this.peers[id].send = data => {
let channel = this.peers[id].channel;
if (channel && channel.readyState === 'open') {
channel.send(data);
}
};
}
// If add function provides a signal, accept it
if (signal) {
this.peers[id].addSignal(signal);
}
}
send(data, id) {
// Define ids to deliver this data
let ids = id ? [id] : Object.keys(this.peers);
data = JSON.stringify(data);
ids.forEach(function(key) {
this.peers[key].send(data);
}, this);
}
requireBlob(peerId, blobId, offset=0) {
return this.send({
type: '__multi-rtc-blob-chunk__',
offset: offset,
id: blobId,
}, peerId);
}
addBlob(data) {
// The unique id of this given Blob
let blobId = Math.random().toString(32).split('.')[1];
this.blobs[blobId] = {id: blobId};
// if sending a File, use the FileReader API
if (data instanceof (this.File || MultiRTC)) {
this.blobs[blobId].content = data;
this.blobs[blobId].send = (key, offset) => {
this._sendFile(this.blobs[blobId], key, offset);
};
return this.blobs[blobId];
}
// if sending anything other than a string, stringify it first
if (typeof data !== 'string') {
data = JSON.stringify(data);
}
this.blobs[blobId].content = data;
this.blobs[blobId].send = (key, offset) => {
this._sendBlob(this.blobs[blobId], key, offset);
};
return this.blobs[blobId];
}
sendBlob(data, id) {
let blob = this.addBlob(data);
// Define ids to deliver this data
let ids = id ? [id] : Object.keys(this.peers);
return ids.forEach(function(key) {
blob.send(key, 0);
}, this);
}
/*
* MRTC Hooks
*/
onSignal(id, signal) {
this.trigger('signal', [id, signal]);
}
onAddStream(id, stream) {
this.trigger('stream', [id, stream]);
}
onOpen(id) {
this.trigger('connect', [id]);
}
onData(id, event) {
let data = JSON.parse(event.data);
// Common case, data is returned to the user directly
if (data.type !== '__multi-rtc-blob-chunk__') {
return this.trigger('data', [id, data]);
} else if (data.offset !== undefined) {
return this.blobs[data.id].send(id, data.offset);
}
let buffer = this.buffers[data.id];
if (!buffer) {
buffer = this.buffers[data.id] = {
id: data.id,
size: data.size,
content: '',
};
}
let content = this.atob(data.content || '');
buffer.content += content;
this.trigger('partial-blob', [id, buffer, content]);
// If there's more content to be brought, request it
if (buffer.size > buffer.content.length) {
return this.requireBlob(id, data.id, buffer.content.length);
}
// Else trigger the 'data' event to the user with his blob.
this.trigger('data', [id, buffer]);
delete this.buffers[data.id];
}
onClose(id) {
delete this.peers[id];
this.trigger('disconnect', [id]);
}
onError(...args) {
this.trigger('error', args);
}
onBufferedAmountLow(...args) {
this.trigger('buffered-amount-low', args);
}
/*
* Event System
*/
on(action, callback) {
this.events[action] = this.events[action] || [];
this.events[action].push(callback);
}
off(action, callback) {
this.events[action] = this.events[action] || [];
if (callback) {
// If a callback has been specified delete it specifically
var index = this.events[action].indexOf(callback);
(index !== -1) && this.events[action].splice(index, 1);
return index !== -1;
}
// Else just erase all callbacks
this.events[action] = [];
}
trigger(action, args=[]) {
this.events[action] = this.events[action] || [];
// Fire all events with the given callback
this.events[action].forEach(function(callback) {
callback.apply(null, args);
});
}
};