gum_test_aec.html (238 lines of code) (raw):
<!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="gum_test_aec_files/css.css" 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: 20px;
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>
<hr>
<div id="background">
Background audio:
<input value="Start" onclick="startBackground();" type="button">
<input value="Stop" onclick="stopBackground();" type="button">
</div>
</div>
<script type="application/javascript">
var start = document.getElementById("startbuttons");
var stop = document.getElementById("stopbuttons");
var background = document.getElementById("background");
var message = document.getElementById("message");
var content = document.getElementById("content");
var frames = document.getElementById("frames");
var snapshot = document.getElementById("snapshot");
var video_status = false;
var video = document.createElement("video");
var song = document.createElement("audio");
song.setAttribute("controls", true);
song.setAttribute("loop", true);
if (0) {
content.appendChild(song);
song.play();
} else {
var songplay = document.createElement("audio");
songplay.setAttribute("controls", true);
background.appendChild(songplay);
}
video.setAttribute("width", 640);
video.setAttribute("height", 480);
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 saved_stream = null;
var capturing = false;
function startBackground() {
song.src = "test.ogg";
songplay.srcObject = song.mozCaptureStream();
song.play();
songplay.play();
}
function stopBackground() {
song.pause();
songplay.pause();
}
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) {
video.srcObject.stop();
video.srcObject = null;
content.removeChild(video);
stopbuttons.removeChild(snapshot);
snapshot.value = "Snapshot";
frames.innerHTML = '';
capturing = false;
video_status = false;
} else if (audio_status) {
audio.srcObject.stop();
audio.srcObject = null;
content.removeChild(audio);
audio_status = false;
} else if (picture_status) {
picture.srcObject = null;
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) {
video.srcObject = saved_stream;
video.play();
} else if (audio_status) {
audio.srcObject = saved_stream;
audio.play();
}
saved_stream = null;
} else {
if (video_status) {
video.pause();
saved_stream = video.srcObject;
video.srcObject = null;
} else if (audio_status) {
audio.pause();
saved_stream = audio.srcObject;
audio.srcObject = null;
}
}
}
function startMedia(param) {
stop.style.display = "block";
start.style.display = "none";
try {
window.navigator.mozGetUserMedia(param, function(stream) {
message.innerHTML = "<p>Success!</p>";
if (video_status) {
content.appendChild(video);
video.srcObject = stream;
video.play();
frames.innerHTML = '';
stopbuttons.appendChild(snapshot);
} else if (audio_status) {
content.appendChild(audio);
audio.muted = true;
audio.srcObject = 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>