-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequestMedia.js
385 lines (316 loc) · 12.5 KB
/
requestMedia.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
var requestMedia = function(opts) {
this.defaults = {
feed_in: null,
feed_out: null,
request_video: true,
request_audio: true,
name: ['file_', (new Date() + '').slice(4, 28), '.mp4'].join(''),
required_audio: true,
required_video: true,
file_type: 'video/mp4',
onGetPermission: function () {},
onForgetPermission: function () {},
onDeniedPermission: function (err) {console.log(err.name + ': ' + err.message)},
onStartRecording: function () {},
onStopRecording: function () {},
onPictureTaken: function() {},
onDownload: function () {}
}
/* copy user options or use default values */
for (var i in this.defaults) {
this[i] = (opts[i] !== undefined) ? opts[i] : this.defaults[i]
}
this.data_array = [];
this.stream = null;
this.recorder = null;
this.blob = null;
this.browser = navigator.userAgent;
this.canvas = null;
}
requestMedia.prototype.getFeed_in = function() {
/*return the value of Feed_in.*/
return this.feed_in;
};
requestMedia.prototype.getFeed_out = function() {
/*return the value of Feed_out.*/
return this.feed_out;
};
requestMedia.prototype.getRequest_video = function() {
/*return the value of Request_video.*/
return this.request_video;
};
requestMedia.prototype.getRequest_audio = function() {
/*return the value of Request_audio.*/
return this.request_audio;
};
requestMedia.prototype.getName = function () {
/*return the value of name.*/
return this.name;
};
requestMedia.prototype.getFile_type = function () {
/*return the value of file_type.*/
return this.file_type;
}
requestMedia.prototype.getOnGetPermission = function () {
/*return the value of onGetPermission.*/
return this.onGetPermission;
};
requestMedia.prototype.getOnForgetPermission = function () {
/*return the value of onForgetPermission.*/
return this.onForgetPermission;
};
requestMedia.prototype.getOnDeniedPermission = function () {
/*return the value of onDeniedPermission.*/
return this.onDeniedPermission;
};
requestMedia.prototype.getOnStartRecording = function () {
/*return the value of onStartRecording.*/
return this.onStartRecording;
};
requestMedia.prototype.getOnStopRecording = function () {
/*return the value of onStopRecording.*/
return this.onStopRecording;
};
requestMedia.prototype.getOnPictureTaken = function () {
/*return the value of onPictureTaken.*/
return this.onPictureTaken;
};
requestMedia.prototype.getOnDownload = function () {
/*return the value of onDownload.*/
return this.onDownload;
};
requestMedia.prototype.getData_array = function() {
/*return the value of Data_array.*/
return this.data_array;
};
requestMedia.prototype.getStream = function() {
/*return the value of Stream.*/
return this.stream;
};
requestMedia.prototype.getRecorder = function() {
/*return the value of Recorder.*/
return this.recorder;
};
requestMedia.prototype.getBlob = function() {
/*return the value of Blob.*/
return this.blob;
};
requestMedia.prototype.getRequired_audio = function() {
/*return the value of Required_audio.*/
return this.required_audio;
};
requestMedia.prototype.getRequired_video = function() {
/*return the value of Required_video.*/
return this.required_video;
};
requestMedia.prototype.getCanvas = function(){
/*return value of Canvas.*/
return this.canvas
};
requestMedia.prototype.setCanvas = function(c){
/*set value in Canvas.*/
this.canvas = c;
};
requestMedia.prototype.forgetPermission = function() {
/*
Forget the permission given by the user to access the webcam and microphone
Optional:
Receive a function by parameter to execute after removing all permissions.
*/
if (this.stream == null) return;
for (var i = 0; i < this.stream.getTracks().length; i++) {
this.stream.getTracks()[i].stop();
}
if (this.onForgetPermission && typeof this.onForgetPermission === 'function'){
//Case we have received a function on parameter, we execute it after forgeting all permissions.
this.onForgetPermission();
}
}
requestMedia.prototype.requestPermission = function() {
/*
Request permission from the user to access the microphone and webcam.
Optional:
success: Receive a function to execute after getting permission.
error: Receive a function to execute in case of error. This function may hava a parameter to receive info on the error.
*/
var that = this;
navigator.mediaDevices.getUserMedia({video: that.request_video, audio: that.request_audio})
.then(function(stm) {
that.stream = stm;
if (that.stream.getVideoTracks().length == 0 && that.required_video) {
/*In case it's required the use of a video device*/
that.onDeniedPermission({name:"Required Video", message:"No video device found."});
that.forgetPermission();
} else if (that.stream.getAudioTracks().length == 0 && that.required_audio) {
/*In case it's required the use of a audio device*/
that.onDeniedPermission({name:"Required Audio", message:"No audio device found."});
that.forgetPermission();
} else {
if (that.feed_in) {
/*Case we have received feed_in, we display the webcam feed on the html element(<video>/<audio>) with id equal to feed_in.*/
that.feed_in.setAttribute('src', URL.createObjectURL(that.stream));
}
if (that.onGetPermission && typeof that.onGetPermission === 'function') {
/*Case we have received a function on parameter, we execute it after the user grants permission.*/
that.onGetPermission();
}
}
})
.then(null, that.onDeniedPermission && typeof that.onDeniedPermission === 'function' ? that.onDeniedPermission : function (err) {console.log(err.name + ': ' + err.message);})
}
requestMedia.prototype.startRecording = function() {
/*
Start recording the webcam feed.
The variable Browser is here to differentiate between Chrome and Firefox/Opera,
because the function mediaRecorder has a different behavior on Chrome.
*/
var that = this;
if (this.browser.indexOf("Chrome") > -1) {
if (that.onStartRecording && typeof that.onStartRecording === 'function') {
that.onStartRecording();
}
that.recorder = new MediaRecorder(that.stream);
that.recorder.ondataavailable = function(e) {that.data_array.push(e.data)};
that.recorder.start();
} else if (that.browser.indexOf("Firefox") > -1 || that.browser.indexOf("Opera") > -1) {
if (that.onStartRecording && typeof that.onStartRecording === 'function') {
that.onStartRecording();
}
that.recorder = new MediaRecorder(that.stream);
that.recorder.start();
} else {
if (that.onStartRecording && typeof that.onStartRecording === 'function') {
that.onStartRecording();
}
that.recorder = new MediaRecorder(that.stream);
that.recorder.start();
that.recorder.ondataavailable = function(e) {that.data_array.push(e.data)};
}
};
requestMedia.prototype.stopRecording = function() {
/*
Stop the recording of the feed.
The variable browser is used to differentiate between Chrome and firefox/Opera,
because the function mediaRecorder has a different behavior on chrome.
Optional:
Receive a function on parameters to execute after stoping the recording.
TODO: Exception/Error Handling; Make possible to record only video or only audio;
*/
var that = this;
if (that.browser.indexOf("Chrome") > -1) {
that.promise = new Promise(function(resolve) {
that.recorder.stop();
that.interval = setInterval(function(){
if (that.data_array.length > 0) {
clearInterval(that.interval);
resolve(that.data_array);
}
}, 2000)
});
that.promise.then(function(e) {
that.blob = new Blob(e, {'type' : that.file_type});
that.urlobj = URL.createObjectURL(that.blob);
if (that.feed_out) {
//In case we hava received feed_out, this insert the recorded file on the element(<video>/<audio>) with the id received in feed_out.
that.feed_out.setAttribute('src', URL.createObjectURL(that.blob));
}
if (that.onStopRecording && typeof that.onStopRecording === 'function') {
//This timeout is here, just to make sure we execute the function after the recorder stoped.
setTimeout(that.onStopRecording(), 50);
}
that.data_array = [];
});
} else if (that.browser.indexOf("Firefox") > -1 || that.browser.indexOf("Opera") > -1) {
that.recorder.ondataavailable = function(e) {
that.urlobj = URL.createObjectURL(e.data);
that.blob = new Blob([e.data], {'type': that.file_type});
if (that.feed_out) {
that.feed_out.setAttribute('src', URL.createObjectURL(e.data));
}
if (that.onStopRecording && typeof that.onStopRecording === 'function') {
setTimeout(that.onStopRecording(), 50);
}
};
that.recorder.stop();
} else {
that.recorder.stop();
that.blob = new Blob(that.data_array, {'type' : that.file_type});
that.urlobj = URL.createObjectURL(that.blob);
if (that.feed_out) {
that.feed_out.setAttribute('src', URL.createObjectURL(that.blob));
}
if (that.onStopRecording && typeof that.onStopRecording === 'function') {
setTimeout(that.onStopRecording(), 50);
}
that.data_array = [];
}
};
requestMedia.prototype.startCanvas = function (w) {
/*Initiate the canvas object with explicit width and height generated accordingly to width value and aspect ratio of video element.*/
var that = this;
var width = w, height = 0, context = null;
if (that.getCanvas()){
height = that.feed_in.videoHeight / (that.feed_in.videoWidth/width);
// if (isNaN(height)) height = width / (4/3);
if (isNaN(height)) height = width * 9/12;
that.canvas.setAttribute('width', width);
that.canvas.setAttribute('height', height);
}
context = that.canvas.getContext('2d');
context.fillStyle = "#AAA";
context.fillRect(0, 0, that.canvas.width, that.canvas.height);
var data = that.canvas.toDataURL('image/png');
that.feed_out.setAttribute('src', data);
}
requestMedia.prototype.takePicture = function () {
/*
From stream object generate image by requested frame moment.
*/
var that = this;
var context = that.canvas.getContext('2d');
if (that.canvas.width && that.canvas.height){
width = that.canvas.width;
height = that.canvas.height;
context.drawImage(that.feed_in, 0, 0, width, height);
var urlimg = that.canvas.toDataURL('image/png');
that.feed_out.setAttribute('src', urlimg);
that.urlobj = urlimg;
var BASE64_MARKER = ';base64,';
var parts = urlimg.split(BASE64_MARKER);
var content_type = parts[0].split(':')[1];
var filebinary = window.atob(parts[1]);
var filebinary_length = filebinary.length;
var uInt8Array = new Uint8Array(filebinary_length);
for (var i = 0; i < filebinary_length; ++i) {
uInt8Array[i] = filebinary.charCodeAt(i);
}
that.blob = new Blob([uInt8Array], {'type': content_type});
if (that.onPictureTaken && typeof that.onPictureTaken === 'function') {
setTimeout(that.onPictureTaken(), 50);
}
}else{
console.log('erro');
console.log(that.canvas);
}
}
requestMedia.prototype.download = function() {
/*
Download the file recorded by the feed.
The first timeout is here just to make sure the click function worked before we remove the element 'a'.
*/
var that = this;
if (that.onDownload && typeof that.onDownload === 'function') {
that.onDownload();
}
a = document.createElement('a');
a.style = "display: none";
a.download = that.name;
a.href = that.urlobj;
a.textContent = a.download;
document.body.appendChild(a);
a.click();
setTimeout(function(){
document.body.removeChild(a);
URL.revokeObjectURL(a.href);
}, 200);
};