Spaces:
Paused
Paused
Update server.js
Browse files
server.js
CHANGED
|
@@ -5,14 +5,10 @@ import cors from "cors";
|
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
app.use(cors());
|
| 8 |
-
app.use(express.text({ type: 'application/sdp' }));
|
| 9 |
app.use(express.json());
|
| 10 |
|
| 11 |
console.log("π Starting mediasoup WHIP/WHEP server...");
|
| 12 |
|
| 13 |
-
// Get announced IP from environment or use public IP detection
|
| 14 |
-
const ANNOUNCED_IP = process.env.ANNOUNCED_IP || null;
|
| 15 |
-
|
| 16 |
// Create mediasoup worker
|
| 17 |
const worker = await mediasoup.createWorker({
|
| 18 |
logLevel: "warn",
|
|
@@ -35,47 +31,37 @@ const router = await worker.createRouter({
|
|
| 35 |
kind: "video",
|
| 36 |
mimeType: "video/VP8",
|
| 37 |
clockRate: 90000,
|
| 38 |
-
parameters: {},
|
| 39 |
-
},
|
| 40 |
-
{
|
| 41 |
-
kind: "video",
|
| 42 |
-
mimeType: "video/H264",
|
| 43 |
-
clockRate: 90000,
|
| 44 |
-
parameters: {
|
| 45 |
-
"packetization-mode": 1,
|
| 46 |
-
"profile-level-id": "42e01f",
|
| 47 |
-
"level-asymmetry-allowed": 1,
|
| 48 |
-
},
|
| 49 |
},
|
| 50 |
],
|
| 51 |
});
|
| 52 |
|
| 53 |
console.log("β
mediasoup router created");
|
| 54 |
|
|
|
|
| 55 |
const transports = new Map();
|
| 56 |
const producers = new Map();
|
| 57 |
const consumers = new Map();
|
| 58 |
|
| 59 |
-
// Middleware for logging
|
| 60 |
app.use((req, res, next) => {
|
| 61 |
console.log(`π¨ ${req.method} ${req.path} - ${new Date().toISOString()}`);
|
| 62 |
next();
|
| 63 |
});
|
| 64 |
|
| 65 |
-
//
|
| 66 |
-
app.get("/
|
| 67 |
res.send(`
|
| 68 |
<!DOCTYPE html>
|
| 69 |
<html lang="en">
|
| 70 |
<head>
|
| 71 |
<meta charset="UTF-8">
|
| 72 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 73 |
-
<title>
|
| 74 |
<style>
|
| 75 |
* { margin: 0; padding: 0; box-sizing: border-box; }
|
| 76 |
body {
|
| 77 |
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
| 78 |
-
background: linear-gradient(135deg, #
|
| 79 |
min-height: 100vh;
|
| 80 |
display: flex;
|
| 81 |
align-items: center;
|
|
@@ -87,29 +73,28 @@ app.get("/publish", (req, res) => {
|
|
| 87 |
border-radius: 20px;
|
| 88 |
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
|
| 89 |
padding: 40px;
|
| 90 |
-
max-width:
|
| 91 |
width: 100%;
|
| 92 |
}
|
| 93 |
-
h1 {
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
}
|
|
|
|
|
|
|
|
|
|
| 98 |
video {
|
| 99 |
width: 100%;
|
| 100 |
border-radius: 12px;
|
| 101 |
background: #000;
|
| 102 |
margin: 20px 0;
|
| 103 |
-
|
| 104 |
-
}
|
| 105 |
-
.controls {
|
| 106 |
-
display: flex;
|
| 107 |
-
gap: 10px;
|
| 108 |
-
margin: 20px 0;
|
| 109 |
-
flex-wrap: wrap;
|
| 110 |
}
|
| 111 |
button {
|
| 112 |
-
background: #
|
| 113 |
color: white;
|
| 114 |
border: none;
|
| 115 |
padding: 12px 30px;
|
|
@@ -117,196 +102,114 @@ app.get("/publish", (req, res) => {
|
|
| 117 |
font-size: 16px;
|
| 118 |
font-weight: 600;
|
| 119 |
cursor: pointer;
|
| 120 |
-
transition: all 0.3s;
|
| 121 |
-
flex: 1;
|
| 122 |
-
min-width: 150px;
|
| 123 |
-
}
|
| 124 |
-
button:hover { background: #e04556; transform: translateY(-2px); }
|
| 125 |
-
button:active { transform: translateY(0); }
|
| 126 |
-
button:disabled {
|
| 127 |
-
background: #ccc;
|
| 128 |
-
cursor: not-allowed;
|
| 129 |
-
transform: none;
|
| 130 |
-
}
|
| 131 |
-
button.secondary {
|
| 132 |
-
background: #6c757d;
|
| 133 |
-
}
|
| 134 |
-
button.secondary:hover {
|
| 135 |
-
background: #5a6268;
|
| 136 |
-
}
|
| 137 |
-
.status {
|
| 138 |
-
padding: 12px 20px;
|
| 139 |
-
border-radius: 8px;
|
| 140 |
-
margin: 20px 0;
|
| 141 |
-
font-weight: 600;
|
| 142 |
-
}
|
| 143 |
-
.status.idle { background: #f8f9fa; color: #6c757d; }
|
| 144 |
-
.status.streaming { background: #d4edda; color: #155724; }
|
| 145 |
-
.status.error { background: #f8d7da; color: #721c24; }
|
| 146 |
-
select {
|
| 147 |
-
padding: 10px;
|
| 148 |
-
border-radius: 8px;
|
| 149 |
-
border: 2px solid #ddd;
|
| 150 |
-
font-size: 14px;
|
| 151 |
-
flex: 1;
|
| 152 |
-
min-width: 200px;
|
| 153 |
}
|
|
|
|
|
|
|
| 154 |
</style>
|
| 155 |
</head>
|
| 156 |
<body>
|
| 157 |
<div class="container">
|
| 158 |
-
<h1>
|
| 159 |
-
<div id="status" class="status
|
| 160 |
-
|
| 161 |
-
<
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
<select id="sourceType">
|
| 165 |
-
<option value="camera">Camera</option>
|
| 166 |
-
<option value="screen">Screen Share</option>
|
| 167 |
-
</select>
|
| 168 |
-
<button id="startBtn" onclick="startStream()">π΄ Start Streaming</button>
|
| 169 |
-
<button id="stopBtn" onclick="stopStream()" disabled class="secondary">βΉ Stop</button>
|
| 170 |
-
</div>
|
| 171 |
-
|
| 172 |
-
<div style="background: #f8f9fa; padding: 15px; border-radius: 8px; margin-top: 20px; font-size: 14px; color: #666;">
|
| 173 |
-
<strong>Instructions:</strong><br>
|
| 174 |
-
1. Select Camera or Screen Share<br>
|
| 175 |
-
2. Click "Start Streaming"<br>
|
| 176 |
-
3. View at: <a href="/" target="_blank">Viewer Page</a>
|
| 177 |
</div>
|
| 178 |
</div>
|
| 179 |
-
|
| 180 |
<script>
|
| 181 |
-
|
| 182 |
-
let pc = null;
|
| 183 |
-
const WHIP_ENDPOINT = window.location.origin + '/whip';
|
| 184 |
const status = document.getElementById('status');
|
| 185 |
-
const
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
function updateStatus(msg, type = 'idle') {
|
| 189 |
-
status.className = 'status ' + type;
|
| 190 |
-
status.textContent = msg;
|
| 191 |
-
}
|
| 192 |
|
| 193 |
-
async function
|
| 194 |
try {
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
const sourceType = document.getElementById('sourceType').value;
|
| 199 |
|
| 200 |
-
// Get
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
video: { width: 1280, height: 720 },
|
| 204 |
-
audio: true
|
| 205 |
-
});
|
| 206 |
-
} else {
|
| 207 |
-
stream = await navigator.mediaDevices.getDisplayMedia({
|
| 208 |
-
video: { width: 1920, height: 1080 },
|
| 209 |
-
audio: true
|
| 210 |
-
});
|
| 211 |
-
}
|
| 212 |
|
| 213 |
-
|
| 214 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
// Create peer connection
|
| 217 |
-
pc = new RTCPeerConnection(
|
| 218 |
-
iceServers: [
|
| 219 |
-
{ urls: 'stun:stun.l.google.com:19302' },
|
| 220 |
-
{ urls: 'stun:stun1.l.google.com:19302' }
|
| 221 |
-
]
|
| 222 |
-
});
|
| 223 |
|
| 224 |
-
// Add
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
pc.addTrack(track, stream);
|
| 228 |
-
});
|
| 229 |
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
}
|
| 236 |
};
|
| 237 |
|
| 238 |
// Create offer
|
| 239 |
const offer = await pc.createOffer();
|
| 240 |
await pc.setLocalDescription(offer);
|
| 241 |
|
| 242 |
-
//
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
pc.removeEventListener('icegatheringstatechange', checkState);
|
| 251 |
-
resolve();
|
| 252 |
-
}
|
| 253 |
-
};
|
| 254 |
-
pc.addEventListener('icegatheringstatechange', checkState);
|
| 255 |
-
}
|
| 256 |
});
|
| 257 |
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
// Send to WHIP endpoint
|
| 261 |
-
const response = await fetch(WHIP_ENDPOINT, {
|
| 262 |
method: 'POST',
|
| 263 |
-
headers: {
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
|
|
|
| 267 |
});
|
| 268 |
|
| 269 |
-
if (!
|
| 270 |
-
|
| 271 |
-
throw new Error('WHIP failed: ' + response.status + ' - ' + errorText);
|
| 272 |
}
|
| 273 |
|
| 274 |
-
const
|
| 275 |
-
console.log('Received answer from server');
|
| 276 |
-
|
| 277 |
-
await pc.setRemoteDescription({
|
| 278 |
-
type: 'answer',
|
| 279 |
-
sdp: answerSdp
|
| 280 |
-
});
|
| 281 |
|
| 282 |
-
|
| 283 |
-
|
|
|
|
| 284 |
|
| 285 |
-
console.log('β
|
| 286 |
|
| 287 |
} catch (error) {
|
| 288 |
console.error('Error:', error);
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
stream.getTracks().forEach(t => t.stop());
|
| 293 |
-
}
|
| 294 |
}
|
| 295 |
}
|
| 296 |
|
| 297 |
-
function
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
updateStatus('Stream stopped', 'idle');
|
| 308 |
-
startBtn.disabled = false;
|
| 309 |
-
stopBtn.disabled = true;
|
| 310 |
}
|
| 311 |
</script>
|
| 312 |
</body>
|
|
@@ -314,20 +217,20 @@ app.get("/publish", (req, res) => {
|
|
| 314 |
`);
|
| 315 |
});
|
| 316 |
|
| 317 |
-
//
|
| 318 |
-
app.get("/", (req, res) => {
|
| 319 |
res.send(`
|
| 320 |
<!DOCTYPE html>
|
| 321 |
<html lang="en">
|
| 322 |
<head>
|
| 323 |
<meta charset="UTF-8">
|
| 324 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 325 |
-
<title>
|
| 326 |
<style>
|
| 327 |
* { margin: 0; padding: 0; box-sizing: border-box; }
|
| 328 |
body {
|
| 329 |
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
| 330 |
-
background: linear-gradient(135deg, #
|
| 331 |
min-height: 100vh;
|
| 332 |
display: flex;
|
| 333 |
align-items: center;
|
|
@@ -339,49 +242,24 @@ app.get("/", (req, res) => {
|
|
| 339 |
border-radius: 20px;
|
| 340 |
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
|
| 341 |
padding: 40px;
|
| 342 |
-
max-width:
|
| 343 |
width: 100%;
|
| 344 |
}
|
| 345 |
-
h1 {
|
| 346 |
-
color: #333;
|
| 347 |
-
margin-bottom: 10px;
|
| 348 |
-
font-size: 2em;
|
| 349 |
-
}
|
| 350 |
-
.status {
|
| 351 |
-
padding: 12px 20px;
|
| 352 |
-
border-radius: 8px;
|
| 353 |
-
margin: 20px 0;
|
| 354 |
-
font-weight: 600;
|
| 355 |
-
display: flex;
|
| 356 |
-
align-items: center;
|
| 357 |
-
gap: 10px;
|
| 358 |
-
}
|
| 359 |
-
.status.waiting { background: #fff3cd; color: #856404; }
|
| 360 |
-
.status.connecting { background: #d1ecf1; color: #0c5460; }
|
| 361 |
-
.status.live { background: #d4edda; color: #155724; }
|
| 362 |
-
.status.error { background: #f8d7da; color: #721c24; }
|
| 363 |
-
.dot {
|
| 364 |
-
width: 10px;
|
| 365 |
-
height: 10px;
|
| 366 |
-
border-radius: 50%;
|
| 367 |
-
display: inline-block;
|
| 368 |
-
}
|
| 369 |
-
.dot.pulse {
|
| 370 |
-
animation: pulse 2s infinite;
|
| 371 |
-
}
|
| 372 |
-
@keyframes pulse {
|
| 373 |
-
0%, 100% { opacity: 1; }
|
| 374 |
-
50% { opacity: 0.5; }
|
| 375 |
-
}
|
| 376 |
video {
|
| 377 |
width: 100%;
|
| 378 |
border-radius: 12px;
|
| 379 |
background: #000;
|
| 380 |
margin: 20px 0;
|
| 381 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
}
|
| 383 |
button {
|
| 384 |
-
background: #
|
| 385 |
color: white;
|
| 386 |
border: none;
|
| 387 |
padding: 12px 30px;
|
|
@@ -389,262 +267,365 @@ app.get("/", (req, res) => {
|
|
| 389 |
font-size: 16px;
|
| 390 |
font-weight: 600;
|
| 391 |
cursor: pointer;
|
| 392 |
-
|
| 393 |
}
|
| 394 |
-
button:hover { background: #
|
| 395 |
-
button:
|
| 396 |
-
button
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
|
|
|
|
|
|
| 400 |
}
|
| 401 |
-
.
|
| 402 |
-
|
| 403 |
-
|
|
|
|
|
|
|
| 404 |
border-radius: 8px;
|
| 405 |
-
|
| 406 |
font-size: 14px;
|
| 407 |
-
|
| 408 |
}
|
| 409 |
-
.info strong { color: #333; }
|
| 410 |
</style>
|
| 411 |
</head>
|
| 412 |
<body>
|
| 413 |
<div class="container">
|
| 414 |
-
<h1>
|
| 415 |
-
<div id="status" class="status
|
| 416 |
-
|
| 417 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
</div>
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
<button id="playBtn" onclick="startViewing()">βΆοΈ Start Viewing</button>
|
| 423 |
-
|
| 424 |
-
<div class="info">
|
| 425 |
-
<strong>Instructions:</strong><br>
|
| 426 |
-
1. Start streaming at: <a href="/publish" target="_blank">Publisher Page</a><br>
|
| 427 |
-
2. Click "Start Viewing" to watch the stream<br>
|
| 428 |
-
3. Stream will auto-connect when available
|
| 429 |
</div>
|
| 430 |
</div>
|
| 431 |
-
|
| 432 |
<script>
|
| 433 |
-
|
|
|
|
|
|
|
|
|
|
| 434 |
const status = document.getElementById('status');
|
| 435 |
-
const
|
| 436 |
-
const
|
| 437 |
-
|
| 438 |
-
let pc = null;
|
| 439 |
-
|
| 440 |
-
function updateStatus(msg, type = 'waiting') {
|
| 441 |
-
const colors = {
|
| 442 |
-
waiting: '#856404',
|
| 443 |
-
connecting: '#0c5460',
|
| 444 |
-
live: '#155724',
|
| 445 |
-
error: '#721c24'
|
| 446 |
-
};
|
| 447 |
-
status.className = 'status ' + type;
|
| 448 |
-
status.innerHTML = \`<span class="dot pulse" style="background: \${colors[type]};"></span>\${msg}\`;
|
| 449 |
-
}
|
| 450 |
|
| 451 |
-
async function
|
| 452 |
try {
|
| 453 |
-
|
| 454 |
-
|
|
|
|
| 455 |
|
| 456 |
-
|
| 457 |
-
pc = new RTCPeerConnection({
|
| 458 |
-
iceServers: [
|
| 459 |
-
{ urls: 'stun:stun.l.google.com:19302' },
|
| 460 |
-
{ urls: 'stun:stun1.l.google.com:19302' }
|
| 461 |
-
]
|
| 462 |
-
});
|
| 463 |
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
|
|
|
|
|
|
| 473 |
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
console.log('Connection state:', pc.connectionState);
|
| 477 |
-
if (pc.connectionState === 'failed' || pc.connectionState === 'disconnected') {
|
| 478 |
-
updateStatus('β Connection lost', 'error');
|
| 479 |
-
playBtn.disabled = false;
|
| 480 |
-
playBtn.style.display = 'block';
|
| 481 |
-
}
|
| 482 |
-
};
|
| 483 |
|
| 484 |
-
//
|
| 485 |
-
|
| 486 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 487 |
|
| 488 |
-
// Create offer
|
| 489 |
const offer = await pc.createOffer();
|
| 490 |
await pc.setLocalDescription(offer);
|
| 491 |
|
| 492 |
-
//
|
| 493 |
-
|
| 494 |
-
if (pc.iceGatheringState === 'complete') {
|
| 495 |
-
resolve();
|
| 496 |
-
} else {
|
| 497 |
-
const checkState = () => {
|
| 498 |
-
if (pc.iceGatheringState === 'complete') {
|
| 499 |
-
pc.removeEventListener('icegatheringstatechange', checkState);
|
| 500 |
-
resolve();
|
| 501 |
-
}
|
| 502 |
-
};
|
| 503 |
-
pc.addEventListener('icegatheringstatechange', checkState);
|
| 504 |
-
}
|
| 505 |
-
});
|
| 506 |
|
| 507 |
-
//
|
| 508 |
-
|
| 509 |
method: 'POST',
|
| 510 |
-
headers: { 'Content-Type': 'application/
|
| 511 |
-
body:
|
|
|
|
|
|
|
|
|
|
| 512 |
});
|
| 513 |
|
| 514 |
-
|
| 515 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 516 |
}
|
| 517 |
|
| 518 |
-
|
| 519 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 520 |
|
| 521 |
-
|
|
|
|
|
|
|
| 522 |
|
| 523 |
} catch (error) {
|
| 524 |
console.error('Error:', error);
|
| 525 |
-
|
| 526 |
-
|
|
|
|
|
|
|
| 527 |
}
|
| 528 |
}
|
| 529 |
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 540 |
}
|
| 541 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 542 |
</script>
|
| 543 |
</body>
|
| 544 |
</html>
|
| 545 |
`);
|
| 546 |
});
|
| 547 |
|
| 548 |
-
//
|
| 549 |
-
app.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 550 |
try {
|
| 551 |
-
console.log("π₯
|
| 552 |
-
|
| 553 |
-
// Create transport for producer
|
| 554 |
const transport = await router.createWebRtcTransport({
|
| 555 |
-
listenIps: [{
|
| 556 |
-
ip: "0.0.0.0",
|
| 557 |
-
announcedIp: ANNOUNCED_IP
|
| 558 |
-
}],
|
| 559 |
enableUdp: true,
|
| 560 |
enableTcp: true,
|
| 561 |
preferUdp: true,
|
| 562 |
});
|
| 563 |
|
| 564 |
-
|
| 565 |
-
transports.set(transportId, transport);
|
| 566 |
|
| 567 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 568 |
|
| 569 |
-
|
| 570 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 571 |
|
| 572 |
-
|
| 573 |
-
const answerSdp = createAnswerSdp(transport, offerSdp);
|
| 574 |
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
}
|
| 581 |
});
|
| 582 |
|
| 583 |
-
|
| 584 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 585 |
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
|
| 590 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 591 |
|
|
|
|
|
|
|
|
|
|
| 592 |
} catch (error) {
|
| 593 |
-
console.error("β
|
| 594 |
res.status(500).json({ error: error.message });
|
| 595 |
}
|
| 596 |
});
|
| 597 |
|
| 598 |
-
//
|
| 599 |
-
app.post("/whep", async (req, res) => {
|
| 600 |
try {
|
| 601 |
-
|
|
|
|
| 602 |
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
if (!hasProducers) {
|
| 606 |
-
console.log("β οΈ No active stream");
|
| 607 |
-
return res.status(404).send("No stream available");
|
| 608 |
}
|
| 609 |
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
listenIps: [{
|
| 613 |
-
ip: "0.0.0.0",
|
| 614 |
-
announcedIp: ANNOUNCED_IP
|
| 615 |
-
}],
|
| 616 |
-
enableUdp: true,
|
| 617 |
-
enableTcp: true,
|
| 618 |
-
preferUdp: true,
|
| 619 |
-
});
|
| 620 |
|
| 621 |
-
|
| 622 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 623 |
|
| 624 |
-
|
|
|
|
|
|
|
| 625 |
|
| 626 |
-
|
|
|
|
| 627 |
|
| 628 |
-
|
| 629 |
-
|
|
|
|
| 630 |
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
}
|
| 634 |
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
|
|
|
|
|
|
| 638 |
|
| 639 |
-
|
| 640 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 641 |
} catch (error) {
|
| 642 |
-
console.error("β
|
| 643 |
res.status(500).json({ error: error.message });
|
| 644 |
}
|
| 645 |
});
|
| 646 |
|
| 647 |
-
// Status endpoint
|
| 648 |
app.get("/status", (req, res) => {
|
| 649 |
res.json({
|
| 650 |
hasStream: producers.size > 0,
|
|
@@ -655,57 +636,13 @@ app.get("/status", (req, res) => {
|
|
| 655 |
});
|
| 656 |
});
|
| 657 |
|
| 658 |
-
// Helper function to create SDP answer
|
| 659 |
-
function createAnswerSdp(transport, offerSdp) {
|
| 660 |
-
const ice = transport.iceParameters;
|
| 661 |
-
const dtls = transport.dtlsParameters;
|
| 662 |
-
const iceCandidates = transport.iceCandidates;
|
| 663 |
-
|
| 664 |
-
let sdp = `v=0
|
| 665 |
-
o=- 0 0 IN IP4 0.0.0.0
|
| 666 |
-
s=-
|
| 667 |
-
t=0 0
|
| 668 |
-
a=group:BUNDLE 0
|
| 669 |
-
a=msid-semantic: WMS *
|
| 670 |
-
`;
|
| 671 |
-
|
| 672 |
-
// Add media section
|
| 673 |
-
sdp += `m=video 9 UDP/TLS/RTP/SAVPF 96
|
| 674 |
-
c=IN IP4 0.0.0.0
|
| 675 |
-
a=rtcp:9 IN IP4 0.0.0.0
|
| 676 |
-
a=ice-ufrag:${ice.usernameFragment}
|
| 677 |
-
a=ice-pwd:${ice.password}
|
| 678 |
-
a=ice-options:trickle
|
| 679 |
-
a=fingerprint:${dtls.fingerprints[0].algorithm} ${dtls.fingerprints[0].value}
|
| 680 |
-
a=setup:active
|
| 681 |
-
a=mid:0
|
| 682 |
-
a=sendrecv
|
| 683 |
-
a=rtcp-mux
|
| 684 |
-
a=rtcp-rsize
|
| 685 |
-
a=rtpmap:96 VP8/90000
|
| 686 |
-
`;
|
| 687 |
-
|
| 688 |
-
// Add ICE candidates
|
| 689 |
-
iceCandidates.forEach(candidate => {
|
| 690 |
-
sdp += `a=candidate:${candidate.foundation} ${candidate.component} ${candidate.protocol} ${candidate.priority} ${candidate.ip} ${candidate.port} typ ${candidate.type}
|
| 691 |
-
`;
|
| 692 |
-
});
|
| 693 |
-
|
| 694 |
-
sdp += `a=end-of-candidates
|
| 695 |
-
`;
|
| 696 |
-
|
| 697 |
-
return sdp;
|
| 698 |
-
}
|
| 699 |
-
|
| 700 |
// Start server
|
| 701 |
const PORT = process.env.PORT || 7860;
|
| 702 |
http.createServer(app).listen(PORT, () => {
|
| 703 |
console.log(`\n${"=".repeat(50)}`);
|
| 704 |
-
console.log(`π
|
| 705 |
console.log(`${"=".repeat(50)}`);
|
| 706 |
console.log(`πΊ Viewer: http://localhost:${PORT}/`);
|
| 707 |
console.log(`πΉ Publisher: http://localhost:${PORT}/publish`);
|
| 708 |
-
console.log(`π€ WHIP: http://localhost:${PORT}/whip`);
|
| 709 |
-
console.log(`π₯ WHEP: http://localhost:${PORT}/whep`);
|
| 710 |
console.log(`${"=".repeat(50)}\n`);
|
| 711 |
});
|
|
|
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
app.use(cors());
|
|
|
|
| 8 |
app.use(express.json());
|
| 9 |
|
| 10 |
console.log("π Starting mediasoup WHIP/WHEP server...");
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
// Create mediasoup worker
|
| 13 |
const worker = await mediasoup.createWorker({
|
| 14 |
logLevel: "warn",
|
|
|
|
| 31 |
kind: "video",
|
| 32 |
mimeType: "video/VP8",
|
| 33 |
clockRate: 90000,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
},
|
| 35 |
],
|
| 36 |
});
|
| 37 |
|
| 38 |
console.log("β
mediasoup router created");
|
| 39 |
|
| 40 |
+
// Store transports and producers/consumers
|
| 41 |
const transports = new Map();
|
| 42 |
const producers = new Map();
|
| 43 |
const consumers = new Map();
|
| 44 |
|
| 45 |
+
// Middleware for logging
|
| 46 |
app.use((req, res, next) => {
|
| 47 |
console.log(`π¨ ${req.method} ${req.path} - ${new Date().toISOString()}`);
|
| 48 |
next();
|
| 49 |
});
|
| 50 |
|
| 51 |
+
// Root route - Viewer
|
| 52 |
+
app.get("/", (req, res) => {
|
| 53 |
res.send(`
|
| 54 |
<!DOCTYPE html>
|
| 55 |
<html lang="en">
|
| 56 |
<head>
|
| 57 |
<meta charset="UTF-8">
|
| 58 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 59 |
+
<title>WHEP Stream Viewer</title>
|
| 60 |
<style>
|
| 61 |
* { margin: 0; padding: 0; box-sizing: border-box; }
|
| 62 |
body {
|
| 63 |
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
| 64 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 65 |
min-height: 100vh;
|
| 66 |
display: flex;
|
| 67 |
align-items: center;
|
|
|
|
| 73 |
border-radius: 20px;
|
| 74 |
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
|
| 75 |
padding: 40px;
|
| 76 |
+
max-width: 900px;
|
| 77 |
width: 100%;
|
| 78 |
}
|
| 79 |
+
h1 { color: #333; margin-bottom: 10px; font-size: 2em; }
|
| 80 |
+
.status {
|
| 81 |
+
padding: 12px 20px;
|
| 82 |
+
border-radius: 8px;
|
| 83 |
+
margin: 20px 0;
|
| 84 |
+
font-weight: 600;
|
| 85 |
}
|
| 86 |
+
.status.waiting { background: #fff3cd; color: #856404; }
|
| 87 |
+
.status.live { background: #d4edda; color: #155724; }
|
| 88 |
+
.status.error { background: #f8d7da; color: #721c24; }
|
| 89 |
video {
|
| 90 |
width: 100%;
|
| 91 |
border-radius: 12px;
|
| 92 |
background: #000;
|
| 93 |
margin: 20px 0;
|
| 94 |
+
max-height: 500px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
}
|
| 96 |
button {
|
| 97 |
+
background: #667eea;
|
| 98 |
color: white;
|
| 99 |
border: none;
|
| 100 |
padding: 12px 30px;
|
|
|
|
| 102 |
font-size: 16px;
|
| 103 |
font-weight: 600;
|
| 104 |
cursor: pointer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
}
|
| 106 |
+
button:hover { background: #5568d3; }
|
| 107 |
+
button:disabled { background: #ccc; cursor: not-allowed; }
|
| 108 |
</style>
|
| 109 |
</head>
|
| 110 |
<body>
|
| 111 |
<div class="container">
|
| 112 |
+
<h1>π‘ Stream Viewer</h1>
|
| 113 |
+
<div id="status" class="status waiting">Waiting for stream...</div>
|
| 114 |
+
<video id="video" autoplay playsinline controls></video>
|
| 115 |
+
<button id="playBtn" onclick="startViewing()">βΆοΈ Start Viewing</button>
|
| 116 |
+
<div style="margin-top: 20px; font-size: 14px; color: #666;">
|
| 117 |
+
<strong>Publisher:</strong> <a href="/publish" target="_blank">Click here to stream</a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
</div>
|
| 119 |
</div>
|
|
|
|
| 120 |
<script>
|
| 121 |
+
const video = document.getElementById('video');
|
|
|
|
|
|
|
| 122 |
const status = document.getElementById('status');
|
| 123 |
+
const playBtn = document.getElementById('playBtn');
|
| 124 |
+
let pc = null;
|
| 125 |
+
let consumeTransportId = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
async function startViewing() {
|
| 128 |
try {
|
| 129 |
+
status.className = 'status waiting';
|
| 130 |
+
status.textContent = 'Connecting...';
|
| 131 |
+
playBtn.disabled = true;
|
|
|
|
| 132 |
|
| 133 |
+
// Get router RTP capabilities
|
| 134 |
+
const rtpCapsResponse = await fetch('/api/rtpCapabilities');
|
| 135 |
+
const rtpCapabilities = await rtpCapsResponse.json();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
+
// Create transport
|
| 138 |
+
const transportResponse = await fetch('/api/createConsumerTransport', {
|
| 139 |
+
method: 'POST',
|
| 140 |
+
headers: { 'Content-Type': 'application/json' }
|
| 141 |
+
});
|
| 142 |
+
const transportData = await transportResponse.json();
|
| 143 |
+
consumeTransportId = transportData.id;
|
| 144 |
|
| 145 |
// Create peer connection
|
| 146 |
+
pc = new RTCPeerConnection();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
+
// Add transceivers
|
| 149 |
+
pc.addTransceiver('video', { direction: 'recvonly' });
|
| 150 |
+
pc.addTransceiver('audio', { direction: 'recvonly' });
|
|
|
|
|
|
|
| 151 |
|
| 152 |
+
pc.ontrack = (event) => {
|
| 153 |
+
console.log('πΉ Received track:', event.track.kind);
|
| 154 |
+
video.srcObject = event.streams[0];
|
| 155 |
+
status.className = 'status live';
|
| 156 |
+
status.textContent = 'π΄ LIVE';
|
|
|
|
| 157 |
};
|
| 158 |
|
| 159 |
// Create offer
|
| 160 |
const offer = await pc.createOffer();
|
| 161 |
await pc.setLocalDescription(offer);
|
| 162 |
|
| 163 |
+
// Connect transport
|
| 164 |
+
await fetch('/api/connectConsumerTransport', {
|
| 165 |
+
method: 'POST',
|
| 166 |
+
headers: { 'Content-Type': 'application/json' },
|
| 167 |
+
body: JSON.stringify({
|
| 168 |
+
transportId: consumeTransportId,
|
| 169 |
+
dtlsParameters: extractDtlsParameters(pc.localDescription.sdp)
|
| 170 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
});
|
| 172 |
|
| 173 |
+
// Consume
|
| 174 |
+
const consumeResponse = await fetch('/api/consume', {
|
|
|
|
|
|
|
| 175 |
method: 'POST',
|
| 176 |
+
headers: { 'Content-Type': 'application/json' },
|
| 177 |
+
body: JSON.stringify({
|
| 178 |
+
transportId: consumeTransportId,
|
| 179 |
+
rtpCapabilities
|
| 180 |
+
})
|
| 181 |
});
|
| 182 |
|
| 183 |
+
if (!consumeResponse.ok) {
|
| 184 |
+
throw new Error('No stream available');
|
|
|
|
| 185 |
}
|
| 186 |
|
| 187 |
+
const { rtpParameters, id: consumerId } = await consumeResponse.json();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
+
// Add track info to answer
|
| 190 |
+
const answer = await pc.createAnswer();
|
| 191 |
+
await pc.setRemoteDescription({ type: 'answer', sdp: answer.sdp });
|
| 192 |
|
| 193 |
+
console.log('β
Viewing started');
|
| 194 |
|
| 195 |
} catch (error) {
|
| 196 |
console.error('Error:', error);
|
| 197 |
+
status.className = 'status error';
|
| 198 |
+
status.textContent = 'β ' + error.message;
|
| 199 |
+
playBtn.disabled = false;
|
|
|
|
|
|
|
| 200 |
}
|
| 201 |
}
|
| 202 |
|
| 203 |
+
function extractDtlsParameters(sdp) {
|
| 204 |
+
const fingerprintLine = sdp.match(/a=fingerprint:(\\S+) (\\S+)/);
|
| 205 |
+
const setupLine = sdp.match(/a=setup:(\\S+)/);
|
| 206 |
+
return {
|
| 207 |
+
role: setupLine ? setupLine[1] : 'auto',
|
| 208 |
+
fingerprints: [{
|
| 209 |
+
algorithm: fingerprintLine[1],
|
| 210 |
+
value: fingerprintLine[2]
|
| 211 |
+
}]
|
| 212 |
+
};
|
|
|
|
|
|
|
|
|
|
| 213 |
}
|
| 214 |
</script>
|
| 215 |
</body>
|
|
|
|
| 217 |
`);
|
| 218 |
});
|
| 219 |
|
| 220 |
+
// Publisher route
|
| 221 |
+
app.get("/publish", (req, res) => {
|
| 222 |
res.send(`
|
| 223 |
<!DOCTYPE html>
|
| 224 |
<html lang="en">
|
| 225 |
<head>
|
| 226 |
<meta charset="UTF-8">
|
| 227 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 228 |
+
<title>Stream Publisher</title>
|
| 229 |
<style>
|
| 230 |
* { margin: 0; padding: 0; box-sizing: border-box; }
|
| 231 |
body {
|
| 232 |
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
| 233 |
+
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
|
| 234 |
min-height: 100vh;
|
| 235 |
display: flex;
|
| 236 |
align-items: center;
|
|
|
|
| 242 |
border-radius: 20px;
|
| 243 |
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
|
| 244 |
padding: 40px;
|
| 245 |
+
max-width: 800px;
|
| 246 |
width: 100%;
|
| 247 |
}
|
| 248 |
+
h1 { color: #333; margin-bottom: 20px; font-size: 2em; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
video {
|
| 250 |
width: 100%;
|
| 251 |
border-radius: 12px;
|
| 252 |
background: #000;
|
| 253 |
margin: 20px 0;
|
| 254 |
+
max-height: 400px;
|
| 255 |
+
}
|
| 256 |
+
.controls {
|
| 257 |
+
display: flex;
|
| 258 |
+
gap: 10px;
|
| 259 |
+
margin: 20px 0;
|
| 260 |
}
|
| 261 |
button {
|
| 262 |
+
background: #f5576c;
|
| 263 |
color: white;
|
| 264 |
border: none;
|
| 265 |
padding: 12px 30px;
|
|
|
|
| 267 |
font-size: 16px;
|
| 268 |
font-weight: 600;
|
| 269 |
cursor: pointer;
|
| 270 |
+
flex: 1;
|
| 271 |
}
|
| 272 |
+
button:hover { background: #e04556; }
|
| 273 |
+
button:disabled { background: #ccc; cursor: not-allowed; }
|
| 274 |
+
button.secondary { background: #6c757d; }
|
| 275 |
+
.status {
|
| 276 |
+
padding: 12px 20px;
|
| 277 |
+
border-radius: 8px;
|
| 278 |
+
margin: 20px 0;
|
| 279 |
+
font-weight: 600;
|
| 280 |
}
|
| 281 |
+
.status.idle { background: #f8f9fa; color: #6c757d; }
|
| 282 |
+
.status.streaming { background: #d4edda; color: #155724; }
|
| 283 |
+
.status.error { background: #f8d7da; color: #721c24; }
|
| 284 |
+
select {
|
| 285 |
+
padding: 10px;
|
| 286 |
border-radius: 8px;
|
| 287 |
+
border: 2px solid #ddd;
|
| 288 |
font-size: 14px;
|
| 289 |
+
flex: 1;
|
| 290 |
}
|
|
|
|
| 291 |
</style>
|
| 292 |
</head>
|
| 293 |
<body>
|
| 294 |
<div class="container">
|
| 295 |
+
<h1>πΉ Stream Publisher</h1>
|
| 296 |
+
<div id="status" class="status idle">Ready to stream</div>
|
| 297 |
+
<video id="preview" autoplay muted playsinline></video>
|
| 298 |
+
<div class="controls">
|
| 299 |
+
<select id="sourceType">
|
| 300 |
+
<option value="camera">Camera</option>
|
| 301 |
+
<option value="screen">Screen Share</option>
|
| 302 |
+
</select>
|
| 303 |
+
<button id="startBtn" onclick="startStream()">π΄ Start</button>
|
| 304 |
+
<button id="stopBtn" onclick="stopStream()" disabled class="secondary">βΉ Stop</button>
|
| 305 |
</div>
|
| 306 |
+
<div style="background: #f8f9fa; padding: 15px; border-radius: 8px; font-size: 14px; color: #666;">
|
| 307 |
+
View stream at: <a href="/" target="_blank">Viewer Page</a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
</div>
|
| 309 |
</div>
|
|
|
|
| 310 |
<script>
|
| 311 |
+
let stream = null;
|
| 312 |
+
let produceTransportId = null;
|
| 313 |
+
let videoProducerId = null;
|
| 314 |
+
let audioProducerId = null;
|
| 315 |
const status = document.getElementById('status');
|
| 316 |
+
const startBtn = document.getElementById('startBtn');
|
| 317 |
+
const stopBtn = document.getElementById('stopBtn');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
|
| 319 |
+
async function startStream() {
|
| 320 |
try {
|
| 321 |
+
status.className = 'status idle';
|
| 322 |
+
status.textContent = 'Getting media...';
|
| 323 |
+
startBtn.disabled = true;
|
| 324 |
|
| 325 |
+
const sourceType = document.getElementById('sourceType').value;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
|
| 327 |
+
if (sourceType === 'camera') {
|
| 328 |
+
stream = await navigator.mediaDevices.getUserMedia({
|
| 329 |
+
video: { width: 1280, height: 720 },
|
| 330 |
+
audio: true
|
| 331 |
+
});
|
| 332 |
+
} else {
|
| 333 |
+
stream = await navigator.mediaDevices.getDisplayMedia({
|
| 334 |
+
video: true,
|
| 335 |
+
audio: true
|
| 336 |
+
});
|
| 337 |
+
}
|
| 338 |
|
| 339 |
+
document.getElementById('preview').srcObject = stream;
|
| 340 |
+
status.textContent = 'Connecting to server...';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
|
| 342 |
+
// Get router RTP capabilities
|
| 343 |
+
const rtpCapsResponse = await fetch('/api/rtpCapabilities');
|
| 344 |
+
const rtpCapabilities = await rtpCapsResponse.json();
|
| 345 |
+
|
| 346 |
+
// Create producer transport
|
| 347 |
+
const transportResponse = await fetch('/api/createProducerTransport', {
|
| 348 |
+
method: 'POST',
|
| 349 |
+
headers: { 'Content-Type': 'application/json' }
|
| 350 |
+
});
|
| 351 |
+
const transportData = await transportResponse.json();
|
| 352 |
+
produceTransportId = transportData.id;
|
| 353 |
+
|
| 354 |
+
// Create local peer connection to get parameters
|
| 355 |
+
const pc = new RTCPeerConnection();
|
| 356 |
+
stream.getTracks().forEach(track => pc.addTrack(track, stream));
|
| 357 |
|
|
|
|
| 358 |
const offer = await pc.createOffer();
|
| 359 |
await pc.setLocalDescription(offer);
|
| 360 |
|
| 361 |
+
// Extract DTLS parameters
|
| 362 |
+
const dtlsParameters = extractDtlsParameters(pc.localDescription.sdp);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
|
| 364 |
+
// Connect transport
|
| 365 |
+
await fetch('/api/connectProducerTransport', {
|
| 366 |
method: 'POST',
|
| 367 |
+
headers: { 'Content-Type': 'application/json' },
|
| 368 |
+
body: JSON.stringify({
|
| 369 |
+
transportId: produceTransportId,
|
| 370 |
+
dtlsParameters
|
| 371 |
+
})
|
| 372 |
});
|
| 373 |
|
| 374 |
+
// Produce video
|
| 375 |
+
const videoTrack = stream.getVideoTracks()[0];
|
| 376 |
+
if (videoTrack) {
|
| 377 |
+
const videoParams = extractTrackParameters(pc.localDescription.sdp, 'video');
|
| 378 |
+
const videoResponse = await fetch('/api/produce', {
|
| 379 |
+
method: 'POST',
|
| 380 |
+
headers: { 'Content-Type': 'application/json' },
|
| 381 |
+
body: JSON.stringify({
|
| 382 |
+
transportId: produceTransportId,
|
| 383 |
+
kind: 'video',
|
| 384 |
+
rtpParameters: videoParams
|
| 385 |
+
})
|
| 386 |
+
});
|
| 387 |
+
const { id } = await videoResponse.json();
|
| 388 |
+
videoProducerId = id;
|
| 389 |
+
console.log('β
Video producer created:', id);
|
| 390 |
}
|
| 391 |
|
| 392 |
+
// Produce audio
|
| 393 |
+
const audioTrack = stream.getAudioTracks()[0];
|
| 394 |
+
if (audioTrack) {
|
| 395 |
+
const audioParams = extractTrackParameters(pc.localDescription.sdp, 'audio');
|
| 396 |
+
const audioResponse = await fetch('/api/produce', {
|
| 397 |
+
method: 'POST',
|
| 398 |
+
headers: { 'Content-Type': 'application/json' },
|
| 399 |
+
body: JSON.stringify({
|
| 400 |
+
transportId: produceTransportId,
|
| 401 |
+
kind: 'audio',
|
| 402 |
+
rtpParameters: audioParams
|
| 403 |
+
})
|
| 404 |
+
});
|
| 405 |
+
const { id } = await audioResponse.json();
|
| 406 |
+
audioProducerId = id;
|
| 407 |
+
console.log('β
Audio producer created:', id);
|
| 408 |
+
}
|
| 409 |
|
| 410 |
+
status.className = 'status streaming';
|
| 411 |
+
status.textContent = 'π΄ STREAMING LIVE';
|
| 412 |
+
stopBtn.disabled = false;
|
| 413 |
|
| 414 |
} catch (error) {
|
| 415 |
console.error('Error:', error);
|
| 416 |
+
status.className = 'status error';
|
| 417 |
+
status.textContent = 'β ' + error.message;
|
| 418 |
+
startBtn.disabled = false;
|
| 419 |
+
if (stream) stream.getTracks().forEach(t => t.stop());
|
| 420 |
}
|
| 421 |
}
|
| 422 |
|
| 423 |
+
function stopStream() {
|
| 424 |
+
if (stream) stream.getTracks().forEach(t => t.stop());
|
| 425 |
+
stream = null;
|
| 426 |
+
document.getElementById('preview').srcObject = null;
|
| 427 |
+
status.className = 'status idle';
|
| 428 |
+
status.textContent = 'Stopped';
|
| 429 |
+
startBtn.disabled = false;
|
| 430 |
+
stopBtn.disabled = true;
|
| 431 |
+
}
|
| 432 |
+
|
| 433 |
+
function extractDtlsParameters(sdp) {
|
| 434 |
+
const fingerprintLine = sdp.match(/a=fingerprint:(\\S+) (\\S+)/);
|
| 435 |
+
const setupLine = sdp.match(/a=setup:(\\S+)/);
|
| 436 |
+
return {
|
| 437 |
+
role: setupLine ? (setupLine[1] === 'active' ? 'client' : 'server') : 'auto',
|
| 438 |
+
fingerprints: [{
|
| 439 |
+
algorithm: fingerprintLine[1],
|
| 440 |
+
value: fingerprintLine[2]
|
| 441 |
+
}]
|
| 442 |
+
};
|
| 443 |
+
}
|
| 444 |
+
|
| 445 |
+
function extractTrackParameters(sdp, kind) {
|
| 446 |
+
// This is simplified - in production use a proper SDP parser
|
| 447 |
+
const rtpRegex = /a=rtpmap:(\\d+) ([^\/]+)\\/([\\d]+)/g;
|
| 448 |
+
const matches = [...sdp.matchAll(rtpRegex)];
|
| 449 |
+
|
| 450 |
+
const codecMatch = matches.find(m =>
|
| 451 |
+
(kind === 'video' && (m[2] === 'VP8' || m[2] === 'H264')) ||
|
| 452 |
+
(kind === 'audio' && m[2] === 'opus')
|
| 453 |
+
);
|
| 454 |
+
|
| 455 |
+
if (!codecMatch) {
|
| 456 |
+
throw new Error(\`No \${kind} codec found\`);
|
| 457 |
}
|
| 458 |
+
|
| 459 |
+
return {
|
| 460 |
+
codecs: [{
|
| 461 |
+
mimeType: \`\${kind}/\${codecMatch[2]}\`,
|
| 462 |
+
clockRate: parseInt(codecMatch[3]),
|
| 463 |
+
payloadType: parseInt(codecMatch[1])
|
| 464 |
+
}],
|
| 465 |
+
encodings: [{ ssrc: Math.floor(Math.random() * 1000000000) }]
|
| 466 |
+
};
|
| 467 |
+
}
|
| 468 |
</script>
|
| 469 |
</body>
|
| 470 |
</html>
|
| 471 |
`);
|
| 472 |
});
|
| 473 |
|
| 474 |
+
// API Routes
|
| 475 |
+
app.get("/api/rtpCapabilities", (req, res) => {
|
| 476 |
+
console.log("π‘ Sending RTP capabilities");
|
| 477 |
+
res.json(router.rtpCapabilities);
|
| 478 |
+
});
|
| 479 |
+
|
| 480 |
+
app.post("/api/createProducerTransport", async (req, res) => {
|
| 481 |
try {
|
| 482 |
+
console.log("π₯ Creating producer transport");
|
|
|
|
|
|
|
| 483 |
const transport = await router.createWebRtcTransport({
|
| 484 |
+
listenIps: [{ ip: "0.0.0.0", announcedIp: null }],
|
|
|
|
|
|
|
|
|
|
| 485 |
enableUdp: true,
|
| 486 |
enableTcp: true,
|
| 487 |
preferUdp: true,
|
| 488 |
});
|
| 489 |
|
| 490 |
+
transports.set(transport.id, transport);
|
|
|
|
| 491 |
|
| 492 |
+
res.json({
|
| 493 |
+
id: transport.id,
|
| 494 |
+
iceParameters: transport.iceParameters,
|
| 495 |
+
iceCandidates: transport.iceCandidates,
|
| 496 |
+
dtlsParameters: transport.dtlsParameters,
|
| 497 |
+
});
|
| 498 |
|
| 499 |
+
console.log("β
Producer transport created:", transport.id);
|
| 500 |
+
} catch (error) {
|
| 501 |
+
console.error("β Error creating producer transport:", error);
|
| 502 |
+
res.status(500).json({ error: error.message });
|
| 503 |
+
}
|
| 504 |
+
});
|
| 505 |
+
|
| 506 |
+
app.post("/api/createConsumerTransport", async (req, res) => {
|
| 507 |
+
try {
|
| 508 |
+
console.log("ποΈ Creating consumer transport");
|
| 509 |
+
const transport = await router.createWebRtcTransport({
|
| 510 |
+
listenIps: [{ ip: "0.0.0.0", announcedIp: null }],
|
| 511 |
+
enableUdp: true,
|
| 512 |
+
enableTcp: true,
|
| 513 |
+
preferUdp: true,
|
| 514 |
+
});
|
| 515 |
|
| 516 |
+
transports.set(transport.id, transport);
|
|
|
|
| 517 |
|
| 518 |
+
res.json({
|
| 519 |
+
id: transport.id,
|
| 520 |
+
iceParameters: transport.iceParameters,
|
| 521 |
+
iceCandidates: transport.iceCandidates,
|
| 522 |
+
dtlsParameters: transport.dtlsParameters,
|
|
|
|
| 523 |
});
|
| 524 |
|
| 525 |
+
console.log("β
Consumer transport created:", transport.id);
|
| 526 |
+
} catch (error) {
|
| 527 |
+
console.error("β Error creating consumer transport:", error);
|
| 528 |
+
res.status(500).json({ error: error.message });
|
| 529 |
+
}
|
| 530 |
+
});
|
| 531 |
+
|
| 532 |
+
app.post("/api/connectProducerTransport", async (req, res) => {
|
| 533 |
+
try {
|
| 534 |
+
const { transportId, dtlsParameters } = req.body;
|
| 535 |
+
const transport = transports.get(transportId);
|
| 536 |
|
| 537 |
+
if (!transport) {
|
| 538 |
+
return res.status(404).json({ error: "Transport not found" });
|
| 539 |
+
}
|
| 540 |
|
| 541 |
+
await transport.connect({ dtlsParameters });
|
| 542 |
+
console.log("β
Producer transport connected:", transportId);
|
| 543 |
+
res.json({ success: true });
|
| 544 |
+
} catch (error) {
|
| 545 |
+
console.error("β Error connecting producer transport:", error);
|
| 546 |
+
res.status(500).json({ error: error.message });
|
| 547 |
+
}
|
| 548 |
+
});
|
| 549 |
+
|
| 550 |
+
app.post("/api/connectConsumerTransport", async (req, res) => {
|
| 551 |
+
try {
|
| 552 |
+
const { transportId, dtlsParameters } = req.body;
|
| 553 |
+
const transport = transports.get(transportId);
|
| 554 |
+
|
| 555 |
+
if (!transport) {
|
| 556 |
+
return res.status(404).json({ error: "Transport not found" });
|
| 557 |
+
}
|
| 558 |
|
| 559 |
+
await transport.connect({ dtlsParameters });
|
| 560 |
+
console.log("β
Consumer transport connected:", transportId);
|
| 561 |
+
res.json({ success: true });
|
| 562 |
} catch (error) {
|
| 563 |
+
console.error("β Error connecting consumer transport:", error);
|
| 564 |
res.status(500).json({ error: error.message });
|
| 565 |
}
|
| 566 |
});
|
| 567 |
|
| 568 |
+
app.post("/api/produce", async (req, res) => {
|
|
|
|
| 569 |
try {
|
| 570 |
+
const { transportId, kind, rtpParameters } = req.body;
|
| 571 |
+
const transport = transports.get(transportId);
|
| 572 |
|
| 573 |
+
if (!transport) {
|
| 574 |
+
return res.status(404).json({ error: "Transport not found" });
|
|
|
|
|
|
|
|
|
|
| 575 |
}
|
| 576 |
|
| 577 |
+
const producer = await transport.produce({ kind, rtpParameters });
|
| 578 |
+
producers.set(producer.id, producer);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 579 |
|
| 580 |
+
console.log(`β
${kind} producer created:`, producer.id);
|
| 581 |
+
res.json({ id: producer.id });
|
| 582 |
+
} catch (error) {
|
| 583 |
+
console.error("β Error creating producer:", error);
|
| 584 |
+
res.status(500).json({ error: error.message });
|
| 585 |
+
}
|
| 586 |
+
});
|
| 587 |
+
|
| 588 |
+
app.post("/api/consume", async (req, res) => {
|
| 589 |
+
try {
|
| 590 |
+
const { transportId, rtpCapabilities } = req.body;
|
| 591 |
+
const transport = transports.get(transportId);
|
| 592 |
|
| 593 |
+
if (!transport) {
|
| 594 |
+
return res.status(404).json({ error: "Transport not found" });
|
| 595 |
+
}
|
| 596 |
|
| 597 |
+
// Get first producer (simplified - should iterate over all)
|
| 598 |
+
const producer = Array.from(producers.values())[0];
|
| 599 |
|
| 600 |
+
if (!producer) {
|
| 601 |
+
return res.status(404).json({ error: "No stream available" });
|
| 602 |
+
}
|
| 603 |
|
| 604 |
+
if (!router.canConsume({ producerId: producer.id, rtpCapabilities })) {
|
| 605 |
+
return res.status(400).json({ error: "Cannot consume" });
|
| 606 |
+
}
|
| 607 |
|
| 608 |
+
const consumer = await transport.consume({
|
| 609 |
+
producerId: producer.id,
|
| 610 |
+
rtpCapabilities,
|
| 611 |
+
paused: false,
|
| 612 |
+
});
|
| 613 |
|
| 614 |
+
consumers.set(consumer.id, consumer);
|
| 615 |
|
| 616 |
+
console.log("β
Consumer created:", consumer.id);
|
| 617 |
+
res.json({
|
| 618 |
+
id: consumer.id,
|
| 619 |
+
producerId: producer.id,
|
| 620 |
+
kind: consumer.kind,
|
| 621 |
+
rtpParameters: consumer.rtpParameters,
|
| 622 |
+
});
|
| 623 |
} catch (error) {
|
| 624 |
+
console.error("β Error creating consumer:", error);
|
| 625 |
res.status(500).json({ error: error.message });
|
| 626 |
}
|
| 627 |
});
|
| 628 |
|
|
|
|
| 629 |
app.get("/status", (req, res) => {
|
| 630 |
res.json({
|
| 631 |
hasStream: producers.size > 0,
|
|
|
|
| 636 |
});
|
| 637 |
});
|
| 638 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 639 |
// Start server
|
| 640 |
const PORT = process.env.PORT || 7860;
|
| 641 |
http.createServer(app).listen(PORT, () => {
|
| 642 |
console.log(`\n${"=".repeat(50)}`);
|
| 643 |
+
console.log(`π Server running on port ${PORT}`);
|
| 644 |
console.log(`${"=".repeat(50)}`);
|
| 645 |
console.log(`πΊ Viewer: http://localhost:${PORT}/`);
|
| 646 |
console.log(`πΉ Publisher: http://localhost:${PORT}/publish`);
|
|
|
|
|
|
|
| 647 |
console.log(`${"=".repeat(50)}\n`);
|
| 648 |
});
|