forked from li-yanhao/janus-webrtc-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
572 lines (522 loc) · 17.4 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// server.js
var express = require('express');
var axios = require('axios');
var app = express();
var PORT = 3000;
var {spawn} = require('child_process');
var fs = require('fs')
var bodyParser = require("body-parser")
var janusHost = "http://0.0.0.0:8088/janus"
var adminHost = "http://0.0.0.0:7088/admin"
var ffmpegHost = "127.0.0.1"
var rtmpHost = "127.0.0.1"
var rtmpStreamPort = 1935
const sessionId = 1;
var handlerId = null;
var publisherId = 5035925950
var roomId = 4321
var pin = '1234'
var audioPort = 10033
var videoPort = 10038
var streamName = 'nihao'
var admin_key = 'supersecret'
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*"); // update to match the domain you will make the request from
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "*");
next();
})
app.listen(PORT, function () {
console.log('Server is running on PORT:', PORT);
});
/**
* Create a session to serve a publisher.
*/
async function createSession(config) {
await axios.post(janusHost, {
janus: "create",
transaction: "create session",
id: config.sessionId,
apisecret: "janusrocks"
}).then(res => {
if (res.data.janus == 'success')
config.sessionId = res.data.data.id;
else
console.log(res.data.error.reason)
})
return config
}
/**
* Attach a videoroom handler to a session to handle the publisher's requests and events.
*/
async function attachHandler(config) {
await axios.post(janusHost + '/' + config.sessionId, {
janus: "attach",
transaction: "attach videoroom plugin for session " + config.sessionId,
plugin: "janus.plugin.videoroom",
apisecret: "janusrocks"
}).then(res => {
switch (res.data.janus) {
case "error":
console.log("janus error: " + body.error.reason);
break;
case "success":
config.handlerId = res.data.data.id;
break;
default: console.log("janus message is not identified")
}
})
return config
}
/**
* List the ids of all the existing rooms. If room is set "private" it will not be listed.
*/
async function listRoomId() {
var roomIds = [];
await axios.post(janusHost + '/' + sessionId + '/' + handlerId, {
janus: "message",
transaction: "list room Ids",
apisecret: "janusrocks",
body: {
request: "list"
}
}).then(res => {
roomList = res.data.plugindata.data.list
for (var i in roomList) {
roomIds.push(roomList[i].room)
}
})
return roomIds;
}
/**
* List all the participants of given rooms.
*/
function listParticipants(roomIds) {
return new Promise((resolve, reject) => {
var rooms_participants = []
requests = []
for (var i in roomIds) {
requests.push(
axios.post(janusHost + '/' + sessionId + '/' + handlerId, {
janus: "message",
transaction: "list room Ids",
apisecret: "janusrocks",
body: {
request: "listparticipants",
room: roomIds[i]
}
}).then(res => {
rooms_participants.push(parseRoomParticipants(res))
})
)
}
axios.all(requests).then(() => {
resolve(rooms_participants)
})
})
}
/**
* Parse the response of "listparticipants" request and return
* an array of participants in the room.
* @param {*} respond JSON response of the "listparticipants" request
*/
function parseRoomParticipants(respond) {
data = respond.data.plugindata.data
room_participants = { room: data.room, participants: [] }
for (var i in data.participants) {
room_participants.participants.push({
id: data.participants[i].id,
publisher: data.participants[i].publisher
})
}
return room_participants
}
/**
* Create a rtp forwarder on Janus to forward the stream of a publisher.
*/
async function createForwarder(config /*[roomId, publisherId]*/) {
audio_port = 10033
audio_pt = 111
video_port = 10038
video_pt = 96
await axios.post(janusHost + '/' + config.sessionId + '/' + config.handlerId, {
janus: "message",
transaction: "rtp forward from " + publisherId + " in room " + config.roomId,
apisecret: "janusrocks",
body: {
request: "rtp_forward",
room: config.roomId,
"publisher_id": publisherId,
"host": ffmpegHost,
"audio_port": audio_port,
"audio_pt": audio_pt,
"video_port": video_port,
"video_pt": video_pt,
"secret": "secret"
}
}).then(res => {
console.log("rtp_forward response: " + JSON.stringify(res.data))
})
return config
}
/**
* Parse the response of "listforwarders" request and return an array
* of stream ids of a given publisher id.
* @param {*} response JSON response of a "listforwarders" request
* @param {*} publisherId The id of the publisher
*/
function parseForwarders(response, publisherId) {
var forwarders = response.data.plugindata.data.rtp_forwarders
console.log(response.data.plugindata.data)
forwarder_raw = forwarders.filter(data => {
return data.publisher_id == publisherId
})
var forwarder
if (forwarder_raw.length > 0) {
forwarder = forwarder_raw[0].rtp_forwarder
}
var streams = []
for (var i in forwarder) {
if (forwarder[i].hasOwnProperty("audio_stream_id")) {
streams.push(forwarder[i].audio_stream_id)
}
else if (forwarder[i].hasOwnProperty("video_stream_id")) {
streams.push(forwarder[i].video_stream_id)
}
}
return streams
}
/**
* Remove all the rtp forwarders of a publisher given his
* id and room id.
*/
function clearForwarders([roomId, publisherId]) {
var listForwarders = new Promise((resolve, reject) => {
axios.post(janusHost + '/' + sessionId + '/' + handlerId, {
janus: "message",
transaction: "list all rtp forwards",
apisecret: "janusrocks",
body: {
request: "listforwarders",
room: roomId,
secret: "secret"
}
}).then(res => {
var streams = parseForwarders(res, publisherId)
resolve(streams)
})
})
var removeForwarders = streams => {
return new Promise((resolve, reject) => {
var requests = []
for (var i in streams) {
requests.push(axios.post(
janusHost + '/' + sessionId + '/' + handlerId, {
janus: "message",
transaction: "remove rtp forward",
apisecret: "janusrocks",
body: {
request: "stop_rtp_forward",
room: roomId,
publisher_id: publisherId,
stream_id: streams[i],
secret: "secret"
}
})
)
}
axios.all(requests).then(res => {
// console.log(res)
})
resolve([roomId, publisherId])
})
}
return listForwarders.then(removeForwarders);
}
/**
* Generate an SDP file for FFmpeg to receive an rtp stream
* from Janus.
* @param {*} nodeStreamIp The IP address of FFmpeg
* @param {*} audioPort The port for receiving audio stream
* @param {*} videoPort The port for receiving video stream
*/
function generateSdpStreamConfig(nodeStreamIp, audioPort, videoPort) {
var sdpRtpOfferString = 'v=0\n';
sdpRtpOfferString += 'o=- 0 0 IN IP4 ' + nodeStreamIp + '\n';
sdpRtpOfferString += 's=RTP Video\n';
sdpRtpOfferString += 'c=IN IP4 ' + nodeStreamIp + '\n';
sdpRtpOfferString += 't=0 0\n';
sdpRtpOfferString += 'm=audio ' + audioPort + ' RTP/AVP 111\n';
sdpRtpOfferString += 'a=rtpmap:111 OPUS/48000/2\n';
sdpRtpOfferString += 'm=video ' + videoPort + ' RTP/AVP 96\n';
sdpRtpOfferString += 'a=rtpmap:96 H264/90000\n';
fs.writeFileSync('v' + videoPort + '_a' + audioPort + '.sdp', sdpRtpOfferString);
}
/**
* Generate a config of FFmpeg as command options.
* @param {*} name The name of the rtmp stream on Nginx-rtmp server
*/
var ffmpeg_args = (name) => {
return [
'-analyzeduration', '300M', '-probesize', '300M',
'-protocol_whitelist', 'file,udp,rtp',
'-i', 'v' + videoPort + '_a' + audioPort + '.sdp',
'-c:v', 'copy', '-c:a', 'aac', '-ar', '16k', '-ac', '1',
// '-preset', 'ultrafast',
'-tune', 'zerolatency',
'-f', 'flv', 'rtmp://' + rtmpHost + ':' + rtmpStreamPort + '/stream/' + name
];
};
/**
* Launch a FFmpeg process to transcode rtp to rtmp.
* @param {*} nodeStreamIp The IP address of FFmpeg
* @param {*} audioPort The port for receiving audio stream
* @param {*} videoPort The port for receiving video stream
* @param {*} streamName The name of the rtmp stream on Nginx-rtmp server
*/
async function createFFmpeg(config) {
generateSdpStreamConfig(config.nodeStreamIp, config.audioPort, config.videoPort)
console.log("ffmpeg args: \n" + ffmpeg_args(config.streamName))
var child = spawn('ffmpeg', ffmpeg_args(config.streamName));
config.ffmpegProcess = child;
child.stderr.on('data', function (data) {
console.log("ffmpeg log : " + data)
});
return config;
}
/**
* Update the global parameters "audioPort", "videoPort" and "roomId".
* (for test use, will be deprecated)
*/
function updateConfig() {
audioPort--;
videoPort++;
roomId++;
}
async function startForwarding(config /*roomId, publisherId*/) {
createSession().then(attachHandler)
.then(() => { return new Promise(resolve => resolve([config.roomId, publisherId])) })
// .then(clearForwarders)
.then(createForwarder)
.then(res => console.log(JSON.stringify(res)))
}
async function createRoom(config /*roomId, pin, secret, token */) {
await axios.post(janusHost + '/' + config.sessionId + '/' + config.handlerId, {
janus: "message",
transaction: "create room of id " + config.roomId +
" with pin " + config.pin + " and secret " + config.secret,
apisecret: "janusrocks",
body: {
request: "create",
room: config.roomId,
secret: config.secret,
pin: config.pin,
is_private: false,
bitrate: 12800000,
fir_freq: 1,
videocodec: "h264",
record: false,
videoorient_ext: true,
playoutdelay_ext: true,
// allowed: [adminToken, config.token]
admin_key: 'supersecret'
},
}).then(res => {
console.log(res.data)
console.log()
})
return config
}
async function destroyRoom(config /*roomId, secret*/) {
axios.post(janusHost + '/' + config.sessionId + '/' + config.handlerId, {
janus: "message",
transaction: "destroy room of id " + config.roomId,
apisecret: "janusrocks",
body: {
request: "destroy",
room: config.roomId,
secret: config.secret
},
}).then(res => {
console.log(res.data)
console.log()
})
}
async function joinRoom(config /*roomId, pin*/) {
await axios.post(janusHost + '/' + config.sessionId + '/' + config.handlerId, {
janus: "message",
transaction: "join the room of id " + config.roomId + " as room holder",
apisecret: "janusrocks",
body: {
request: "join",
ptype: "publisher",
room: config.roomId,
id: 1234,
pin: config.pin,
},
})
return config
}
/**
* a specific event listener to wait for the publisher to join
*/
async function waitPublisher(publisherId) {
var maxev = 5;
var longpoll = janusHost + '/' + sessionId + '?rid=' + new Date().getTime();
longpoll = longpoll + '&maxev=' + 5;
var joined = false;
// Parse the event message and check if the publisher has joined the room
var parseJoined = json => {
try {
if (json.plugindata.data.publishers.filter(data => {
console.log(data.id == publisherId)
return data.id == publisherId
}).length > 0) {
console.log("parsed ! ")
console.log(json.plugindata.data.publishers)
return true
}
else
return false
} catch (e) {
console.log("non parsed :(")
return false
}
}
var cn = 1
while (!joined) {
await axios.get(longpoll).then(res => {
console.log('count = ' + cn++)
console.log(JSON.stringify(res.data))
for (var i in res.data) {
if (parseJoined(res.data[i])) {
joined = true;
break;
}
}
})
}
}
async function listenEvent(config) {
var longpoll = janusHost + '/' + config.sessionId + '?rid=' + new Date().getTime();
longpoll = longpoll + '&maxev=' + 5;
while (config.sessionId !== undefined && config.sessionId !== null) {
await axios.get(longpoll).then(res => {
for (var i in res.data) {
console.log(res.data[i])
}
handleEvent(res.data, config);
})
}
console.log("\n ### ListenEvent completed :) ### \n")
return config
}
async function handleEvent(json, config) {
for (var i in json) {
switch (json[i].janus) {
case 'event':
handleVideoroomEvent(json[i].plugindata.data, config)
case 'keepalive':
break;
default:
console.log("handleEvent: unknown event")
}
}
}
async function handleVideoroomEvent(json, config) {
switch (json.videoroom) {
case 'event':
// some videoroom room events
if (json.hasOwnProperty('leaving')) {
if (json.leaving == publisherId) {
// then destroy the room
// TODO
config.ffmpegProcess.kill();
config.ffmpegProcess = null;
await destroyRoom(config)
config.leaving = true;
config.sessionId = null;
}
}
break;
case 'joined':
// admin joined the room
break;
case 'destroyed':
// room has been destroyed, stop listening.
config.roomId = null;
config.sessionId = null;
break;
default:
console.log("Something weird in videoroom plugin event")
break;
}
}
var responseConfig = (requestConfig) => {
var resConfig;
if (requestConfig.login == "yanhao" && requestConfig.passwd == '1234') {
resConfig = {
status: "success",
key: {
room: requestConfig.roomid,
pin: pin,
id: publisherId
}
};
}
else
resConfig = {
status: "error",
reason: "Authentification failed"
}
return resConfig
}
/** *-* Processing of a client request:
* 1. Create new session/handler to manage client Alice (done)
* 2. Create a room for Alice with a pin (for Alice to join) and a secret (done)
* 3. Create a token for Alice and add the token to the whitelist of the room. (TODO)
* 4. Join the room. (done)
* 5. Set eventHandler() to listen any event from the room => event:
* 'keep alive':
* 'joined':
* 'leave': TODO
* 'destroyed': TODO
* 'error': TODO
* 6. create id for Alice and send pin and id to Alice (done)
* 7. wait for the joined event of Alice (done)
* 8. start rtp forwarder and ffmpeg transcoder (done)
* 9. Wait for some unknown requests ... then handle them !
* 9. wait for Alice to leave
* 10. destroy the room (done)
*
*/
app.post('/stream', async function (req, res) {
console.log(req.body)
var resConfig = responseConfig(req.body)
var roomId = resConfig.key.room
var adminConfig = {
sessionId: 1, handlerId: null,
roomId: roomId, pin: pin, secret: 'secret', token: null,
nodeStreamIp: ffmpegHost, audioPort: audioPort, videoPort: videoPort, streamName: "nihao",
ffmpegProcess: null
};
if (resConfig.status == 'success') {
await createSession(adminConfig)
.then(attachHandler)
.then(createRoom)
.then(joinRoom);
res.status(201).send(resConfig);
await waitPublisher(publisherId);
console.log(JSON.stringify(adminConfig))
await createForwarder(adminConfig);
createFFmpeg(adminConfig);
// updateConfig(); // update audioPort, videoPort and roomId config
listenEvent(adminConfig);
}
else {
res.status(500).send(resConfig);
}
// var allowed = await waitPublisher(publisherId)
});