Skip to content

Instantly share code, notes, and snippets.

@mganeko
Last active August 29, 2015 14:12
Show Gist options
  • Select an option

  • Save mganeko/23e3530a95cff6df1ca7 to your computer and use it in GitHub Desktop.

Select an option

Save mganeko/23e3530a95cff6df1ca7 to your computer and use it in GitHub Desktop.
WebRTC mediasource select
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Capture Crop</title>
</head>
<body>
Media Source list<br />
<select id="mic_list" size="5" onchange="micSelected();">
</select>
<select id="camera_list" size="5" onchange="cameraSelected();">
</select>
<br />
<button id="start" type="button" onclick="startMedia1();">Start1</button>
<button id="start" type="button" onclick="startMedia2();">Start2</button>
<br />
<video id="local-video-1" autoplay="1" style="width:320px; height:240px; border: solid 1px;"></video>
<video id="local-video-2" autoplay="1" style="width:320px; height:240px; border: solid 1px;"></video>
<script>
var micList = document.getElementById("mic_list");
var cameraList = document.getElementById("camera_list");
var audioSource = null;
var videoSource = null;
MediaStreamTrack.getSources(function(sourceInfos) {
for (var i = 0; i != sourceInfos.length; ++i) {
var sourceInfo = sourceInfos[i];
if (sourceInfo.kind === 'audio') {
console.log(sourceInfo);
var id = sourceInfo.id;
var label = sourceInfo.label || 'microphone'; // label is available for https
audioSource = sourceInfo.id; // set last item
var option = document.createElement('option');
option.setAttribute('value', id);
option.innerHTML = label + '(' + id + ')';;
micList.appendChild(option);
}
else if (sourceInfo.kind === 'video') {
console.log(sourceInfo);
var id = sourceInfo.id;
var label = sourceInfo.label || 'camera'; // label is available for https
videoSource = sourceInfo.id; // set last item
var option = document.createElement('option');
option.setAttribute('value', id);
option.innerHTML = label + '(' + id + ')';
cameraList.appendChild(option);
} else {
console.log('Some other kind of source: ', sourceInfo);
}
}
});
function micSelected() {
var index = micList.selectedIndex;
if (index >= 0){
audioSource = micList.options[index].value;
console.log("mic selected:" + audioSource);
}
}
function cameraSelected() {
var index = cameraList.selectedIndex;
if (index >= 0){
videoSource = cameraList.options[index].value;
console.log("camera selected:" + videoSource);
}
}
function startMedia1() {
var constraints = {
audio: {
optional: [{sourceId: audioSource}]
},
video: {
optional: [{sourceId: videoSource}]
}
};
navigator.webkitGetUserMedia(constraints, successCallback1, errorCallback);
}
function startMedia2() {
var constraints = {
audio: {
optional: [{sourceId: audioSource}]
},
video: {
optional: [{sourceId: videoSource}]
}
};
navigator.webkitGetUserMedia(constraints, successCallback2, errorCallback);
}
function successCallback1(stream) {
var localVideo = document.getElementById('local-video-1');
localVideo.src = window.webkitURL.createObjectURL(stream);
}
function successCallback2(stream) {
var localVideo = document.getElementById('local-video-2');
localVideo.src = window.webkitURL.createObjectURL(stream);
}
function errorCallback(e) {
console.error(e);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment