forked from anantn/webrtc-landing
-
Notifications
You must be signed in to change notification settings - Fork 22
/
compat_gum.html
291 lines (260 loc) · 7.83 KB
/
compat_gum.html
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<!DOCTYPE html>
<html>
<head>
<title>gUM Test Page</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" charset="utf-8">
<link href='https://fonts.googleapis.com/css?family=Gentium+Basic:400,700' rel='stylesheet' type='text/css'>
<style>
#main {
display: block;
margin: 0px auto;
text-align: center
}
#content {
display: inline-block;
}
#frames {
display: inline-block;
max-width: 180px;
vertical-align: top;
}
#startbuttons {
display: block;
}
#stopbuttons {
display: none;
}
#message {
font-size: 24px;
font-family: "Gentium Basic", serif;
}
h1 {
font-size: 40px;
font-family: "Gentium Basic", serif;
}
input {
font-size: 28px;
font-family: "Gentium Basic", serif;
}
p {
color: green;
}
p.error {
color: red;
}
</style>
</head>
<body>
<div id="main">
<h2><b>getUserMedia Test Page</b></h2>
<a href="http://mozilla.github.com/webrtc-landing">Main webrtc demo page</a><br>
Note: Audio will feedback unless you use a headset!<br><br>
<div id="startbuttons">
<input value="Video" onclick="startVideo();" type="button">
<input value="Audio" onclick="startAudio();" type="button">
<input value="Audio & Video" onclick="startAudioVideo();" type="button">
<input value="Picture" onclick="startPicture();" type="button">
</div>
<div id="images">
<div id="content"></div>
<div id="frames"></div>
</div>
<div id="message"></div>
<div style="display: none;" id="stopbuttons">
<input value="Stop" onclick="stopMedia();" type="button">
<input value="Pause/Play" onclick="pauseMedia();" type="button">
<input id="snapshot" value="Snapshot" onclick="startSnapshot();" type="button">
</div>
</div>
<script type="application/javascript">
var video_status = false;
var video = document.createElement("video");
video.setAttribute("width", 640);
video.setAttribute("height", 480);
video.setAttribute("autoplay","autoplay");
var snapshots = [];
var audio_status = false;
var audio = document.createElement("audio");
audio.setAttribute("controls", true);
var picture_status = false;
var picture = document.createElement("img");
var start = document.getElementById("startbuttons");
var stop = document.getElementById("stopbuttons");
var message = document.getElementById("message");
var content = document.getElementById("content");
var frames = document.getElementById("frames");
var snapshot = document.getElementById("snapshot");
var saved_stream = null;
var capturing = false;
// polyfill based on code from webrtc.org
// http://review.webrtc.org/930012/patch/2001/3002
var getUserMedia = null;
var attachMediaStream = null;
var detachMediaStream = null;
var stopMediaStream = null;
window.URL = window.URL || window.webkitURL;
if (navigator.mozGetUserMedia) {
console.log("This appears to be Firefox");
// Get UserMedia (only difference is the prefix).
// Code from Adam Barth.
getUserMedia = navigator.mozGetUserMedia.bind(navigator);
// Attach a media stream to an element.
attachMediaStream = function(element, stream) {
console.log("Attaching media stream");
element.mozSrcObject = stream;
};
detachMediaStream = function(element) {
var temp = element.mozSrcObject;
element.mozSrcObject = null;
return temp;
};
stopMediaStream = function(element) {
element.mozSrcObject.stop();
}
} else if (navigator.webkitGetUserMedia) {
console.log("This appears to be Chrome");
// The RTCPeerConnection object.
RTCPeerConnection = webkitRTCPeerConnection;
// Get UserMedia (only difference is the prefix).
// Code from Adam Barth.
getUserMedia = navigator.webkitGetUserMedia.bind(navigator);
// Attach a media stream to an element.
attachMediaStream = function(element, stream) {
element.src = window.URL.createObjectURL(stream);
//message.innerHTML = "<p>" + element.src + "</p>";
element._saved_stream = stream;
};
detachMediaStream = function(element) {
var temp = element._saved_stream;
element.src = null;
element._saved_stream = null;
return temp;
};
stopMediaStream = function(element) {
element._saved_stream.stop();
}
} else {
console.log("Browser does not appear to be WebRTC-capable");
}
function startVideo() {
video_status = true;
startMedia({video: true});
}
function startAudioVideo() {
video_status = true;
audio_status = true;
startMedia({video: true, audio: true});
}
function startAudio() {
audio_status = true;
startMedia({audio: true});
}
function startPicture() {
picture_status = true;
startMedia({picture: true});
}
function stopMedia() {
if (video_status) {
stopMediaStream(video);
detachMediaStream(video);
content.removeChild(video);
stopbuttons.removeChild(snapshot);
snapshot.value = "Snapshot";
frames.innerHTML = '';
capturing = false;
video_status = false;
} else if (audio_status) {
stopMediaStream(audio);
detachMediaStream(audio);
content.removeChild(audio);
audio_status = false;
} else if (picture_status) {
detachMediaStream(picture);
content.removeChild(picture);
picture_status = false;
}
saved_stream = null;
stop.style.display = "none";
start.style.display = "block";
}
function pauseMedia() {
if (saved_stream) {
if (video_status) {
attachMediaStream(video, saved_stream);
video.play();
} else if (audio_status) {
attachMediaStream(audio, saved_stream);
audio.play();
}
saved_stream = null;
} else {
if (video_status) {
video.pause();
saved_stream = detachMediaStream(video);
} else if (audio_status) {
audio.pause();
saved_stream = detachMediaStream(audio);
}
}
}
function startMedia(param) {
stop.style.display = "block";
start.style.display = "none";
try {
getUserMedia(param, function(stream) {
message.innerHTML = "<p>Success!</p>";
if (video_status) {
content.appendChild(video);
attachMediaStream(video, stream);
video.play();
frames.innerHTML = '';
stopbuttons.appendChild(snapshot);
} else if (audio_status) {
content.appendChild(audio);
attachMediaStream(audio, stream);
audio.play();
} else if (picture_status) {
content.appendChild(picture);
picture.src = window.URL.createObjectURL(stream);
picture.onload = function(e) {
window.URL.revokeObjectURL(this.src);
}
}
}, function(err) {
message.innerHTML = "<p class='error'>" + err + "</p>";
stopMedia();
});
} catch(e) {
message.innerHTML = "<p class='error'>" + e + "</p>";
stopMedia();
}
}
function startSnapshot() {
capturing = !capturing;
if (capturing) {
captureImage();
snapshot.value = "Stop Snapshot";
} else {
snapshot.value = "Snapshot";
}
}
function captureImage() {
if (video_status && capturing) {
//dump("Capturing len " + snapshots.length + "\n");
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
canvas.width = video.videoWidth/4;
canvas.height = video.videoHeight/4;
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
if (snapshots.unshift(canvas) > 4)
snapshots.length = 4;
frames.innerHTML = '';
for(var i=0; i<snapshots.length; i++) {
frames.appendChild(snapshots[i]);
}
setTimeout(captureImage, 2000);
}
}
</script>
</body>
</html>