forked from addyosmani/getUserMedia.js
/
getUserMedia_v2.js
147 lines (108 loc) · 3.45 KB
/
getUserMedia_v2.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
/**
* getUserMedia() polyfill
* Copyright (c) 2012, Addy Osmani
* Dual licensed under the MIT or GPL Version 2 licenses.
* Date: 09/12/2010
*
* @specs http://dev.w3.org/2011/webrtc/editor/getusermedia.html
**/
/*
MediaStreamOptions options
NavigatorUserMediaSuccessCallback successCallback
NavigatiorUserMediaErrorCallback errorCallback
*/
//MediaStreamOptions
$(function(){
var options = {
"audio": true,
"video": true,
extern: null,
append: true, // append object instead of overwriting
width: 320,
height: 240,
mode: "callback", // callback | save | stream
swffile: "resource/jscam_canvas_only.swf",
quality: 85,
debug: function () {},
onCapture: function () {},
onTick: function () {},
onSave: function () {},
onLoad: function () {}
}
//errorCallback optional
getUserMedia = function( sourceId, options, successCallback, errorCallback){
if(navigator.getUserMedia){
if(!(options.audio && options.video)){
console.log('This mode is not supported: NOT_SUPPORTED_ERR');
}else{
// assign the <video> to a variable
// this should be more flexible..perhaps pass in the
// source directly? That or fall on qSA for other selectors
var video = document.getElementById(sourceId);
// navigator.getUserMedia('video')
navigator.getUserMedia(options, successCallback, errorCallback);
// This should probably all be shifted to a cleaner object literal
successCallback = function( stream ){
//use stream
//video.src = stream;
video.src = window.URL.getObjectURL(stream);
}
errorCallback = function( error ){
console.error('An error occurred: [CODE ' + error.code + ']');
return;
}
// system should default to the system camera
// or microphone for the media stream
// UAs may allow users to use any media source
// including pre-recorded media files
// if the user indicates permission for local
// devices like webcams, an indicator should
// be displayed
var videoRatio = video.videoWidth / video.videoHeight;
}
}else{
//fallback to flash
var source = '<object id="XwebcamXobjectX" type="application/x-shockwave-flash" data="'+options.swffile+'" width="'+options.width+'" height="'+options.height+'"><param name="movie" value="'+options.swffile+'" /><param name="FlashVars" value="mode='+options.mode+'&quality='+options.quality+'" /><param name="allowScriptAccess" value="always" /></object>';
var el = document.getElementById(sourceId)
el.innerHTML = source;
(_register = function(run) {
var cam = document.getElementById('XwebcamXobjectX');
if (cam.capture !== undefined) {
/* Simple callback methods are not allowed :-/ */
options.capture = function(x) {
try {
return cam.capture(x);
} catch(e) {}
}
options.save = function(x) {
try {
return cam.save(x);
} catch(e) {}
}
options.setCamera = function(x) {
try {
return cam.setCamera(x);
} catch(e) {}
}
options.getCameraList = function() {
try {
return cam.getCameraList();
} catch(e) {}
}
//options.onLoad();
options.onLoad = successCallback;
} else if (0 == run) {
options.debug("error", "Flash movie not yet registered!");
} else {
/* Flash interface not ready yet */
window.setTimeout(_register, 1000 * (4 - run), run - 1);
}
})(3);
}
}
//
getUserMedia('webcam', options, function(){
console.log('success');
});
//
});