-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
246 lines (193 loc) · 7.34 KB
/
Copy pathindex.js
File metadata and controls
246 lines (193 loc) · 7.34 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
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
// // // // // server.js
// // // // import express from 'express';
// // // // import { WebSocketServer } from 'ws';
// // // // import { spawn } from 'child_process';
// // // // const app = express();
// // // // const port = 5000;
// // // // // Basic route
// // // // app.get('/', (req, res) => {
// // // // res.send('WebRTC RTSP server running');
// // // // });
// // // // const server = app.listen(port, () => {
// // // // console.log(`HTTP server running at http://localhost:${port}`);
// // // // });
// // // // // WebSocket server for signaling
// // // // const wss = new WebSocketServer({ server });
// // // // wss.on('connection', (ws) => {
// // // // console.log('Client connected for WebRTC signaling');
// // // // ws.on('message', (message) => {
// // // // const data = JSON.parse(message);
// // // // if (data.type === 'start') {
// // // // const rtspUrl = data.url; // RTSP URL sent from client
// // // // console.log('Starting streaming for', rtspUrl);
// // // // // Spawn FFmpeg to convert RTSP → WebRTC-friendly format (RTP/H.264)
// // // // const ffmpeg = spawn('ffmpeg', [
// // // // '-i', rtspUrl,
// // // // '-f', 'rtp', // send RTP
// // // // '-payload_type', '96',
// // // // 'rtp://127.0.0.1:5004' // send RTP to local WebRTC pipeline
// // // // ]);
// // // // ffmpeg.stderr.on('data', (data) => {
// // // // console.log(`FFmpeg: ${data}`);
// // // // });
// // // // ffmpeg.on('close', (code) => {
// // // // console.log(`FFmpeg exited with code ${code}`);
// // // // });
// // // // }
// // // // });
// // // // });
// // // // ws-rtsp-proxy.js
// // // import WebSocket, { WebSocketServer } from 'ws';
// // // import { spawn } from 'child_process';
// // // const wss = new WebSocketServer({ port: 8080 });
// // // wss.on('connection', (ws, req) => {
// // // console.log('Client connected');
// // // // You can send the RTSP URL from the client, or hardcode it
// // // const rtspUrl = "rtsp://rtspViewer:rtspViewerPass321@13.201.247.124:8554/synqkriya1";
// // // // FFmpeg: RTSP → fragmented MP4 → stdout
// // // // FFmpeg: RTSP → fragmented MP4 → stdout
// // // const ffmpeg = spawn('ffmpeg', [
// // // '-rtsp_transport', 'tcp',
// // // '-i', rtspUrl,
// // // '-an', // no audio
// // // '-c:v', 'copy', // copy video codec instead of re-encoding
// // // '-f', 'mp4',
// // // '-movflags', 'frag_keyframe+empty_moov',
// // // 'pipe:1'
// // // ]);
// // // ffmpeg.stdout.on('data', (chunk) => {
// // // if (ws.readyState === WebSocket.OPEN) {
// // // ws.send(chunk);
// // // }
// // // });
// // // ffmpeg.stderr.on('data', (data) => {
// // // console.log(`FFmpeg: ${data}`);
// // // });
// // // ffmpeg.on('close', () => {
// // // console.log('FFmpeg process closed');
// // // });
// // // ws.on('close', () => {
// // // console.log('Client disconnected');
// // // ffmpeg.kill('SIGINT');
// // // });
// // // });
// // // console.log('WebSocket RTSP proxy running on ws://localhost:8080');
// // import WebSocket, { WebSocketServer } from 'ws';
// // import { spawn } from 'child_process';
// // import ffmpegPath from 'ffmpeg-static'; // ✅ ffmpeg binary bundled with npm
// // const wss = new WebSocketServer({ port: 8080 });
// // wss.on('connection', (ws, req) => {
// // console.log('Client connected');
// // // RTSP URL (hardcoded for now, can be sent from client later)
// // const rtspUrl = "rtsp://rtspviewer1:rtspviewerpass1@13.201.247.124:8554/sherbrooke";
// // // FFmpeg: RTSP → fragmented MP4 → stdout
// // // const ffmpeg = spawn(ffmpegPath, [
// // // '-rtsp_transport', 'tcp',
// // // '-i', rtspUrl,
// // // '-an', // drop audio
// // // '-c:v', 'copy', // copy video codec (no re-encode)
// // // '-f', 'mp4',
// // // '-movflags', 'frag_keyframe+empty_moov',
// // // 'pipe:1'
// // // ]);
// // const ffmpeg = spawn('ffmpeg', [
// // '-rtsp_transport', 'tcp',
// // '-i', rtspUrl,
// // '-an',
// // '-c:v', 'libx264', // ✅ re-encode to H.264
// // '-preset', 'ultrafast', // ✅ low latency
// // '-tune', 'zerolatency',
// // '-f', 'mp4',
// // '-movflags', 'frag_keyframe+empty_moov',
// // 'pipe:1'
// // ]);
// // // Send FFmpeg output chunks to WebSocket
// // ffmpeg.stdout.on('data', (chunk) => {
// // if (ws.readyState === WebSocket.OPEN) {
// // ws.send(chunk);
// // }
// // });
// // // Log FFmpeg status/errors
// // ffmpeg.stderr.on('data', (data) => {
// // console.log(`FFmpeg: ${data}`);
// // });
// // ffmpeg.on('close', (code) => {
// // console.log(`FFmpeg process closed with code ${code}`);
// // });
// // // Kill FFmpeg when client disconnects
// // ws.on('close', () => {
// // console.log('Client disconnected');
// // ffmpeg.kill('SIGINT');
// // });
// // });
// // console.log('WebSocket RTSP proxy running on ws://localhost:8080');
// import WebSocket, { WebSocketServer } from 'ws';
// import { spawn } from 'child_process';
// import ffmpegPath from 'ffmpeg-static'; // ✅ use static ffmpeg binary
// const wss = new WebSocketServer({ port: 8080 });
// wss.on('connection', (ws, req) => {
// console.log('Client connected');
// const rtspUrl = "rtsp://rtspviewer1:rtspviewerpass1@13.201.247.124:8554/sherbrooke";
// const ffmpeg = spawn(ffmpegPath, [ // ✅ use ffmpegPath here
// '-rtsp_transport', 'tcp',
// '-i', rtspUrl,
// '-an',
// '-c:v', 'libx264',
// '-preset', 'ultrafast',
// '-tune', 'zerolatency',
// '-f', 'mp4',
// '-movflags', 'frag_keyframe+empty_moov',
// 'pipe:1'
// ]);
// ffmpeg.stdout.on('data', (chunk) => {
// if (ws.readyState === WebSocket.OPEN) {
// ws.send(chunk);
// }
// });
// ffmpeg.stderr.on('data', (data) => {
// console.log(`FFmpeg: ${data}`);
// });
// ffmpeg.on('close', (code) => {
// console.log(`FFmpeg process closed with code ${code}`);
// });
// ws.on('close', () => {
// console.log('Client disconnected');
// ffmpeg.kill('SIGINT');
// });
// });
// console.log('WebSocket RTSP proxy running on ws://localhost:8080');
import { spawn } from "child_process";
import ffmpegPath from "ffmpeg-static";
import WebSocket, { WebSocketServer } from "ws";
const wss = new WebSocketServer({ port: 8080 });
console.log("WebSocket RTSP proxy running on ws://localhost:8080");
wss.on("connection", (ws) => {
console.log("Client connected");
const ffmpeg = spawn(ffmpegPath, [
"-rtsp_transport", "tcp",
"-i", "rtsp://rtspviewer1:rtspviewerpass1@13.201.247.124:8554/sherbrooke",
"-an",
"-c:v", "libx264",
"-preset", "ultrafast",
"-tune", "zerolatency",
"-f", "mp4",
"-movflags", "frag_keyframe+empty_moov+default_base_moof", // ✅ very important
"pipe:1"
]);
ffmpeg.stdout.on("data", (data) => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(data);
}
});
ffmpeg.stderr.on("data", (data) => {
console.log("FFmpeg:", data.toString());
});
ffmpeg.on("close", () => {
console.log("FFmpeg process closed");
ws.close();
});
ws.on("close", () => {
console.log("Client disconnected");
ffmpeg.kill("SIGINT");
});
});