-
Notifications
You must be signed in to change notification settings - Fork 158
/
getUserMedia.js
144 lines (117 loc) · 5.76 KB
/
getUserMedia.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
/*global navigator, document */
;(function (window, document) {
"use strict";
window.getUserMedia = function (options, successCallback, errorCallback) {
// Options are required
if (options !== undefined) {
// getUserMedia() feature detection
navigator.getUserMedia_ = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
if ( !! navigator.getUserMedia_) {
// constructing a getUserMedia config-object and
// an string (we will try both)
var option_object = {};
var option_string = '';
var getUserMediaOptions, container, temp, video, ow, oh;
if (options.video === true) {
option_object.video = true;
option_string = 'video';
}
if (options.audio === true) {
option_object.audio = true;
if (option_string !== '') {
option_string = option_string + ', ';
}
option_string = option_string + 'audio';
}
container = document.getElementById(options.el);
temp = document.createElement('video');
// Fix for ratio
ow = parseInt(container.offsetWidth, 10);
oh = parseInt(container.offsetHeight, 10);
if (options.width < ow && options.height < oh) {
options.width = ow;
options.height = oh;
}
// configure the interim video
temp.width = options.width;
temp.height = options.height;
temp.autoplay = true;
container.appendChild(temp);
video = temp;
// referenced for use in your applications
options.videoEl = video;
options.context = 'webrtc';
// first we try if getUserMedia supports the config object
try {
// try object
navigator.getUserMedia_(option_object, successCallback, errorCallback);
} catch (e) {
// option object fails
try {
// try string syntax
// if the config object failes, we try a config string
navigator.getUserMedia_(option_string, successCallback, errorCallback);
} catch (e2) {
// both failed
// neither object nor string works
return undefined;
}
}
} else {
// Act as a plain getUserMedia shield if no fallback is required
if (options.noFallback === undefined || options.noFallback === false) {
// Fallback to flash
var source, el, cam;
source = '<!--[if IE]>'+
'<object id="XwebcamXobjectX" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + options.width + '" height="' + options.height + '">'+
'<param name="movie" value="' + options.swffile + '" />'+
'<![endif]-->'+
'<!--[if !IE]>-->'+
'<object id="XwebcamXobjectX" type="application/x-shockwave-flash" data="' + options.swffile + '" width="' + options.width + '" height="' + options.height + '">'+
'<!--<![endif]-->'+
'<param name="FlashVars" value="mode=' + options.mode + '&quality=' + options.quality + '" />'+
'<param name="allowScriptAccess" value="always" />'+
'</object>';
el = document.getElementById(options.el);
el.innerHTML = source;
(function register(run) {
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.context = 'flash';
options.onLoad = successCallback;
} else if (run === 0) {
// options.debug("error", "Flash movie not yet registered!");
errorCallback();
} else {
// Flash interface not ready yet
window.setTimeout(register, 1000 * (4 - run), run - 1);
}
}(3));
}
}
}
};
}(this, document));