forked from leemeichin/say-cheese
-
Notifications
You must be signed in to change notification settings - Fork 0
/
say-cheese.js
200 lines (157 loc) · 5.11 KB
/
say-cheese.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
/*
* Say Cheese!
* Lee Machin, 2012
* http://leemach.in, http://new-bamboo.co.uk
*
* Minimal javascript library for integrating a webcam and snapshots into your app.
*
* Handles starting up the webcam and rendering the element, and also capturing shots
* in a separate canvas element.
*
* Depends on video and canvas, and of course, getUserMedia. It's unlikely to work
* on anything but the newest browsers.
*/
var SayCheese = (function() {
var SayCheese;
navigator.getUserMedia = (navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia ||
false);
window.AudioContext = (window.AudioContext ||
window.webkitAudioContext);
window.URL = (window.URL ||
window.webkitURL);
var ERRORS = {
NOT_SUPPORTED: 'NOT_SUPPORTED',
AUDIO_NOT_SUPPORTED: 'AUDIO_NOT_SUPPORTED'
}
SayCheese = function SayCheese(element, options) {
this.snapshots = [],
this.video = null,
this.events = {},
this.stream = null,
this.options = {
videoSource: null,
snapshots: true,
audio: false,
width: 320
};
this.setOptions(options);
this.element = document.querySelector(element);
return this;
};
SayCheese.prototype.on = function on(evt, handler) {
if (this.events.hasOwnProperty(evt) === false) {
this.events[evt] = [];
}
this.events[evt].push(handler)
};
SayCheese.prototype.off = function off(evt, handler) {
this.events[evt] = this.events[evt].filter(function(h) {
return h !== handler;
});
};
SayCheese.prototype.trigger = function trigger(evt, data) {
if (this.events.hasOwnProperty(evt) === false) {
return false;
}
this.events[evt].forEach(function(handler) {
handler.call(this, data);
}.bind(this));
};
SayCheese.prototype.setOptions = function setOptions(options) {
// just use naïve, shallow cloning
for (var opt in options) {
this.options[opt] = options[opt];
}
}
SayCheese.prototype.getStreamUrl = function getStreamUrl() {
if (window.URL && window.URL.createObjectURL) {
return window.URL.createObjectURL(this.stream);
} else {
return this.stream;
}
};
SayCheese.prototype.createVideo = function createVideo() {
var width = this.options.width,
height = 0,
streaming = false;
this.video = document.createElement('video');
this.video.addEventListener('canplay', function() {
if (!streaming) {
height = this.video.videoHeight / (this.video.videoWidth / width);
this.video.width = width;
this.video.height = height;
streaming = true;
return this.trigger('start');
}
}.bind(this), false);
};
SayCheese.prototype.linkAudio = function linkAudio() {
this.audioCtx = new window.AudioContext();
this.audioStream = this.audioCtx.createMediaStreamSource(this.stream);
var biquadFilter = this.audioCtx.createBiquadFilter();
this.audioStream.connect(biquadFilter);
biquadFilter.connect(this.audioCtx.destination);
};
SayCheese.prototype.takeSnapshot = function takeSnapshot(width, height) {
if (this.options.snapshots === false) {
return false;
}
width = width || this.video.videoWidth;
height = height || this.video.videoHeight;
var snapshot = document.createElement('canvas'),
ctx = snapshot.getContext('2d');
snapshot.width = width;
snapshot.height = height;
ctx.drawImage(this.video, 0, 0, width, height);
this.snapshots.push(snapshot);
this.trigger('snapshot', snapshot);
ctx = null;
};
/* Start up the stream, if possible */
SayCheese.prototype.start = function start() {
// fail fast and softly if browser not supported
if (navigator.getUserMedia === false) {
this.trigger('error', ERRORS.NOT_SUPPORTED);
return false;
}
var success = function success(stream) {
this.stream = stream;
this.createVideo();
if (navigator.mozGetUserMedia) {
this.video.mozSrcObject = stream;
} else {
this.video.src = this.getStreamUrl();
}
if (this.options.audio === true) {
try {
this.linkAudio();
} catch(e) {
this.trigger('error', ERRORS.AUDIO_NOT_SUPPORTED);
}
}
this.element.appendChild(this.video);
this.video.play();
this.trigger('success');
}.bind(this);
/* error is also called when someone denies access */
var error = function error(error) {
this.trigger('error', error);
}.bind(this);
return navigator.getUserMedia({ video: {
optional: [{
sourceId: this.options.videoSource
}]
}, audio: this.options.audio }, success, error);
};
SayCheese.prototype.stop = function stop() {
this.stream.stop();
if (window.URL && window.URL.revokeObjectURL) {
window.URL.revokeObjectURL(this.video.src);
}
return this.trigger('stop');
};
return SayCheese;
})();