-
Notifications
You must be signed in to change notification settings - Fork 6
/
hlsServer.js
61 lines (52 loc) · 1.71 KB
/
hlsServer.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
const fs = require('fs');
const mkdirp = require('mkdirp');
const http = require('http');
const defaultConfig = require('./defaultConfig.json');
const Logger = require('./logger');
const HLSSession = require('./hlsSession');
const requestHandler = require('./requestHandler');
class HLSServer {
constructor (config = defaultConfig) {
this.server = null;
this.config = { ...defaultConfig, ...config }; ;
this.hlsSessions = new Map();
}
run () {
try {
mkdirp.sync(this.config.mediaPath);
fs.accessSync(this.config.mediaPath, fs.constants.W_OK);
} catch (error) {
Logger.error(`HLS server startup failed. MediaRoot:${this.config.mediaPath} cannot be written.`);
return;
}
const port = this.config.port || 8080;
this.server = http.createServer(requestHandler(this.config, this.hlsSessions));
this.server.listen(port, (error) => {
if (error) {
Logger.error('HLS server startup failed. Problem creating http server', error);
} else {
Logger.debug(`HLS Server successfully started, listening on port ${this.config.port}, ready to start HLS sessions...`);
}
});
}
addStream (name, input) {
if (!this.hlsSessions.has(name)) {
const hlsSession = new HLSSession(name, input, this.config);
hlsSession.run();
this.hlsSessions.set(name, hlsSession);
hlsSession.on('end', (name) => {
this.hlsSessions.delete(name);
});
}
}
stopStream (name) {
if (this.hlsSessions.has(name)) {
const session = this.hlsSessions.get(name);
session.stop();
this.hlsSessions.delete(name);
} else {
Logger.warning('Stream not found: ' + name);
}
}
}
module.exports = HLSServer;