-
Notifications
You must be signed in to change notification settings - Fork 5
/
example.js
68 lines (63 loc) · 1.65 KB
/
example.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
// import fs from 'node:fs';
// import path from 'node:path';
import {Writable, Transform} from 'node:stream';
import {print} from '@mediafish/flv';
import {createSimpleServer} from './index.js';
class Terminator extends Writable {
constructor() {
super({objectMode: true});
}
_write(chunk, encoding, cb) {
setImmediate(cb);
}
}
class Logger extends Transform {
constructor() {
super({objectMode: true});
this.index = 0;
}
_transform(obj, _, cb) {
const {type, timestamp, data} = obj;
if (type === 'video') {
console.log(`timestamp: ${timestamp}`);
print(data);
/*
if (this.index < 20) {
const filePath = path.join(__dirname, `video-${this.index++}.dat`);
fs.writeFile(filePath, data, err => {
if (err) {
return console.error(err.stack);
}
console.log(`Video writen to ${filePath}`);
});
}
*/
} else if (type === 'audio') {
console.log(`timestamp: ${timestamp}`);
print(data);
/*
if (this.index < 20) {
const filePath = path.join(__dirname, `audio-${this.index++}.dat`);
fs.writeFile(filePath, data, err => {
if (err) {
return console.error(err.stack);
}
console.log(`Video writen to ${filePath}`);
});
}
*/
} else if (type === 'data') {
console.log(`${timestamp} [Data] ${JSON.stringify(data, null, 4)}`);
}
cb(null, obj);
}
}
createSimpleServer('/live')
.pipe(new Logger())
.on('finish', () => {
console.log('Finish!');
})
.on('error', err => {
console.error(err.stack);
})
.pipe(new Terminator());