Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
// screen.js
// mjpeg content server
// Neil Gershenfeld
// 2/9/17
// todo
// standalone client content push
// browser client content push
// https
// parse command line
// add authentication
// interframe compression
// include audio
// uses maim for screen capture:
// https://github.com/naelstrof/maim
// https://github.com/naelstrof/maim/archive/master.zip
// install libimlib2-dev libxrandr-dev libxfixes-dev cmake
//
// command line
//
if (process.argv.length != 5) {
console.log("node screen.js server_port server_update_rate_ms client_update_rate_ms")
process.exit()
}
var port = parseInt(process.argv[2])
var server_delay = parseFloat(process.argv[3])
var client_delay = parseFloat(process.argv[4])
//
// requires
//
var http = require('http')
const spawn = require('child_process').spawn;
const spawnSync = require('child_process').spawnSync
const fork = require('child_process').fork
const fs = require('fs')
const events = require('events')
//
// globals
//
var img,newimg
var max=0
var last
//
// initialization
//
spawnSync('maim',['--showcursor','out.jpg'])
img = fs.readFileSync('out.jpg','binary')
var requests = [null]
//
// start http server
//
http.createServer(function(request,response) {
var headers = request.headers
var method = request.method
var url = request.url
if (url == '/') {
fs.readFile('viewer.html',function(err,data){
response.writeHead(200,{'Content-Type':'text/html'});
response.end(data)
})
}
else if (url == '/img') {
requests.push(response)
}
else if (url == '/initvars') {
var vars = {'client_delay':client_delay}
response.writeHead(200,{'Content-Type':'text/plain'});
response.end(JSON.stringify(vars))
}
else if (url == '/initimg') {
response.writeHead(200,{'Content-Type':'application/octet-stream'})
response.end(img,'binary')
}
else if (url == '/screen.js') {
fs.readFile('screen.js',function(err,data){
response.writeHead(200,{'Content-Type':'text/plain'});
response.end(data)
})
}
else if (url == '/viewer.html') {
fs.readFile('viewer.html',function(err,data){
response.writeHead(200,{'Content-Type':'text/plain'});
response.end(data)
})
}
else if (url == '/stats') {
var resp = "<html>"
resp += "<body>"
resp += "last: "+last+"<br>"
resp += "max: "+max+"<br>"
response.writeHead(200,{'Content-Type':'text/html'});
response.end(resp)
}
}).listen(port)
//
// start event loop
//
update()
//
// event loop
//
function update() {
if (requests[0] == null) {
const ps = spawn('maim',['--showcursor','out.jpg'])
ps.on('close',function(code){
fs.readFile('out.jpg','binary',function(err,data){
var changed = false
for (var i = 0; i < img.length; ++i) {
if (img[i] != data[i]) {
changed = true
break
}
}
if (changed == true) {
if (requests.length > max)
max = requests.length
last = requests.length
img = data
requests.splice(0,1)
requests.push(null)
//console.log('changed: '+requests.length)
}
//else
//console.log('not changed: '+requests.length)
})
setTimeout(update,server_delay)
})
}
else {
requests[0].writeHead(200,{'Content-Type':'application/octet-stream'})
requests[0].end(img,'binary')
requests.splice(0,1)
setTimeout(update,server_delay)
}
}