forked from illuspas/Node-Media-Server
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_relay_session.js
78 lines (67 loc) · 2.13 KB
/
node_relay_session.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
78
//
// Created by Mingliang Chen on 18/3/16.
// illuspas[a]gmail.com
// Copyright (c) 2018 Nodemedia. All rights reserved.
//
const Logger = require('./node_core_logger');
const EventEmitter = require('events');
const { spawn } = require('child_process');
const RTSP_TRANSPORT = ['udp', 'tcp', 'udp_multicast', 'http'];
class NodeRelaySession extends EventEmitter {
constructor(conf) {
super();
this.conf = conf;
}
run() {
let format = this.conf.ouPath.startsWith('rtsp://') ? 'rtsp' : 'flv';
let argv = ['-fflags', 'nobuffer', '-i', this.conf.inPath, '-c', 'copy', '-f', format, this.conf.ouPath,
'-stimeout', '10000000 ','-reconnect', '1', '-reconnect_at_eof', '1', '-reconnect_streamed', '1', '-reconnect_delay_max', '2']
if (this.conf.inPath[0] === '/' || this.conf.inPath[1] === ':') {
argv.unshift('-1');
argv.unshift('-stream_loop');
argv.unshift('-re');
}
if (this.conf.inPath.startsWith('rtsp://') && this.conf.rtsp_transport) {
if (RTSP_TRANSPORT.indexOf(this.conf.rtsp_transport) > -1) {
argv.unshift(this.conf.rtsp_transport);
argv.unshift('-rtsp_transport');
}
}
Logger.ffdebug(argv.toString());
this.ffmpeg_exec = spawn(this.conf.ffmpeg, argv);
this.ffmpeg_exec.on('error', (e) => {
Logger.ffdebug(e);
});
this.ffmpeg_exec.stdout.on('data', (data) => {
Logger.ffdebug(`FF输出:${data}`);
});
this.ffmpeg_exec.stderr.on('data', (data) => {
Logger.ffdebug(`FF输出:${data}`);
});
this.ffmpeg_exec.on('close', (code) => {
Logger.log('[Relay end] id=', this.id);
this.ffmpeg_exec = null;
if (!this._ended) {
this._runtimeout = setTimeout(() => {
this._runtimeout = null;
this.run();
}, 1000);
} else {
this.emit('end', this.id);
}
});
}
end() {
this._ended = true;
if (this._runtimeout != null) {
clearTimeout(this._runtimeout);
this._runtimeout = null;
}
if (this.ffmpeg_exec) {
this.ffmpeg_exec.kill();
} else {
this.emit('end', this.id);
}
}
}
module.exports = NodeRelaySession;