forked from streamproc/MediaStreamRecorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MediaStreamRecorder.js
76 lines (58 loc) · 2.48 KB
/
MediaStreamRecorder.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
// Muaz Khan - https://github.com/muaz-khan
// neizerth - https://github.com/neizerth
// MIT License - https://www.webrtc-experiment.com/licence/
// Documentation - https://github.com/streamproc/MediaStreamRecorder
// ==========================================================
// MediaStreamRecorder.js
function MediaStreamRecorder(mediaStream) {
if (!mediaStream) throw 'MediaStream is mandatory.';
// void start(optional long timeSlice)
// timestamp to fire "ondataavailable"
this.start = function(timeSlice) {
// Media Stream Recording API has not been implemented in chrome yet;
// That's why using WebAudio API to record stereo audio in WAV format
var Recorder = IsChrome ? window.StereoRecorder : window.MediaRecorder;
// video recorder (in WebM format)
if (this.mimeType === 'video/webm') Recorder = window.WhammyRecorder;
// video recorder (in GIF format)
if (this.mimeType === 'image/gif') Recorder = window.GifRecorder;
mediaRecorder = new Recorder(mediaStream);
mediaRecorder.ondataavailable = this.ondataavailable;
mediaRecorder.onstop = this.onstop;
// Merge all data-types except "function"
mediaRecorder = mergeProps(mediaRecorder, this);
mediaRecorder.start(timeSlice);
};
this.stop = function() {
if (mediaRecorder) mediaRecorder.stop();
};
this.ondataavailable = function(blob) {
console.log('ondataavailable..', blob);
};
this.onstop = function() {
console.log('stopped..');
};
// Reference to "MediaRecorder.js"
var mediaRecorder;
}
// ==========================
// Cross-Browser Declarations
// Media Stream Recording API representer
MediaRecorderWrapper = window.MediaRecorder;
// animation-frame used in WebM recording
requestAnimationFrame = window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame;
cancelAnimationFrame = window.webkitCancelAnimationFrame || window.mozCancelAnimationFrame;
// WebAudio API representer
AudioContext = window.webkitAudioContext || window.mozAudioContext;
URL = window.URL || window.webkitURL;
navigator.getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
IsChrome = !!navigator.webkitGetUserMedia;
// Merge all other data-types except "function"
function mergeProps(mergein, mergeto) {
for (var t in mergeto) {
if (typeof mergeto[t] !== 'function') {
mergein[t] = mergeto[t];
}
}
return mergein;
}