-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
165 lines (154 loc) · 6.69 KB
/
index.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
const path = require('path');
const getLength = require("utf8-byte-length")
const net = require('net');
const shell = require('shelljs');
const HashMap = require('hashmap');
const sessions = new HashMap();
const serverName = "NodeJS RTSP server"
const server = net.createServer();
server.on('connection', handleConnection);
server.listen(8554, function () {
console.log('server listening to %j', server.address());
});
function handleConnection(conn) {
var remoteAddress = conn.remoteAddress.replace(/^.*:/, '');
console.log('new client connection from %s', remoteAddress + ':' + conn.remotePort);
conn.on('data', onConnData);
conn.once('close', onConnClose);
conn.on('error', onConnError);
function onConnData(req) {
var tcpString = req.toString('utf8');
console.log('');
console.log('%s', tcpString);
console.log('');
const headers = new HashMap();
var lines = tcpString.split("\r\n");
for (var i = 0, len = lines.length; i < len; i++) {
if (lines[i].includes(": ")) {
headers.set(lines[i].split(": ")[0], lines[i].split(": ")[1]);
}
}
const messageType = lines[0].split(" ")[0];
const contentBase = lines[0].split(" ")[1];
const RTSP_200 = "RTSP/1.0 200 OK\r\n";
const RTSP_501 = "RTSP/1.0 501 Not Implemented\r\n";
var response = "NO RESPONSE SET";
switch (messageType) {
case "RTSP/1.0":
response = RTSP_501;
response += "CSeq: " + headers.get("CSeq") + "\r\n";
response += "Server: " + serverName + "\r\n";
response += rtspDate();
break;
case "OPTIONS":
// OPTIONS rtsp://localhost:8554/live.sdp RTSP/1.0
// CSeq: 1
// User-Agent: Lavf57.83.100
response = RTSP_200;
response += "CSeq: " + headers.get("CSeq") + "\r\n";
response += "Public: OPTIONS, DESCRIBE, PLAY, SETUP, TEARDOWN\r\n";
response += "Server: " + serverName + "\r\n";
response += rtspDate();
break;
case "DESCRIBE":
// DESCRIBE rtsp://localhost:8554/live.sdp RTSP/1.0
// Accept: application / sdp
// CSeq: 2
// User - Agent: Lavf57.83.100
const sdp = generateSdp();
const sdpLengthInBytes = getLength(sdp);
response = RTSP_200
response += "CSeq: " + headers.get("CSeq") + "\r\n";
response += "Content-Type: application/sdp\r\n"
response += "Content-Base: " + contentBase + "/\r\n"
response += "Server: " + serverName + "\r\n"
response += rtspDate();
response += "Content-Length: " + sdpLengthInBytes + "\r\n"
response += sdp;
break;
case "SETUP":
// SETUP rtsp://localhost:8554/live.sdp/stream=0 RTSP/1.0
// Transport: RTP/AVP/UDP;unicast;client_port=23752-23753
// CSeq: 3
// User-Agent: Lavf57.83.100
const clientPorts = headers.get("Transport").split(";")[2].split("=")[1];
const rtpPort = clientPorts.split("-")[0];
const rtcpPort = clientPorts.split("-")[1];
const sessionId = getRandomInt(1, 999999).toString();
sessions.set(sessionId, rtpPort);
console.log("Session RTP Port: " + sessions.get(sessionId));
response = RTSP_200;
response += "CSeq: " + headers.get("CSeq") + "\r\n";
response += "Transport: RTP/AVP;unicast;client_port=" + clientPorts + ";mode=\"PLAY\"\r\n"
response += "Server: " + serverName + "\r\n"
response += "Session: " + sessionId + "\r\n"
response += rtspDate();
break;
case "PLAY":
// PLAY rtsp://localhost:8554/live.sdp/ RTSP/1.0
// Range: npt=0.000-
// CSeq: 4
// User-Agent: Lavf57.83.100
// Session: 12345678
const streamIdentifer = contentBase.split("://")[1].split("/")[1];
response = RTSP_200
response += "CSeq: " + headers.get("CSeq") + "\r\n";
response += "RTP-Info: url=" + contentBase + "stream=0;seq=1;rtptime=0\r\n"
response += "Range: npt=0-\r\n"
response += "Server: " + serverName + "\r\n"
response += "Session: " + headers.get("Session") + "\r\n"
response += rtspDate();
const scriptCmd = "gst-launch-1.0 -v videotestsrc pattern=" + streamIdentifer + " ! " +
"video/x-raw,framerate=30/1 ! videoconvert ! " +
"x264enc tune=zerolatency ! rtph264pay ! " +
"udpsink host=" + remoteAddress + " port=" + sessions.get(headers.get("Session"))
console.log(scriptCmd);
shell.exec(scriptCmd,
{ async: true, silent: true });
break;
case "TEARDOWN":
// TEARDOWN rtsp://localhost:8554/live.sdp/ RTSP/1.0
// CSeq: 5
// User-Agent: Lavf57.83.100
// Session: 12345678
response = RTSP_200
response += "CSeq: " + headers.get("CSeq") + "\r\n";
response += "Server: " + serverName + "\r\n"
response += "Session: " + headers.get("Session") + "\r\n"
response += "Connection: close\r\n"
response += rtspDate();
break;
default:
response = RTSP_501;
response += "CSeq: " + headers.get("CSeq") + "\r\n";
response += "Server: " + serverName + "\r\n";
response += rtspDate();
break;
}
console.log(response);
conn.write(response + "\r\n");
}
function onConnClose() {
console.log('connection from %s closed', remoteAddress);
}
function onConnError(err) {
console.log('Connection %s error: %s', remoteAddress, err.message);
}
}
function generateSdp() {
var sdp = "\r\n"
sdp += "v=0\r\n"
sdp += "s=" + serverName + "\r\n"
sdp += "t=0 0\r\n"
sdp += "m=video 0 RTP/AVP 96\r\n"
sdp += "a=rtpmap:96 H264/90000\r\n"
return sdp;
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function rtspDate() {
return "Date: " + new Date().toGMTString() + "\r\n";
}