Updated device-switch demo page to work with Chrome M30.

BUG=2218
R=braveyao@webrtc.org, dutton@google.com

Review URL: https://webrtc-codereview.appspot.com/2025004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@4892 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
vikasmarwaha@webrtc.org
2013-10-01 17:02:32 +00:00
parent b74b96f487
commit 19134bae95

View File

@ -1,154 +1,201 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<!-- Load the polyfill to switch-hit between Chrome and Firefox -->
<script src='../../base/adapter.js'></script>
<head> <head>
<title>Device Switching</title> <title>Device Switch Demo</title>
<style> <style>
video { h2 {
border:5px solid black; font-size: 1em;
width:480px; font-family: sans-serif;
height:360px; margin: 0 0 0.5em 0;
padding: 0;
} }
button { video {
font: 18px sans-serif; width:40%;
padding: 8px;
} }
</style> </style>
<script>
// If the getSourceInfos call is not present, fake it.
var fakeSources = [
{"id": "zxyfyz", "kind": "audio", "label": "Default audio"},
{"id": "xfesaa", "kind": "video", "label": "Default video",
"facing": "front"},
{"id": "blwe4e", "kind": "video", "label": "Alternate video",
"facing": "rear"}
];
var fakeSourcesNoLabel = [
{"id": "zxyfyz", "kind": "audio"},
{"id": "xfesaa", "kind": "video", "facing": "front"},
{"id": "blwe4e", "kind": "video", "facing": "rear"}
];
var isAuthorized = false;
// Make sure there's a MediaStreamTrack object to manipulate.
if (typeof(MediaStreamTrack) === 'undefined') {
MediaStreamTrack = new Object;
}
if (!MediaStreamTrack.getSourceInfos) {
MediaStreamTrack.getSourceInfos = function() {
if (!isAuthorized) {
return fakeSourcesNoLabel;
}
return fakeSources;
};
}
// End fake getSourceInfos implementation.
var mainStream;
var choices;
</script>
</head> </head>
<body> <body>
This demo shows device switching via the (proposed) device selection API. <video id="vid1" autoplay muted></video>
The big frame shows the currently playing stream; the grid below shows <video id="vid2" autoplay></video>
available devices, with a button for choosing a device. <div>
<p> <h2>Select an audio and video source, then click Start.</h2>
<video id="vid" autoplay="true"></video> Audio source: <select id="audiosrc"></select>
<br> Video source: <select id="videosrc"></select><br>
<button id="btn" onclick="start()">Start</button> <button id="btn1">Start</button>
<div id="choices"> <button id="btn2">Call</button>
<button id="btn3">Hang Up</button>
</div> </div>
<script> <script>
function failure() { var btn1 = document.querySelector('button#btn1');
console.log("Failure"); var btn2 = document.querySelector('button#btn2');
alert("Failure!") var btn3 = document.querySelector('button#btn3');
var audio_select = document.getElementById("audiosrc");
var video_select = document.getElementById("videosrc");
//audio_select.onchange = changeDevices;
//video_select.onchange = changeDevices;
btn1.onclick = start;
btn2.onclick = call;
btn3.onclick = hangup;
btn1.disabled = false;
btn2.disabled = true;
btn3.disabled = true;
var pc1,pc2;
var localstream;
var sdpConstraints = {'mandatory': {
'OfferToReceiveAudio':true,
'OfferToReceiveVideo':true }};
refreshSources();
function refreshSources() {
if (webrtcDetectedVersion >= 30) {
MediaStreamTrack.getSources(gotSources);
} else {
alert('Make sure that you have Chrome M30 to test device enumeration api.');
}
}
function gotSources(sourceInfos) {
var audio_count = 0;
var video_count = 0;
audio_select.disabled = true;
video_select.disabled = true;
audio_select.innerHTML = '';
video_select.innerHTML = '';
for (var i = 0; i < sourceInfos.length; i++) {
var option = document.createElement("option");
option.value = sourceInfos[i].id;
option.text = sourceInfos[i].label;
if (sourceInfos[i].kind === 'audio') {
audio_count++;
if (option.text === '') {
option.text = 'Audio ' + audio_count;
}
audio_select.appendChild(option);
} else {
video_count++;
if (option.text === '') {
option.text = 'Video ' + video_count;
}
video_select.appendChild(option);
}
}
audio_select.disabled = false;
video_select.disabled = false;
} }
function start() { function start() {
navigator.webkitGetUserMedia({video:true}, gotStream, failure); changeDevices();
btn.disabled = true; btn1.disabled = true;
btn2.disabled = false;
audio_select.disabled = true;
video_select.disabled = true;
}
function changeDevices() {
var audio_source = null;
var video_source = null;
if (audio_select.options.length > 0) {
audio_source = audio_select.options[audio_select.selectedIndex].value;
trace('selected audio_source :' + audio_source);
}
if (video_select.options.length > 0 ) {
video_source = video_select.options[video_select.selectedIndex].value;
trace('selected video_source :' + video_source);
}
setWebcamAndMic(audio_source, video_source);
}
function setWebcamAndMic(audio_source, video_source) {
trace("Requesting local stream");
// Call into getUserMedia via the polyfill (adapter.js).
getUserMedia({ audio: {optional: [{sourceId: audio_source}]},
video: {optional: [{sourceId: video_source}]}
}, gotStream, function() {});
} }
function gotStream(stream) { function gotStream(stream) {
mainStream = stream; trace("Received local stream");
video.src = webkitURL.createObjectURL(stream); // Call the polyfill wrapper to attach the media stream to this element.
// Telling fake that permission is granted: attachMediaStream(vid1, stream);
isAuthorized = true; localstream = stream;
getChoices(); }
function call() {
btn2.disabled = true;
btn3.disabled = false;
trace("Starting call");
videoTracks = localstream.getVideoTracks();
audioTracks = localstream.getAudioTracks();
if (videoTracks.length > 0) {
trace('Using Video device: ' + videoTracks[0].label);
}
if (audioTracks.length > 0) {
trace('Using Audio device: ' + audioTracks[0].label);
}
var servers = null;
pc1 = new RTCPeerConnection(servers);
trace("Created local peer connection object pc1");
pc1.onicecandidate = iceCallback1;
pc2 = new RTCPeerConnection(servers);
trace("Created remote peer connection object pc2");
pc2.onicecandidate = iceCallback2;
pc2.onaddstream = gotRemoteStream;
pc1.addStream(localstream);
trace("Adding Local Stream to peer connection");
pc1.createOffer(gotDescription1);
} }
function switchTo(id) { function gotDescription1(desc) {
console.log('Switching to camera with id ' + id); pc1.setLocalDescription(desc);
// Note: Constraint should be mandatory. Setting it to optional here trace("Offer from pc1 \n" + desc.sdp);
// so that call won't fail when the constraint is not known. pc2.setRemoteDescription(desc);
navigator.webkitGetUserMedia({'audio':false, // Since the "remote" side has no media stream we need
'video': {'optional': [{'sourceId': id }]}}, // to pass in the right constraints in order for it to
switchedTo, failure); // accept the incoming offer of audio and video.
pc2.createAnswer(gotDescription2, null, sdpConstraints);
} }
function switchedTo(stream) { function gotDescription2(desc) {
console.log("Switching main video feed to new track"); pc2.setLocalDescription(desc);
try { trace("Answer from pc2 \n" + desc.sdp);
mainStream.removeTrack(mainStream.getVideoTracks()[0]); pc1.setRemoteDescription(desc);
mainStream.addTrack(stream.getVideoTracks()[0]); }
} catch(e) {
console.log('Failure to switch tracks: ' + e); function hangup() {
trace("Ending call");
localstream.stop();
pc1.close();
pc2.close();
pc1 = null;
pc2 = null;
btn3.disabled = true;
btn1.disabled = false;
audio_select.disabled = false;
video_select.disabled = false;
}
function gotRemoteStream(e) {
// Call the polyfill wrapper to attach the media stream to this element.
attachMediaStream(vid2, e.stream);
trace("Received remote stream");
}
function iceCallback1(event) {
if (event.candidate) {
pc2.addIceCandidate(new RTCIceCandidate(event.candidate));
trace("Local ICE candidate: \n" + event.candidate.candidate);
} }
} }
function getChoices() { function iceCallback2(event) {
choices = MediaStreamTrack.getSourceInfos(); if (event.candidate) {
var choiceTable = document.getElementById("choices"); pc1.addIceCandidate(new RTCIceCandidate(event.candidate));
var choicelist = ""; trace("Remote ICE candidate: \n " + event.candidate.candidate);
choicelist += "<table border>"
for (i = 0; i < choices.length; i++) {
if (choices[i].kind === 'video') {
// Create a table entry for the video, with a select button.
// There is a cleaner way to do this in JS. Apologies.
choicelist += "<tr><td>";
choicelist += choices[i].id;
choicelist += "<td>";
choicelist += choices[i].label;
choicelist += '<td><button onclick="switchTo(choices[';
choicelist += i;
choicelist += '].id)">Choose</button>';
choicelist += "<td>"
if (choices[i].id === videoIdNowPlaying()) {
choicelist += "Playing";
} }
}
}
choicelist += "</table>"
choiceTable.innerHTML = choicelist;
} }
// Returns the ID of the source of the currently playing video track.
// This function always returns a string, and logs why the right string
// is not returned if there is a problem.
function videoIdNowPlaying() {
if (!mainStream) {
console.log('No main stream');
return 'No main stream';
}
if (mainStream.getVideoTracks().length == 0) {
console.log('No video tracks');
return 'No video tracks';
}
if (!mainStream.getVideoTracks()[0].sourceId) {
console.log('No source ID');
return 'No source id';
}
return mainStream.videoTracks()[0].sourceId;
}
video = document.getElementById("vid");
getChoices();
</script> </script>
</body> </body>
</html> </html>