-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.js
executable file
·159 lines (156 loc) · 5.21 KB
/
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
const tcp = require('net');
const Connection = require('./lib/connection');
class RTMPServer extends tcp.Server {
constructor(opts) {
super(opts);
this.channels = new Map();
this.sessions = new Map();
this.producers = {};
this.on('connection', socket => {
const session = new Connection(socket);
session.id = this.generateNewSessionID();
this.emit('client', session);
});
this.on('client', session => {
const { id } = session;
this.sessions.set(id, session);
session.once('end', () => {
this.sessions.delete(id);
this.emit('offline', session);
});
session.on('connect', this.onConnect.bind(this, session));
session.on('audio', this.onAudio.bind(this, session));
session.on('video', this.onVideo.bind(this, session));
session.on('audio-ready', this.onAudioReady.bind(this, session));
session.on('video-ready', this.onVideoReady.bind(this, session));
session.on('publish', this.onPublish.bind(this, session));
session.on('play', this.onPlay.bind(this, session));
session.on('command', this.handleCommand.bind(this, session));
session.on('AMFDataMessage:@setDataFrame:onMetaData', cmd => {
const { streamName } = session;
const producer = this.producers[streamName];
producer.metaData = cmd.cmdObj;
session.startPlay(producer);
});
});
}
generateNewSessionID() {
const SESSION_ID_CHARS = 'abcdefghijklmnopqrstuvwxyz0123456789';
const SESSION_ID_LENGTH = 12;
let sessionId;
do {
sessionId = '';
for (let i = 0; i < SESSION_ID_LENGTH; i++) {
const charIndex = (Math.random() * SESSION_ID_CHARS.length) | 0;
sessionId += SESSION_ID_CHARS.charAt(charIndex);
}
} while (this.sessions.has(sessionId));
return sessionId;
}
onConnect(client, cmd){
Object.assign(client, cmd.cmdObj);
client.windowACK(5000000);
client.setPeerBandwidth(5000000, 2);
client.outChunkSize = 4096;
client.setChunkSize(client.outChunkSize);
client.respondConnect();
}
onAudioReady(client, audio){
const streamName = client.streamName;
const producer = this.producers[streamName];
producer.cacheAudioSequenceBuffer = audio;
for (var id in producer.consumers) {
producer.consumers[id].startPlay(producer);
}
}
onVideoReady(client, video){
const streamName = client.streamName;
const producer = this.producers[streamName];
producer.cacheVideoSequenceBuffer = Buffer.from(video);
for (var id in producer.consumers) {
producer.consumers[id].startPlay(producer);
}
}
onAudio(client, header, audio){
const { streamName } = client;
const producer = this.producers[streamName];
for (var id in producer.consumers) {
producer.consumers[id].pushAudio(audio, header.timestamp);
}
}
onVideo(client, header, video){
const { streamName } = client;
const producer = this.producers[streamName];
for (var id in producer.consumers) {
producer.consumers[id].pushVideo(video, header.timestamp);
}
}
onPlay(client, cmd){
const streamName = client.streamName = client.app + '/' + cmd.streamName;
if (!this.producers[streamName]) {
console.info("[rtmp streamPlay] There's no stream named " + streamName + " is" +
" publushing! Create a producer.");
this.producers[streamName] = {
id: null,
consumers: {}
};
} else if (this.producers[streamName].id == null) {
console.info("[rtmp streamPlay] There's no stream named " + streamName + " is " +
"publushing! But the producer is created.");
} else {
console.info("[rtmp streamPlay] There's a stream named " + streamName + " is " +
"publushing! id=" + this.producers[streamName].id);
}
this.producers[streamName].consumers[client.id] = client;
const producer = this.producers[streamName];
client.respondPlay();
client.startPlay(producer);
}
onPublish(client, cmd){
const streamName = client.streamName = client.app + '/' + cmd.streamName;
if (!this.producers[streamName]) {
this.producers[streamName] = {
id: client.id,
consumers: {}
};
} else if (this.producers[streamName].id == null) {
this.producers[streamName].id = client.id;
} else {
console.warn("[rtmp publish] Already has a stream named " + streamName);
client.respondPublishError();
return;
}
client.respondPublish();
}
handleCommand(client, cmd){
switch (cmd.cmd) {
case 'createStream':
client.respondCreateStream(cmd);
break;
case 'getStreamLength':
break;
case 'closeStream':
client.closeStream();
break;
case 'deleteStream':
client.deleteStream(this);
break;
case 'pause':
client.pauseOrUnpauseStream();
break;
case 'releaseStream':
client.respondReleaseStream();
break;
case 'FCPublish':
client.respondFCPublish();
break;
case 'FCUnpublish':
client.respondFCUnpublish();
break;
default:
console.warn("[rtmp:receive] unknown AMF command: " + cmd.cmd);
return;
}
}
}
module.exports = RTMPServer;