-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrun_with_audio.js
77 lines (61 loc) · 1.8 KB
/
run_with_audio.js
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
const commander = require('commander')
commander
.version('1.0.0', '-v, --version')
.usage('[OPTIONS]...')
.option('-p, --port <value>', 'port number to use, default 3000', 3000)
.option('-t, --thisip <value>','IP of the interface to bind')
.option('-b, --broadcastip <value>','IP of the interface to bind','255.255.255.255')
.parse(process.argv);
const options = commander.opts()
console.log(options)
const PPPP = require('./pppp')
const p = new PPPP(options)
const speaker = require('./speaker')
p.on('connected', (data) => {
console.log('connected!', data)
p.sendCMDrequestVideo1()
setTimeout(p.sendCMDrequestAudio.bind(p), 200)
p.sendCMDgetParams()
})
p.on('audioFrame', (audioFrame) => {
speaker.write(audioFrame.frame)
// console.log(audioFrame)
})
p.on('videoFrame', (videoFrame) => {
// console.log(videoFrame)
let s = '--xxxxxxkkdkdkdkdkdk__BOUNDARY\r\n'
s += 'Content-Type: image/jpeg\r\n\r\n'
videoStream.write(Buffer.from(s))
videoStream.write(videoFrame.frame)
})
// p.on('log', console.log)
p.on('cmd', console.log)
//http server with mjpeg
const PassThrough = require('stream').PassThrough
var videoStream = new PassThrough()
const http = require('http')
const server = http.createServer((req, res) => {
if (req.url === '/v.mjpg') {
res.setHeader(
'Content-Type',
'multipart/x-mixed-replace; boundary="xxxxxxkkdkdkdkdkdk__BOUNDARY"'
)
videoStream.pipe(res)
} else if (req.url === '/') {
res.statusCode = 200
res.setHeader('Content-Type', 'text/html; charset=utf-8')
res.end(
'<!DOCTYPE html>\r\n<http><head></head><body><img src="/v.mjpg"></body></html>'
)
}
})
server.listen(3000)
process.on('SIGINT', () => {
server.close()
server.unref()
p.destroy()
setTimeout(() => {
console.log('exiting.')
process.exit()
}, 1000)
})