forked from illuspas/Node-Media-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_relay_server.js
More file actions
134 lines (120 loc) · 4.36 KB
/
node_relay_server.js
File metadata and controls
134 lines (120 loc) · 4.36 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
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
//
// Created by Mingliang Chen on 18/3/16.
// illuspas[a]gmail.com
// Copyright (c) 2018 Nodemedia. All rights reserved.
//
const Logger = require('./logger');
const NodeCoreUtils = require('./node_core_utils');
const NodeRelaySession = require('./node_relay_session');
const context = require('./node_core_ctx');
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();
}
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;
}
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() {
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://localhost:${this.config.rtmp.port}/${conf.app}/${conf.name}`;
let session = new NodeRelaySession(conf);
session.id = i;
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);
}
}
}
onPrePlay(id, streamPath, args) {
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://localhost:${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) {
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://localhost:${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();
}
}
stop() {
clearInterval(this.staticCycle);
}
}
module.exports = NodeRelayServer;