forked from mafintosh/hyperphone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·63 lines (51 loc) · 1.73 KB
/
index.js
File metadata and controls
executable file
·63 lines (51 loc) · 1.73 KB
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
#!/usr/bin/env node
const Hyperbeam = require('hyperbeam')
const { spawn } = require('child_process')
if (process.argv.length < 3) {
console.error('Usage: hyperphone <topic>')
process.exit(1)
}
const osAudioMap = {
linux: 'alsa',
darwin: 'avfoundation',
windows: 'dshow'
}
let record
let play
const beam = new Hyperbeam('hyperphone ' + process.argv.slice(2).join(' '))
beam.on('remote-address', function ({ host, port }) {
if (!host) console.error('[hyperbeam] Could not detect remote address')
else console.error('[hyperbeam] Joined the DHT - remote address is ' + host + ':' + port)
if (port) console.error('[hyperbeam] Network is holepunchable \\o/')
})
beam.on('connected', function () {
console.error('[hyperbeam] Success! Encrypted tunnel established to remote peer')
record = spawn('ffmpeg', ['-f', osAudioMap[process.platform], '-i', 'default', '-fflags', 'nobuffer', '-ar', '44100', '-vn', '-f', 'mpegts', '-v', '8', '-hide_banner', '-'], {
stdio: ['inherit', 'pipe', 'inherit' ]
})
play = spawn('ffplay', ['-f', 'mpegts', '-probesize', '32', '-analyzeduration', '0', '-af', 'adeclick=w=10:o=50', '-i', '-v', '8', '-hide_banner', '-nodisp', '-'], {
stdio: ['pipe', 'inherit', 'inherit' ]
})
record.stdout.pipe(beam).pipe(play.stdin)
})
beam.on('end', () => {
beam.end()
if (record) record.kill()
if (play) play.kill()
})
beam.resume()
beam.pause()
process.once('SIGINT', () => {
if (record) record.kill()
if (play) play.kill()
if (!beam.connected) closeASAP()
else beam.end()
})
function closeASAP () {
console.error('[hyperbeam] Shutting down beam...')
const timeout = setTimeout(() => process.exit(1), 2000)
beam.destroy()
beam.on('close', function () {
clearTimeout(timeout)
})
}