This repository has been archived by the owner on Apr 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Browser-Recording-Helper.js
executable file
·206 lines (162 loc) · 5.82 KB
/
Browser-Recording-Helper.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
// Muaz Khan - www.MuazKhan.com
// MIT License - www.webrtc-experiment.com/licence
// Documentation - github.com/streamproc/MediaStreamRecorder
var RecorderHelper = (function() {
var socket; // socket.io
var roomId;
var userId;
var UploadInterval = 5 * 1000;
var mediaStreamRecorder;
function initRecorder(mediaStream, video) {
mediaStream.addEventListener('ended', function() {
// RecorderHelper.StopRecording();
}, false);
mediaStreamRecorder = new MediaStreamRecorder(mediaStream);
mediaStreamRecorder.mimeType = 'video/webm';
mediaStreamRecorder.ondataavailable = function(blobs) {
onDataAvailable(blobs);
};
mediaStreamRecorder.start(UploadInterval);
socket.on('complete', function(fileName) {
RecorderHelper.OnComplete(fileName);
});
socket.on('ffmpeg-progress', function(response) {
RecorderHelper.OnProgress(response);
});
}
window.addEventListener('beforeunload', function(event) {
if (mediaStreamRecorder) {
mediaStreamRecorder.stop();
mediaStreamRecorder = null;
}
if (Object.keys(socketPendingMessages).length) {
event.returnValue = 'Still some recording intervals are pending.';
}
}, false);
function onDataAvailable(blob) {
getDataURL(blob, function(dataURL) {
var data = {
blob: blob,
dataURL: dataURL
};
postFiles(data);
});
}
var fileNameString;
var index = 1;
function postFiles(data) {
var interval = index;
fileName = fileNameString + '-' + index;
index++;
var files = {
interval: interval,
roomId: roomId || generatefileNameString(),
userId: userId || generatefileNameString(),
fileName: fileNameString
};
files.data = {
name: fileName + '.' + data.blob.type.split('/')[1],
type: data.blob.type,
contents: data.dataURL,
interval: interval
};
if (isSocketBusy) {
socketPendingMessages[interval] = {
files: files,
emit: function() {
isSocketBusy = true;
console.info('emitting', interval);
socket.emit('recording-message', JSON.stringify(files), function() {
isSocketBusy = false;
if (socketPendingMessages[interval + 1]) {
socketPendingMessages[interval + 1].emit();
delete socketPendingMessages[interval + 1];
} else if(!mediaStreamRecorder) {
socket.emit('stream-stopped');
}
});
}
};
return;
}
isSocketBusy = true;
console.info('emitting', interval);
socket.emit('recording-message', JSON.stringify(files), function() {
isSocketBusy = false;
console.info('emitting', interval);
if (socketPendingMessages[interval + 1]) {
socketPendingMessages[interval + 1].emit();
delete socketPendingMessages[interval + 1];
} else if(!mediaStreamRecorder) {
socket.emit('stream-stopped');
}
});
}
var isSocketBusy = false;
var socketPendingMessages = {};
function generatefileNameString() {
if (window.crypto) {
var a = window.crypto.getRandomValues(new Uint32Array(3)),
token = '';
for (var i = 0, l = a.length; i < l; i++) token += a[i].toString(36);
return token;
} else {
return (Math.random() * new Date().getTime()).toString(36).replace(/\./g, '');
}
}
function getDataURL(blob, callback) {
if (!!window.Worker) {
var webWorker = processInWebWorker(function readFile(_blob) {
postMessage(new FileReaderSync().readAsDataURL(_blob));
});
webWorker.onmessage = function(event) {
callback(event.data);
};
webWorker.postMessage(blob);
} else {
var reader = new FileReader();
reader.readAsDataURL(blob);
reader.onload = function(event) {
callback(event.target.result);
};
}
}
var worker;
function processInWebWorker(_function) {
if (worker) {
return worker;
}
var blob = URL.createObjectURL(new Blob([_function.toString(),
'this.onmessage = function (e) {' + _function.name + '(e.data);}'
], {
type: 'application/javascript'
}));
worker = new Worker(blob);
URL.revokeObjectURL(blob);
return worker;
}
return {
StartRecording: function(obj) {
index = 1;
fileNameString = obj.FileName || generatefileNameString();
roomId = obj.roomId;
userId = obj.userId;
UploadInterval = obj.UploadInterval;
socket = obj.Socket;
initRecorder(obj.MediaStream, obj.HTMLVideoElement);
this.alreadyStopped = false;
},
StopRecording: function() {
if (this.alreadyStopped) return;
this.alreadyStopped = true;
mediaStreamRecorder.stop();
mediaStreamRecorder = null;
},
OnComplete: function(fileName) {
console.debug('File saved at: /uploads/' + roomId + '/' + fileName);
},
OnProgress: function(response) {
console.info('ffmpeg progress', response.progress, response);
}
};
})();