1
1
2
-
3
- function Video ( _w , _h ) {
4
- var imgData ;
5
2
var video ;
3
+ var hidden_ctx ;
4
+ var showBgImg = false ;
5
+ var showVideo = false ;
6
+
6
7
document . addEventListener ( "DOMContentLoaded" , function ( ) {
8
+ hidden_ctx = createHiddenCanvas ( "hidden_canvas" ) ;
9
+ video = document . createElement ( 'video' ) ;
10
+ document . body . appendChild ( video ) ;
7
11
8
- video = document . createElement ( 'video' ) ;
9
- document . body . appendChild ( video ) ;
12
+ video . autoplay = true ;
13
+ video . loop = true ;
14
+ video . setAttribute ( "id" , "videoOutput" ) ;
15
+ video . setAttribute ( "style" , "display:none;" ) ;
16
+ video . width = 320 ;
17
+ video . height = 240 ;
10
18
11
- video . autoplay = true ;
12
- video . loop = true ;
13
- video . setAttribute ( "id" , "videoOutput" ) ;
14
- video . setAttribute ( "style" , "display:none;" ) ;
15
- video . width = _w || 320 ;
16
- video . height = _h || 240 ;
17
19
18
- // check if browser supports getUserMedia - yes we are looking at you Safari!
19
- navigator . getUserMedia = navigator . getUserMedia || navigator . webkitGetUserMedia || navigator . mozGetUserMedia || navigator . msGetUserMedia ;
20
- window . URL = window . URL || window . webkitURL || window . mozURL || window . msURL ;
20
+ // check if browser supports getUserMedia - yes we are looking at you Safari!
21
+ navigator . getUserMedia = navigator . getUserMedia || navigator . webkitGetUserMedia || navigator . mozGetUserMedia || navigator . msGetUserMedia ;
22
+ window . URL = window . URL || window . webkitURL || window . mozURL || window . msURL ;
21
23
22
- if ( navigator . getUserMedia ) {
24
+ if ( navigator . getUserMedia ) {
23
25
24
- // Set the source of the video element with the stream from the camera
25
- if ( typeof MediaStreamTrack === 'undefined' ||
26
- typeof MediaStreamTrack . getSources === 'undefined' ) {
27
- alert ( 'This browser does not support MediaStreamTrack.\nTry Chrome.\n\nAlterntatively you may need to be serving this page using https' ) ;
28
- } else {
29
- MediaStreamTrack . getSources ( gotSources ) ;
30
- }
26
+ if ( ! navigator . mediaDevices || ! navigator . mediaDevices . enumerateDevices ) {
27
+ console . log ( "enumerateDevices() not supported." ) ;
28
+ return ;
29
+ }
31
30
32
- } else {
31
+ // List cameras and microphones.
33
32
34
- alert ( 'Native web camera streaming (getUserMedia) not supported in this browser.' ) ;
33
+ navigator . mediaDevices . enumerateDevices ( )
34
+ . then ( function ( devices ) {
35
+ gotSources ( devices ) ;
36
+ } )
37
+ . catch ( function ( err ) {
38
+ console . log ( err . name + ": " + err . message ) ;
39
+ } ) ;
35
40
36
- }
41
+ }
37
42
38
- //doing this so that can use another camera
43
+ //CHOOSE WHICH CAMERA TO USE
39
44
40
45
function setupCamera ( cameras ) {
41
46
42
- console . log ( cameras )
47
+ // console.log(cameras)
43
48
44
- var videoSource = cameras [ cameras . length - 1 ] . id ;
45
- // var videoSource = cameras[0].id;
49
+ // var videoSource = cameras[cameras.length-1].id;
50
+ var videoSource = cameras [ 0 ] . id ;
46
51
47
52
var constraints = {
48
53
// audio: {
@@ -61,68 +66,76 @@ function Video(_w, _h){
61
66
62
67
}
63
68
64
-
65
-
66
69
function successCallback ( stream ) {
67
70
71
+
68
72
if ( video . mozCaptureStream ) {
69
73
video . mozSrcObject = stream ;
70
74
} else {
71
75
video . src = ( window . URL && window . URL . createObjectURL ( stream ) ) || stream ;
72
76
}
73
- video . play ( ) ;
77
+ video . play ( ) ;
78
+ this . video = video ;
74
79
}
75
80
76
81
function errorCallback ( error ) {
77
82
alert ( 'Unable to get webcam stream.' ) ;
78
83
}
79
84
80
85
81
- // hacky loop to make sure video is streaming
86
+ // hacky loop to make sure video is streaming
87
+
88
+ video . addEventListener ( 'loadeddata' , function ( ) {
89
+ var attempts = 50 ;
90
+ function checkVideo ( ) {
82
91
83
- video . addEventListener ( 'loadeddata' , function ( ) {
84
- var attempts = 50 ;
85
- function checkVideo ( ) {
92
+ if ( attempts > 0 ) {
86
93
87
- if ( attempts > 0 ) {
94
+ if ( video . videoWidth > 0 && video . videoHeight > 0 ) {
88
95
89
- if ( video . videoWidth > 0 && video . videoHeight > 0 ) {
96
+ video . available = true ;
97
+
98
+ } else {
99
+
100
+ // Wait a bit and try again
101
+ window . setTimeout ( checkVideo , 100 ) ;
102
+
103
+ }
90
104
91
- video . available = true ;
92
105
93
106
} else {
94
107
95
- // Wait a bit and try again
96
- window . setTimeout ( checkVideo , 100 ) ;
108
+ // Give up after 10 attempts
109
+ alert ( 'Unable to play video stream. Is webcam working?' ) ;
97
110
98
111
}
99
112
100
- } else {
101
- // Give up after 10 attempts
102
- alert ( 'Unable to play video stream. Is webcam working?' ) ;
113
+ attempts -- ;
114
+
103
115
}
104
- attempts -- ;
105
- }
106
116
107
- checkVideo ( ) ;
117
+ checkVideo ( ) ;
118
+
119
+ } , false ) ;
108
120
109
- } , false ) ;
110
121
122
+ function gotSources ( devices ) {
111
123
112
- function gotSources ( sourceInfos ) {
113
124
var cameras = [ ] ;
114
- for ( var i = 0 ; i !== sourceInfos . length ; ++ i ) {
115
125
116
- var sourceInfo = sourceInfos [ i ] ;
117
- console . log ( sourceInfo ) ;
118
- if ( sourceInfo . kind === 'video' ) {
119
- console . log ( sourceInfo . label ) ;
120
- cameras . push ( sourceInfo ) ;
121
- }
122
- }
126
+ devices . forEach ( function ( device ) {
127
+
128
+ // console.log(device.kind + ": " + device.label + " id = " + device.deviceId);
129
+
130
+ if ( device . kind === 'videoinput' ) {
131
+ //console.log(device.deviceId);
132
+ cameras . push ( device ) ;
133
+ }
134
+
135
+ } )
136
+
123
137
setupCamera ( cameras ) ;
124
- }
125
138
126
- } ) ;
139
+ } ;
127
140
128
- }
141
+ } )
0 commit comments