forked from illuspas/Node-Media-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_relay_server.js
193 lines (177 loc) · 6.38 KB
/
node_relay_server.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//
// 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 NodeCoreUtils = require('./node_core_utils');
const NodeRelaySession = require('./node_relay_session');
const context = require('./node_core_ctx');
const { getFFmpegVersion, getFFmpegUrl } = require('./node_core_utils');
const fs = require('fs');
const _ = require('lodash');
class NodeRelayServer {
constructor(config) {
this.config = config;
this.staticCycle = null;
this.staticSessions = new Map();
this.dynamicSessions = new Map();
}
async run() {
try {
fs.accessSync(this.config.relay.ffmpeg, fs.constants.X_OK);
} catch (error) {
Logger.error(`Node Media Relay Server startup failed. ffmpeg:${this.config.relay.ffmpeg} cannot be executed.`);
return;
}
let version = await getFFmpegVersion(this.config.relay.ffmpeg);
if (version === '' || parseInt(version.split('.')[0]) < 4) {
Logger.error('Node Media Relay Server startup failed. ffmpeg requires version 4.0.0 above');
Logger.error('Download the latest ffmpeg static program:', getFFmpegUrl());
return;
}
context.nodeEvent.on('relayPull', this.onRelayPull.bind(this));
context.nodeEvent.on('relayPush', this.onRelayPush.bind(this));
context.nodeEvent.on('prePlay', this.onPrePlay.bind(this));
context.nodeEvent.on('donePlay', this.onDonePlay.bind(this));
context.nodeEvent.on('postPublish', this.onPostPublish.bind(this));
context.nodeEvent.on('donePublish', this.onDonePublish.bind(this));
this.staticCycle = setInterval(this.onStatic.bind(this), 1000);
Logger.log('Node Media Relay Server started');
}
onStatic() {
if (!this.config.relay.tasks) {
return;
}
let i = this.config.relay.tasks.length;
while (i--) {
if (this.staticSessions.has(i)) {
continue;
}
let conf = this.config.relay.tasks[i];
let isStatic = conf.mode === 'static';
if (isStatic) {
conf.name = conf.name ? conf.name : NodeCoreUtils.genRandomName();
conf.ffmpeg = this.config.relay.ffmpeg;
conf.inPath = conf.edge;
conf.ouPath = `rtmp://127.0.0.1:${this.config.rtmp.port}/${conf.app}/${conf.name}`;
let session = new NodeRelaySession(conf);
session.id = i;
session.streamPath = `/${conf.app}/${conf.name}`;
session.on('end', (id) => {
this.staticSessions.delete(id);
});
this.staticSessions.set(i, session);
session.run();
Logger.log('[Relay static pull] start', i, conf.inPath, ' to ', conf.ouPath);
}
}
}
//从远端拉推到本地
onRelayPull(url, app, name) {
let conf = {};
conf.ffmpeg = this.config.relay.ffmpeg;
conf.inPath = url;
conf.ouPath = `rtmp://127.0.0.1:${this.config.rtmp.port}/${app}/${name}`;
let id = NodeCoreUtils.generateNewSessionID();
let session = new NodeRelaySession(conf);
session.id = id;
session.on('end', (id) => {
this.dynamicSessions.delete(id);
});
this.dynamicSessions.set(id, session);
session.run();
Logger.log('[Relay dynamic pull] start', id, conf.inPath, ' to ', conf.ouPath);
return id;
}
//从本地拉推到远端
onRelayPush(url, app, name) {
let conf = {};
conf.ffmpeg = this.config.relay.ffmpeg;
conf.inPath = `rtmp://127.0.0.1:${this.config.rtmp.port}/${app}/${name}`;
conf.ouPath = url;
let id = NodeCoreUtils.generateNewSessionID();
let session = new NodeRelaySession(conf);
session.id = id;
session.on('end', (id) => {
this.dynamicSessions.delete(id);
});
this.dynamicSessions.set(id, session);
session.run();
Logger.log('[Relay dynamic push] start', id, conf.inPath, ' to ', conf.ouPath);
}
onPrePlay(id, streamPath, args) {
if (!this.config.relay.tasks) {
return;
}
let regRes = /\/(.*)\/(.*)/gi.exec(streamPath);
let [app, stream] = _.slice(regRes, 1);
let i = this.config.relay.tasks.length;
while (i--) {
let conf = this.config.relay.tasks[i];
let isPull = conf.mode === 'pull';
if (isPull && app === conf.app && !context.publishers.has(streamPath)) {
let hasApp = conf.edge.match(/rtmp:\/\/([^\/]+)\/([^\/]+)/);
conf.ffmpeg = this.config.relay.ffmpeg;
conf.inPath = hasApp ? `${conf.edge}/${stream}` : `${conf.edge}${streamPath}`;
conf.ouPath = `rtmp://127.0.0.1:${this.config.rtmp.port}${streamPath}`;
let session = new NodeRelaySession(conf);
session.id = id;
session.on('end', (id) => {
this.dynamicSessions.delete(id);
});
this.dynamicSessions.set(id, session);
session.run();
Logger.log('[Relay dynamic pull] start', id, conf.inPath, ' to ', conf.ouPath);
}
}
}
onDonePlay(id, streamPath, args) {
let session = this.dynamicSessions.get(id);
let publisher = context.sessions.get(context.publishers.get(streamPath));
if (session && publisher.players.size == 0) {
session.end();
}
}
onPostPublish(id, streamPath, args) {
if (!this.config.relay.tasks) {
return;
}
let regRes = /\/(.*)\/(.*)/gi.exec(streamPath);
let [app, stream] = _.slice(regRes, 1);
let i = this.config.relay.tasks.length;
while (i--) {
let conf = this.config.relay.tasks[i];
let isPush = conf.mode === 'push';
if (isPush && app === conf.app) {
let hasApp = conf.edge.match(/rtmp:\/\/([^\/]+)\/([^\/]+)/);
conf.ffmpeg = this.config.relay.ffmpeg;
conf.inPath = `rtmp://127.0.0.1:${this.config.rtmp.port}${streamPath}`;
conf.ouPath = hasApp ? `${conf.edge}/${stream}` : `${conf.edge}${streamPath}`;
let session = new NodeRelaySession(conf);
session.id = id;
session.on('end', (id) => {
this.dynamicSessions.delete(id);
});
this.dynamicSessions.set(id, session);
session.run();
Logger.log('[Relay dynamic push] start', id, conf.inPath, ' to ', conf.ouPath);
}
}
}
onDonePublish(id, streamPath, args) {
let session = this.dynamicSessions.get(id);
if (session) {
session.end();
}
for (session of this.staticSessions.values()) {
if (session.streamPath === streamPath) {
session.end();
}
}
}
stop() {
clearInterval(this.staticCycle);
}
}
module.exports = NodeRelayServer;