-
Notifications
You must be signed in to change notification settings - Fork 31
/
example.js
91 lines (81 loc) · 2.52 KB
/
example.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
'use strict';
//process.env.DEBUG = "mediasoup*";
const fs = require('fs');
const ip = require('ip');
const path = require('path');
const {WebRtcServer, RtspServer, ffmpeg} = require('./index.js');
const recordingsPath = path.join(__dirname, "recordings");
const ipAddress = ip.address();
const streamer = new ffmpeg({
enableDebug: true
});
const webRtcServer = new WebRtcServer();
webRtcServer
.listen({
enableDebug: true,
key: fs.readFileSync('keys/server.key'),
cert: fs.readFileSync('keys/server.crt'),
port: 3888,
path: 'public',
})
.on('listen', () => {
console.log('Mediasoup demo started');
})
.on('web-listen', (port) => {
console.log(`Open https://${ipAddress}:${port} with browser`);
})
.on('new-connection', (connection) => {
console.log(`New connection [${connection.id}]`);
connection
.on('error', (err) => {
console.log(`Connection [${connection.id}] error: ${err}`);
})
.on('receive', (action, data) => {
console.log(`Connection [${connection.id}] receive [${action}]`);
})
.on('send', (action, data) => {
console.log(`Connection [${connection.id}] send [${action}]`);
})
.on('new-stream', (stream) => {
console.log(`Connection [${connection.id}] peer [${stream.peer.id}] new stream [${stream.id}]`);
})
.on('ready', (peerConnection) => {
console.log(`Connection [${connection.id}] peer [${peerConnection.peer.id}] ready`);
})
.on('close', (peerId) => {
console.log(`Connection [${connection.id}] peer [${peerId}] closed`);
})
.on('disconnect', (err) => {
console.log(`Connection [${connection.id}] signaling disconnected`);
connection = null;
});
});
const rtspServer = new RtspServer(webRtcServer);
rtspServer
.listen(5000)
.on('listen', (port) => {
console.log(`RTSP server started rtsp://${ipAddress}:${port}`);
})
.on('new-source', (source) => {
let rtspUrl = `rtsp://${ipAddress}:${rtspServer.port}/${source.id}.sdp`;
console.log(`New RTSP source ${rtspUrl}`);
let process;
source.on('enabled', () => {
let filepath = `${recordingsPath}/${source.id}.mp4`;
let logpath = `${recordingsPath}/${source.id}.log`;
console.log(`Recording [${source.id}]: ${filepath}`);
process = streamer.record(rtspUrl, filepath, logpath)
.on('error', (err) => {
console.error(`Streamer [${source.id}] error: ${err}`);
})
.on('exit', (code, signal) => {
console.log(`Streamer [${source.id}] closed, log: ${logpath}`);
});
})
.on('error', (err) => {
console.error(`RTSP Source [${source.id}] error:`, err);
});
})
.on('request', (method, uri) => {
console.log(`RTSP Request [${method}]`, uri);
});