Here is a sample script that will capture the screen to a texture over a specified time interval and send a downsized version of the image to all the connected websocket clients. The image is downsized and compressed to PNG in a separate thread, so it should not slow down the main graphics thread too much, depending on the size of your window. If you need to reduce the network traffic, you could try compressing to JPG instead.
Code:
import viz
import vizhtml
import viztask
import base64
import Image
import StringIO
viz.go()
viz.add('maze.osgb')
code = """
<html>
<head>
<title>vizhtml Screen Share Example</title>
<script type="text/javascript" src="vizhtml.js"></script>
</head>
<body>
<script language="javascript">
var socket = new VizardWebSocket();
socket.onevent('open', function(e){
document.getElementById('status').innerHTML = 'connected';
})
socket.onevent('close', function(e){
document.getElementById('status').innerHTML = 'waiting for connection';
socket.reconnect();
})
socket.onevent('set_image', function(e){
document.getElementById('screen').src = 'data:image/png;base64,' + e.data.data;
})
</script>
<div>Status:</div><div id='status'>waiting for connection</div></br>
<div>Screen:</div>
<img id='screen'>
</body>
</html>
"""
# http://localhost:8080/vizhtml/screen_share
vizhtml.registerCode('screen_share',code)
# Size of image to send
IMAGE_SIZE = [160,120]
# Time interval (in seconds) to send screen capture
CAPTURE_INTERVAL = 0.25
def ScreenCaptureTask():
# Blank texture that will hold screen capture
tex = viz.addBlankTexture([1,1])
def SendScreenImage(texture):
# Compute downsample size while preserving original aspect ratio
width, height = texture.getSize()[:2]
ratio = min( IMAGE_SIZE[0] / float(width), IMAGE_SIZE[1] / float(height) )
if ratio < 1.0:
size = [ int(round(width * ratio)), int(round(height * ratio)) ]
else:
size = [ width, height ]
# Convert texture to PIL image
im = Image.frombuffer('RGB',[width,height],texture.getImageData(),'raw','RGB',0,1)
# Resize image and flip
im = im.resize(size).transpose(Image.FLIP_TOP_BOTTOM)
# Save image to base 64 encoded PNG buffer
output = StringIO.StringIO()
im.save(output,'PNG')
buf = base64.b64encode(output.getvalue())
output.close()
# Send buffer to all connected clients
vizhtml.sendAll('set_image',viz.Data(data=buf))
while True:
# Wait for capture interval
yield viztask.waitTime(CAPTURE_INTERVAL)
# Capture screen to texture
viz.window.screenCapture(tex)
yield None
# Send texture to clients asynchronously
yield viztask.waitDirector(SendScreenImage,tex)
viztask.schedule( ScreenCaptureTask() )