-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrecordingServer.js
More file actions
84 lines (68 loc) · 1.83 KB
/
recordingServer.js
File metadata and controls
84 lines (68 loc) · 1.83 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const BinaryServer = require('binaryjs').BinaryServer
const wav = require('wav')
const isEmpty = require('lodash').isEmpty
import {
generateChunkPath,
startRecording,
completeRecording,
convertFileToMp3,
uploadToS3,
clearRecordingPath
} from './recordingUtils'
const run = server => {
const bs = server
? BinaryServer({ server: server, path: '/recording' })
: BinaryServer({ port: 9001 })
console.log(`Wait for new user connections`)
bs.on('connection', client => {
console.dir('connection')
let fileWriter = null
client.on('stream', (stream, meta) => {
console.log('stream')
console.log('meta', meta)
const { id, chunkId, sampleRate } = meta
client.meta = {
id,
chunkId
}
startRecording(id)
const filePath = generateChunkPath(id, chunkId)
console.log('client sample rate:', sampleRate)
console.log('writing in:', filePath)
let fileWriter = new wav.FileWriter(filePath, {
channels: 1,
sampleRate: sampleRate,
bitDepth: 16
})
stream.pipe(fileWriter)
})
client.on('close', () => {
if (isEmpty(client.meta)) {
return
}
const { id, chunkId } = client.meta
completeRecording(id)
console.log('stopped recording')
convertFileToMp3(id).then(filePath => {
uploadToS3(filePath, id, (err, data) => {
if (err) {
console.log('error')
console.error(err);
} else {
console.log('success')
console.dir(data);
console.log('Success upload to S3', id);
clearRecordingPath(id);
}
});
})
if (fileWriter !== null) {
fileWriter.end()
}
})
})
}
if (!module.parent) {
run()
}
export default run