-
-
Save redgecombe/835fe42b8308a0afaecc to your computer and use it in GitHub Desktop.
Sample app for desktop capture API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"main": "screencapturetest.html", | |
"name": "Screen cap test", | |
"description": "screen cap test", | |
"version": "0.0.1", | |
"keywords": [ ], | |
"chromium-args" : "--enable-usermedia-screen-capturing", | |
"window": { | |
"show": true, | |
"title": "Screen cap test", | |
"toolbar": true, | |
"frame": true, | |
"width": 1920, | |
"height": 1080, | |
"position": "center", | |
"resizable": true | |
}, | |
"webkit": { | |
"plugin": true | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<script> | |
var gui = require('nw.gui'); | |
navigator.getUserMedia = navigator.mozGetUserMedia || navigator.webkitGetUserMedia; | |
gui.Screen.Init(); | |
var desktopcapture = gui.Screen.DesktopCaptureMonitor; | |
var once = false; | |
var streams = {}; | |
//HTML Generating | |
/////////////////////////////////////////// | |
function refreshGroupingHTML(id) { | |
var order = streams[id].order; | |
var grouping = document.getElementById('grouping' + order); | |
if (grouping == undefined || grouping == null) { | |
console.log("index undef:" + order); | |
} else { | |
grouping.innerHTML = "<img id='stream" + order + "' src='data:image/png;base64," + streams[id].png + "' onclick='javascript:startStream(\"" + id + "\");'/>"; | |
grouping.innerHTML = grouping.innerHTML + "<span style='max-width:100px; display:block;overflow: hidden;' id='title" + order + "'>" + streams[id].name + "</span><br>"; | |
} | |
} | |
function removeLastChild() { | |
var thumbnails = document.getElementById('thumbnails'); | |
thumbnails.removeChild(thumbnails.lastChild); | |
} | |
function addGrouping(index) { | |
var thumbsdiv = document.getElementById('thumbnails'); | |
thumbsdiv.innerHTML = thumbsdiv.innerHTML + "<div style='display: inline-block;vertical-align: top;' id='grouping" + index + "'>"; | |
thumbsdiv.innerHTML = thumbsdiv.innerHTML + "</div>" | |
} | |
function setGroupingsNotStarted() { | |
document.getElementById('thumbnails').innerHTML = "[Not started]"; | |
} | |
function clearGroupings() { | |
document.getElementById('thumbnails').innerHTML = ""; | |
} | |
/////////////////////////////////////////// | |
//End HTML Generating | |
//Picker logic | |
/////////////////////////////////////////// | |
function start() { | |
if (desktopcapture.started) | |
return; | |
clearGroupings(); | |
desktopcapture.on("added", function (id, name, order,type) { | |
addGrouping(Object.keys(streams).length); | |
if(type=="screen") | |
name = "Screen " + (order + 1); | |
streams[id] = { "order": order, "png": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACklEQVQYV2P4DwABAQEAWk1v8QAAAABJRU5ErkJggg==", "name": name }; | |
refreshGroupingHTML(id); | |
}); | |
desktopcapture.on("removed", function (id) { | |
delete streams[id]; | |
removeLastChild(); | |
}); | |
desktopcapture.on("orderchanged", function (id, new_order, old_order) { | |
streams[id].order = new_order; | |
refreshGroupingHTML(id); | |
}); | |
desktopcapture.on("namechanged", function (id, name) { | |
streams[id].name = name; | |
refreshGroupingHTML(id); | |
}); | |
desktopcapture.on("thumbnailchanged", function (id, thumbnail) { | |
streams[id].png = thumbnail; | |
refreshGroupingHTML(id); | |
}); | |
desktopcapture.start(true, true); | |
} | |
function stop() { | |
if (!desktopcapture.started) | |
return; | |
desktopcapture.removeAllListeners("added"); | |
desktopcapture.removeAllListeners("removed"); | |
desktopcapture.removeAllListeners("orderchanged"); | |
desktopcapture.removeAllListeners("namechanged"); | |
desktopcapture.removeAllListeners("thumbnailchanged"); | |
desktopcapture.stop(); | |
streams = {}; | |
document.getElementById('video').src = ""; | |
setGroupingsNotStarted(); | |
} | |
//End picker logic | |
/////////////////////////////////////////// | |
//Stream viewer | |
/////////////////////////////////////////// | |
var curStream = null; | |
function startStream(id) { | |
if (id == "") | |
return; | |
var constraints = { | |
video: { | |
mandatory: { | |
chromeMediaSource: 'desktop', | |
chromeMediaSourceId: id, | |
maxWidth: 1920, | |
maxHeight: 1080 | |
} | |
} | |
}; | |
if (curStream != null) | |
curStream.stop(); | |
navigator.webkitGetUserMedia(constraints, function (stream) { | |
curStream = stream; | |
document.getElementById('video').src = URL.createObjectURL(stream); | |
document.getElementById('video').play(); | |
}, function (error) { | |
console.error(error); | |
}); | |
} | |
//End stream viewer | |
/////////////////////////////////////////// | |
</script> | |
<body> | |
<button onclick="javascript:start();">Start</button><button onclick="javascript:stop();">Stop</button><br/> | |
Click a thumbnail to view stream: | |
<div id="thumbnails" style="max-width: 800px;">[Not started]</div><br/> | |
<video id="video"></video><br/><br/><br/> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment