|
@@ -1,4 +1,4 @@
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
@@ -7,13 +7,37 @@
|
|
|
|
|
|
|
|
|
(function(window) {
|
|
|
+var _userMedia;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+function FlashError() {
|
|
|
+ var temp = Error.apply(this, arguments);
|
|
|
+ temp.name = this.name = "FlashError";
|
|
|
+ this.stack = temp.stack;
|
|
|
+ this.message = temp.message;
|
|
|
+}
|
|
|
+
|
|
|
+function WebcamError() {
|
|
|
+ var temp = Error.apply(this, arguments);
|
|
|
+ temp.name = this.name = "WebcamError";
|
|
|
+ this.stack = temp.stack;
|
|
|
+ this.message = temp.message;
|
|
|
+}
|
|
|
+
|
|
|
+IntermediateInheritor = function() {};
|
|
|
+IntermediateInheritor.prototype = Error.prototype;
|
|
|
+
|
|
|
+FlashError.prototype = new IntermediateInheritor();
|
|
|
+WebcamError.prototype = new IntermediateInheritor();
|
|
|
|
|
|
var Webcam = {
|
|
|
- version: '1.0.6',
|
|
|
+ version: '1.0.8',
|
|
|
|
|
|
|
|
|
protocol: location.protocol.match(/https/i) ? 'https' : 'http',
|
|
|
- swfURL: '',
|
|
|
loaded: false,
|
|
|
live: false,
|
|
|
userMedia: true,
|
|
@@ -29,7 +53,14 @@ var Webcam = {
|
|
|
flip_horiz: false,
|
|
|
fps: 30,
|
|
|
upload_name: 'webcam',
|
|
|
- constraints: null
|
|
|
+ constraints: null,
|
|
|
+ swfURL: '',
|
|
|
+ flashNotDetectedText: 'ERROR: No Adobe Flash Player detected. Webcam.js relies on Flash for browsers that do not support getUserMedia (like yours).'
|
|
|
+ },
|
|
|
+
|
|
|
+ errors: {
|
|
|
+ FlashError: FlashError,
|
|
|
+ WebcamError: WebcamError
|
|
|
},
|
|
|
|
|
|
hooks: {},
|
|
@@ -73,7 +104,7 @@ var Webcam = {
|
|
|
elem = document.getElementById(elem) || document.querySelector(elem);
|
|
|
}
|
|
|
if (!elem) {
|
|
|
- return this.dispatch('error', "Could not locate DOM element to attach to.");
|
|
|
+ return this.dispatch('error', new WebcamError("Could not locate DOM element to attach to."));
|
|
|
}
|
|
|
this.container = elem;
|
|
|
elem.innerHTML = '';
|
|
@@ -87,12 +118,21 @@ var Webcam = {
|
|
|
if (!this.params.width) this.params.width = elem.offsetWidth;
|
|
|
if (!this.params.height) this.params.height = elem.offsetHeight;
|
|
|
|
|
|
+
|
|
|
+ if (!this.params.width || !this.params.height) {
|
|
|
+ return this.dispatch('error', new WebcamError("No width and/or height for webcam. Please call set() first, or attach to a visible element."));
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
if (!this.params.dest_width) this.params.dest_width = this.params.width;
|
|
|
if (!this.params.dest_height) this.params.dest_height = this.params.height;
|
|
|
|
|
|
+ this.userMedia = _userMedia === undefined ? this.userMedia : _userMedia;
|
|
|
|
|
|
- if (this.params.force_flash) this.userMedia = null;
|
|
|
+ if (this.params.force_flash) {
|
|
|
+ _userMedia = this.userMedia;
|
|
|
+ this.userMedia = null
|
|
|
+ }
|
|
|
|
|
|
|
|
|
if (typeof this.params.fps !== "number") this.params.fps = 30;
|
|
@@ -139,16 +179,18 @@ var Webcam = {
|
|
|
})
|
|
|
.then( function(stream) {
|
|
|
|
|
|
+ video.onloadedmetadata = function(e) {
|
|
|
+ self.stream = stream;
|
|
|
+ self.loaded = true;
|
|
|
+ self.live = true;
|
|
|
+ self.dispatch('load');
|
|
|
+ self.dispatch('live');
|
|
|
+ self.flip();
|
|
|
+ };
|
|
|
video.src = window.URL.createObjectURL( stream ) || stream;
|
|
|
- self.stream = stream;
|
|
|
- self.loaded = true;
|
|
|
- self.live = true;
|
|
|
- self.dispatch('load');
|
|
|
- self.dispatch('live');
|
|
|
- self.flip();
|
|
|
})
|
|
|
.catch( function(err) {
|
|
|
- return self.dispatch('error', "Could not access webcam: " + err.name + ": " + err.message, err);
|
|
|
+ return self.dispatch('error', err);
|
|
|
});
|
|
|
}
|
|
|
else {
|
|
@@ -200,7 +242,12 @@ var Webcam = {
|
|
|
delete this.stream;
|
|
|
delete this.video;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ if (this.userMedia !== true) {
|
|
|
+
|
|
|
+ this.getMovie()._releaseCamera();
|
|
|
+ }
|
|
|
+
|
|
|
if (this.container) {
|
|
|
this.container.innerHTML = '';
|
|
|
delete this.container;
|
|
@@ -271,16 +318,23 @@ var Webcam = {
|
|
|
return true;
|
|
|
}
|
|
|
else if (name == 'error') {
|
|
|
+ if ((args[0] instanceof FlashError) || (args[0] instanceof WebcamError)) {
|
|
|
+ message = args[0].message;
|
|
|
+ } else {
|
|
|
+ message = "Could not access webcam: " + args[0].name + ": " +
|
|
|
+ args[0].message + " " + args[0].toString();
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
- alert("Webcam.js Error: " + args[0]);
|
|
|
+ alert("Webcam.js Error: " + message);
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
},
|
|
|
-
|
|
|
- setSWFLocation: function(url) {
|
|
|
-
|
|
|
- this.swfURL = url;
|
|
|
+
|
|
|
+ setSWFLocation: function(value) {
|
|
|
+
|
|
|
+ this.set('swfURL', value);
|
|
|
},
|
|
|
|
|
|
detectFlash: function() {
|
|
@@ -315,22 +369,23 @@ var Webcam = {
|
|
|
|
|
|
getSWFHTML: function() {
|
|
|
|
|
|
- var html = '';
|
|
|
+ var html = '',
|
|
|
+ swfURL = this.params.swfURL;
|
|
|
|
|
|
|
|
|
if (location.protocol.match(/file/)) {
|
|
|
- this.dispatch('error', "Flash does not work from local disk. Please run from a web server.");
|
|
|
+ this.dispatch('error', new FlashError("Flash does not work from local disk. Please run from a web server."));
|
|
|
return '<h3 style="color:red">ERROR: the Webcam.js Flash fallback does not work from local disk. Please run it from a web server.</h3>';
|
|
|
}
|
|
|
|
|
|
|
|
|
if (!this.detectFlash()) {
|
|
|
- this.dispatch('error', "Adobe Flash Player not found. Please install from get.adobe.com/flashplayer and try again.");
|
|
|
- return '<h3 style="color:red">ERROR: No Adobe Flash Player detected. Webcam.js relies on Flash for browsers that do not support getUserMedia (like yours).</h3>';
|
|
|
+ this.dispatch('error', new FlashError("Adobe Flash Player not found. Please install from get.adobe.com/flashplayer and try again."));
|
|
|
+ return '<h3 style="color:red">' + this.params.flashNotDetectedText + '</h3>';
|
|
|
}
|
|
|
|
|
|
|
|
|
- if (!this.swfURL) {
|
|
|
+ if (!swfURL) {
|
|
|
|
|
|
var base_url = '';
|
|
|
var scpts = document.getElementsByTagName('script');
|
|
@@ -341,8 +396,8 @@ var Webcam = {
|
|
|
idx = len;
|
|
|
}
|
|
|
}
|
|
|
- if (base_url) this.swfURL = base_url + '/webcam.swf';
|
|
|
- else this.swfURL = 'webcam.swf';
|
|
|
+ if (base_url) swfURL = base_url + '/webcam.swf';
|
|
|
+ else swfURL = 'webcam.swf';
|
|
|
}
|
|
|
|
|
|
|
|
@@ -359,17 +414,17 @@ var Webcam = {
|
|
|
}
|
|
|
|
|
|
|
|
|
- html += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" type="application/x-shockwave-flash" codebase="'+this.protocol+'://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="'+this.params.width+'" height="'+this.params.height+'" id="webcam_movie_obj" align="middle"><param name="wmode" value="opaque" /><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="false" /><param name="movie" value="'+this.swfURL+'" /><param name="loop" value="false" /><param name="menu" value="false" /><param name="quality" value="best" /><param name="bgcolor" value="#ffffff" /><param name="flashvars" value="'+flashvars+'"/><embed id="webcam_movie_embed" src="'+this.swfURL+'" wmode="opaque" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="'+this.params.width+'" height="'+this.params.height+'" name="webcam_movie_embed" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="'+flashvars+'"></embed></object>';
|
|
|
+ html += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" type="application/x-shockwave-flash" codebase="'+this.protocol+'://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="'+this.params.width+'" height="'+this.params.height+'" id="webcam_movie_obj" align="middle"><param name="wmode" value="opaque" /><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="false" /><param name="movie" value="'+swfURL+'" /><param name="loop" value="false" /><param name="menu" value="false" /><param name="quality" value="best" /><param name="bgcolor" value="#ffffff" /><param name="flashvars" value="'+flashvars+'"/><embed id="webcam_movie_embed" src="'+swfURL+'" wmode="opaque" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="'+this.params.width+'" height="'+this.params.height+'" name="webcam_movie_embed" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="'+flashvars+'"></embed></object>';
|
|
|
|
|
|
return html;
|
|
|
},
|
|
|
|
|
|
getMovie: function() {
|
|
|
|
|
|
- if (!this.loaded) return this.dispatch('error', "Flash Movie is not loaded yet");
|
|
|
+ if (!this.loaded) return this.dispatch('error', new FlashError("Flash Movie is not loaded yet"));
|
|
|
var movie = document.getElementById('webcam_movie_obj');
|
|
|
if (!movie || !movie._snap) movie = document.getElementById('webcam_movie_embed');
|
|
|
- if (!movie) this.dispatch('error', "Cannot locate Flash movie in DOM");
|
|
|
+ if (!movie) this.dispatch('error', new FlashError("Cannot locate Flash movie in DOM"));
|
|
|
return movie;
|
|
|
},
|
|
|
|
|
@@ -504,9 +559,9 @@ var Webcam = {
|
|
|
var self = this;
|
|
|
var params = this.params;
|
|
|
|
|
|
- if (!this.loaded) return this.dispatch('error', "Webcam is not loaded yet");
|
|
|
-
|
|
|
- if (!user_callback) return this.dispatch('error', "Please provide a callback function or canvas to snap()");
|
|
|
+ if (!this.loaded) return this.dispatch('error', new WebcamError("Webcam is not loaded yet"));
|
|
|
+
|
|
|
+ if (!user_callback) return this.dispatch('error', new WebcamError("Please provide a callback function or canvas to snap()"));
|
|
|
|
|
|
|
|
|
if (this.preview_active) {
|
|
@@ -611,12 +666,11 @@ var Webcam = {
|
|
|
|
|
|
this.live = true;
|
|
|
this.dispatch('live');
|
|
|
- this.flip();
|
|
|
break;
|
|
|
|
|
|
case 'error':
|
|
|
|
|
|
- this.dispatch('error', msg);
|
|
|
+ this.dispatch('error', new FlashError(msg));
|
|
|
break;
|
|
|
|
|
|
default:
|