Spaces:
Paused
Paused
Update server.js
Browse files
server.js
CHANGED
|
@@ -1,12 +1,18 @@
|
|
| 1 |
import express from "express";
|
| 2 |
import http from "http";
|
| 3 |
import mediasoup from "mediasoup";
|
|
|
|
| 4 |
|
| 5 |
const app = express();
|
|
|
|
|
|
|
| 6 |
app.use(express.json());
|
| 7 |
|
| 8 |
console.log("π Starting mediasoup WHIP/WHEP server...");
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
// Create mediasoup worker
|
| 11 |
const worker = await mediasoup.createWorker({
|
| 12 |
logLevel: "warn",
|
|
@@ -46,8 +52,9 @@ const router = await worker.createRouter({
|
|
| 46 |
|
| 47 |
console.log("β
mediasoup router created");
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
// Middleware for logging requests
|
| 53 |
app.use((req, res, next) => {
|
|
@@ -55,6 +62,258 @@ app.use((req, res, next) => {
|
|
| 55 |
next();
|
| 56 |
});
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
// Serve viewer HTML on root route
|
| 59 |
app.get("/", (req, res) => {
|
| 60 |
res.send(`
|
|
@@ -164,9 +423,9 @@ app.get("/", (req, res) => {
|
|
| 164 |
|
| 165 |
<div class="info">
|
| 166 |
<strong>Instructions:</strong><br>
|
| 167 |
-
1. Start streaming
|
| 168 |
2. Click "Start Viewing" to watch the stream<br>
|
| 169 |
-
3.
|
| 170 |
</div>
|
| 171 |
</div>
|
| 172 |
|
|
@@ -174,6 +433,7 @@ app.get("/", (req, res) => {
|
|
| 174 |
const video = document.getElementById('video');
|
| 175 |
const status = document.getElementById('status');
|
| 176 |
const playBtn = document.getElementById('playBtn');
|
|
|
|
| 177 |
|
| 178 |
let pc = null;
|
| 179 |
|
|
@@ -195,13 +455,18 @@ app.get("/", (req, res) => {
|
|
| 195 |
|
| 196 |
// Create peer connection
|
| 197 |
pc = new RTCPeerConnection({
|
| 198 |
-
iceServers: [
|
|
|
|
|
|
|
|
|
|
| 199 |
});
|
| 200 |
|
| 201 |
// Handle incoming tracks
|
| 202 |
pc.ontrack = (event) => {
|
| 203 |
console.log('πΉ Received track:', event.track.kind);
|
| 204 |
-
video.srcObject
|
|
|
|
|
|
|
| 205 |
updateStatus('π΄ LIVE', 'live');
|
| 206 |
playBtn.style.display = 'none';
|
| 207 |
};
|
|
@@ -216,7 +481,7 @@ app.get("/", (req, res) => {
|
|
| 216 |
}
|
| 217 |
};
|
| 218 |
|
| 219 |
-
// Add
|
| 220 |
pc.addTransceiver('video', { direction: 'recvonly' });
|
| 221 |
pc.addTransceiver('audio', { direction: 'recvonly' });
|
| 222 |
|
|
@@ -229,14 +494,18 @@ app.get("/", (req, res) => {
|
|
| 229 |
if (pc.iceGatheringState === 'complete') {
|
| 230 |
resolve();
|
| 231 |
} else {
|
| 232 |
-
|
| 233 |
-
if (pc.iceGatheringState === 'complete')
|
|
|
|
|
|
|
|
|
|
| 234 |
};
|
|
|
|
| 235 |
}
|
| 236 |
});
|
| 237 |
|
| 238 |
// Send WHEP request
|
| 239 |
-
const response = await fetch(
|
| 240 |
method: 'POST',
|
| 241 |
headers: { 'Content-Type': 'application/sdp' },
|
| 242 |
body: pc.localDescription.sdp
|
|
@@ -281,36 +550,42 @@ app.post("/whip", async (req, res) => {
|
|
| 281 |
try {
|
| 282 |
console.log("π₯ WHIP publish request received");
|
| 283 |
|
| 284 |
-
const offerSdp = req.body;
|
| 285 |
-
console.log("π Received SDP offer");
|
| 286 |
-
|
| 287 |
// Create transport for producer
|
| 288 |
-
|
| 289 |
-
listenIps: [{
|
|
|
|
|
|
|
|
|
|
| 290 |
enableUdp: true,
|
| 291 |
enableTcp: true,
|
| 292 |
preferUdp: true,
|
| 293 |
});
|
| 294 |
|
| 295 |
-
|
|
|
|
| 296 |
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
|
|
|
|
|
|
| 302 |
|
| 303 |
// Set up transport listeners
|
| 304 |
-
|
| 305 |
console.log("π DTLS state:", dtlsState);
|
| 306 |
-
if (dtlsState === "
|
| 307 |
-
|
| 308 |
}
|
| 309 |
});
|
| 310 |
|
|
|
|
|
|
|
|
|
|
| 311 |
res.setHeader("Content-Type", "application/sdp");
|
| 312 |
-
res.setHeader("Location",
|
| 313 |
-
res.status(201).send(
|
| 314 |
|
| 315 |
console.log("β
WHIP answer sent");
|
| 316 |
|
|
@@ -325,48 +600,41 @@ app.post("/whep", async (req, res) => {
|
|
| 325 |
try {
|
| 326 |
console.log("ποΈ WHEP view request received");
|
| 327 |
|
| 328 |
-
if
|
|
|
|
|
|
|
| 329 |
console.log("β οΈ No active stream");
|
| 330 |
return res.status(404).send("No stream available");
|
| 331 |
}
|
| 332 |
|
| 333 |
-
const offerSdp = req.body;
|
| 334 |
-
console.log("π Received WHEP SDP offer");
|
| 335 |
-
|
| 336 |
// Create transport for consumer
|
| 337 |
-
const
|
| 338 |
-
listenIps: [{
|
|
|
|
|
|
|
|
|
|
| 339 |
enableUdp: true,
|
| 340 |
enableTcp: true,
|
| 341 |
preferUdp: true,
|
| 342 |
});
|
| 343 |
|
| 344 |
-
|
|
|
|
| 345 |
|
| 346 |
-
|
| 347 |
-
const consumer = await consumerTransport.consume({
|
| 348 |
-
producerId: producer.id,
|
| 349 |
-
rtpCapabilities: router.rtpCapabilities,
|
| 350 |
-
paused: false,
|
| 351 |
-
});
|
| 352 |
|
| 353 |
-
|
| 354 |
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
sdp: generateAnswerSdp(consumerTransport, offerSdp, consumer),
|
| 358 |
-
};
|
| 359 |
|
| 360 |
-
|
| 361 |
console.log("π Consumer DTLS state:", dtlsState);
|
| 362 |
-
if (dtlsState === "closed") {
|
| 363 |
-
consumerTransport.close();
|
| 364 |
-
}
|
| 365 |
});
|
| 366 |
|
| 367 |
res.setHeader("Content-Type", "application/sdp");
|
| 368 |
-
res.setHeader("Location",
|
| 369 |
-
res.status(201).send(
|
| 370 |
|
| 371 |
console.log("β
WHEP answer sent");
|
| 372 |
|
|
@@ -379,26 +647,30 @@ app.post("/whep", async (req, res) => {
|
|
| 379 |
// Status endpoint
|
| 380 |
app.get("/status", (req, res) => {
|
| 381 |
res.json({
|
| 382 |
-
hasStream:
|
| 383 |
workerPid: worker.pid,
|
|
|
|
|
|
|
|
|
|
| 384 |
});
|
| 385 |
});
|
| 386 |
|
| 387 |
-
// Helper function to
|
| 388 |
-
function
|
| 389 |
const ice = transport.iceParameters;
|
| 390 |
const dtls = transport.dtlsParameters;
|
|
|
|
| 391 |
|
| 392 |
let sdp = `v=0
|
| 393 |
-
o=- 0 0 IN IP4
|
| 394 |
s=-
|
| 395 |
t=0 0
|
| 396 |
a=group:BUNDLE 0
|
| 397 |
a=msid-semantic: WMS *
|
| 398 |
`;
|
| 399 |
|
| 400 |
-
|
| 401 |
-
|
| 402 |
c=IN IP4 0.0.0.0
|
| 403 |
a=rtcp:9 IN IP4 0.0.0.0
|
| 404 |
a=ice-ufrag:${ice.usernameFragment}
|
|
@@ -407,40 +679,33 @@ a=ice-options:trickle
|
|
| 407 |
a=fingerprint:${dtls.fingerprints[0].algorithm} ${dtls.fingerprints[0].value}
|
| 408 |
a=setup:active
|
| 409 |
a=mid:0
|
| 410 |
-
a=recvonly
|
| 411 |
-
a=rtcp-mux
|
| 412 |
-
a=rtcp-rsize
|
| 413 |
-
a=rtpmap:96 VP8/90000
|
| 414 |
-
a=ssrc:${consumer.rtpParameters.encodings[0].ssrc} cname:mediasoup
|
| 415 |
-
`;
|
| 416 |
-
} else {
|
| 417 |
-
sdp += `m=video 9 UDP/TLS/RTP/SAVPF 96
|
| 418 |
-
c=IN IP4 0.0.0.0
|
| 419 |
-
a=rtcp:9 IN IP4 0.0.0.0
|
| 420 |
-
a=ice-ufrag:${ice.usernameFragment}
|
| 421 |
-
a=ice-pwd:${ice.password}
|
| 422 |
-
a=ice-options:trickle
|
| 423 |
-
a=fingerprint:${dtls.fingerprints[0].algorithm} ${dtls.fingerprints[0].value}
|
| 424 |
-
a=setup:passive
|
| 425 |
-
a=mid:0
|
| 426 |
a=sendrecv
|
| 427 |
a=rtcp-mux
|
| 428 |
a=rtcp-rsize
|
| 429 |
a=rtpmap:96 VP8/90000
|
| 430 |
`;
|
| 431 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
|
| 433 |
return sdp;
|
| 434 |
}
|
| 435 |
|
| 436 |
// Start server
|
| 437 |
-
const PORT = 7860;
|
| 438 |
http.createServer(app).listen(PORT, () => {
|
| 439 |
console.log(`\n${"=".repeat(50)}`);
|
| 440 |
console.log(`π WHIP/WHEP Server running on port ${PORT}`);
|
| 441 |
console.log(`${"=".repeat(50)}`);
|
| 442 |
-
console.log(`πΊ Viewer:
|
| 443 |
-
console.log(`
|
| 444 |
-
console.log(`
|
|
|
|
| 445 |
console.log(`${"=".repeat(50)}\n`);
|
| 446 |
});
|
|
|
|
| 1 |
import express from "express";
|
| 2 |
import http from "http";
|
| 3 |
import mediasoup from "mediasoup";
|
| 4 |
+
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",
|
|
|
|
| 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 requests
|
| 60 |
app.use((req, res, next) => {
|
|
|
|
| 62 |
next();
|
| 63 |
});
|
| 64 |
|
| 65 |
+
// Serve publisher HTML
|
| 66 |
+
app.get("/publish", (req, res) => {
|
| 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>WHIP Publisher</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, #f093fb 0%, #f5576c 100%);
|
| 79 |
+
min-height: 100vh;
|
| 80 |
+
display: flex;
|
| 81 |
+
align-items: center;
|
| 82 |
+
justify-content: center;
|
| 83 |
+
padding: 20px;
|
| 84 |
+
}
|
| 85 |
+
.container {
|
| 86 |
+
background: white;
|
| 87 |
+
border-radius: 20px;
|
| 88 |
+
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
|
| 89 |
+
padding: 40px;
|
| 90 |
+
max-width: 800px;
|
| 91 |
+
width: 100%;
|
| 92 |
+
}
|
| 93 |
+
h1 {
|
| 94 |
+
color: #333;
|
| 95 |
+
margin-bottom: 20px;
|
| 96 |
+
font-size: 2em;
|
| 97 |
+
}
|
| 98 |
+
video {
|
| 99 |
+
width: 100%;
|
| 100 |
+
border-radius: 12px;
|
| 101 |
+
background: #000;
|
| 102 |
+
margin: 20px 0;
|
| 103 |
+
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
|
| 104 |
+
}
|
| 105 |
+
.controls {
|
| 106 |
+
display: flex;
|
| 107 |
+
gap: 10px;
|
| 108 |
+
margin: 20px 0;
|
| 109 |
+
flex-wrap: wrap;
|
| 110 |
+
}
|
| 111 |
+
button {
|
| 112 |
+
background: #f5576c;
|
| 113 |
+
color: white;
|
| 114 |
+
border: none;
|
| 115 |
+
padding: 12px 30px;
|
| 116 |
+
border-radius: 8px;
|
| 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>πΉ WHIP Publisher</h1>
|
| 159 |
+
<div id="status" class="status idle">Ready to stream</div>
|
| 160 |
+
|
| 161 |
+
<video id="preview" autoplay muted playsinline></video>
|
| 162 |
+
|
| 163 |
+
<div class="controls">
|
| 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 |
+
let stream = null;
|
| 182 |
+
let pc = null;
|
| 183 |
+
const WHIP_ENDPOINT = window.location.origin + '/whip';
|
| 184 |
+
const status = document.getElementById('status');
|
| 185 |
+
const startBtn = document.getElementById('startBtn');
|
| 186 |
+
const stopBtn = document.getElementById('stopBtn');
|
| 187 |
+
|
| 188 |
+
function updateStatus(msg, type = 'idle') {
|
| 189 |
+
status.className = 'status ' + type;
|
| 190 |
+
status.textContent = msg;
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
async function startStream() {
|
| 194 |
+
try {
|
| 195 |
+
updateStatus('Getting media...', 'idle');
|
| 196 |
+
startBtn.disabled = true;
|
| 197 |
+
|
| 198 |
+
const sourceType = document.getElementById('sourceType').value;
|
| 199 |
+
|
| 200 |
+
// Get media stream
|
| 201 |
+
if (sourceType === 'camera') {
|
| 202 |
+
stream = await navigator.mediaDevices.getUserMedia({
|
| 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 |
+
document.getElementById('preview').srcObject = stream;
|
| 214 |
+
updateStatus('Connecting to server...', 'idle');
|
| 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 tracks
|
| 225 |
+
stream.getTracks().forEach(track => {
|
| 226 |
+
console.log('Adding track:', track.kind);
|
| 227 |
+
pc.addTrack(track, stream);
|
| 228 |
+
});
|
| 229 |
+
|
| 230 |
+
// Monitor connection
|
| 231 |
+
pc.oniceconnectionstatechange = () => {
|
| 232 |
+
console.log('ICE connection state:', pc.iceConnectionState);
|
| 233 |
+
if (pc.iceConnectionState === 'failed' || pc.iceConnectionState === 'disconnected') {
|
| 234 |
+
updateStatus('β Connection lost', 'error');
|
| 235 |
+
}
|
| 236 |
+
};
|
| 237 |
+
|
| 238 |
+
// Create offer
|
| 239 |
+
const offer = await pc.createOffer();
|
| 240 |
+
await pc.setLocalDescription(offer);
|
| 241 |
+
|
| 242 |
+
// Wait for ICE gathering
|
| 243 |
+
updateStatus('Gathering connection info...', 'idle');
|
| 244 |
+
await new Promise(resolve => {
|
| 245 |
+
if (pc.iceGatheringState === 'complete') {
|
| 246 |
+
resolve();
|
| 247 |
+
} else {
|
| 248 |
+
const checkState = () => {
|
| 249 |
+
if (pc.iceGatheringState === 'complete') {
|
| 250 |
+
pc.removeEventListener('icegatheringstatechange', checkState);
|
| 251 |
+
resolve();
|
| 252 |
+
}
|
| 253 |
+
};
|
| 254 |
+
pc.addEventListener('icegatheringstatechange', checkState);
|
| 255 |
+
}
|
| 256 |
+
});
|
| 257 |
+
|
| 258 |
+
console.log('Sending offer to WHIP endpoint...');
|
| 259 |
+
|
| 260 |
+
// Send to WHIP endpoint
|
| 261 |
+
const response = await fetch(WHIP_ENDPOINT, {
|
| 262 |
+
method: 'POST',
|
| 263 |
+
headers: {
|
| 264 |
+
'Content-Type': 'application/sdp'
|
| 265 |
+
},
|
| 266 |
+
body: pc.localDescription.sdp
|
| 267 |
+
});
|
| 268 |
+
|
| 269 |
+
if (!response.ok) {
|
| 270 |
+
const errorText = await response.text();
|
| 271 |
+
throw new Error('WHIP failed: ' + response.status + ' - ' + errorText);
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
const answerSdp = await response.text();
|
| 275 |
+
console.log('Received answer from server');
|
| 276 |
+
|
| 277 |
+
await pc.setRemoteDescription({
|
| 278 |
+
type: 'answer',
|
| 279 |
+
sdp: answerSdp
|
| 280 |
+
});
|
| 281 |
+
|
| 282 |
+
updateStatus('π΄ STREAMING LIVE', 'streaming');
|
| 283 |
+
stopBtn.disabled = false;
|
| 284 |
+
|
| 285 |
+
console.log('β
Streaming started successfully');
|
| 286 |
+
|
| 287 |
+
} catch (error) {
|
| 288 |
+
console.error('Error:', error);
|
| 289 |
+
updateStatus('β Error: ' + error.message, 'error');
|
| 290 |
+
startBtn.disabled = false;
|
| 291 |
+
if (stream) {
|
| 292 |
+
stream.getTracks().forEach(t => t.stop());
|
| 293 |
+
}
|
| 294 |
+
}
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
function stopStream() {
|
| 298 |
+
if (pc) {
|
| 299 |
+
pc.close();
|
| 300 |
+
pc = null;
|
| 301 |
+
}
|
| 302 |
+
if (stream) {
|
| 303 |
+
stream.getTracks().forEach(t => t.stop());
|
| 304 |
+
stream = null;
|
| 305 |
+
}
|
| 306 |
+
document.getElementById('preview').srcObject = null;
|
| 307 |
+
updateStatus('Stream stopped', 'idle');
|
| 308 |
+
startBtn.disabled = false;
|
| 309 |
+
stopBtn.disabled = true;
|
| 310 |
+
}
|
| 311 |
+
</script>
|
| 312 |
+
</body>
|
| 313 |
+
</html>
|
| 314 |
+
`);
|
| 315 |
+
});
|
| 316 |
+
|
| 317 |
// Serve viewer HTML on root route
|
| 318 |
app.get("/", (req, res) => {
|
| 319 |
res.send(`
|
|
|
|
| 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 |
|
|
|
|
| 433 |
const video = document.getElementById('video');
|
| 434 |
const status = document.getElementById('status');
|
| 435 |
const playBtn = document.getElementById('playBtn');
|
| 436 |
+
const WHEP_ENDPOINT = window.location.origin + '/whep';
|
| 437 |
|
| 438 |
let pc = null;
|
| 439 |
|
|
|
|
| 455 |
|
| 456 |
// Create peer connection
|
| 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 |
// Handle incoming tracks
|
| 465 |
pc.ontrack = (event) => {
|
| 466 |
console.log('πΉ Received track:', event.track.kind);
|
| 467 |
+
if (!video.srcObject) {
|
| 468 |
+
video.srcObject = event.streams[0];
|
| 469 |
+
}
|
| 470 |
updateStatus('π΄ LIVE', 'live');
|
| 471 |
playBtn.style.display = 'none';
|
| 472 |
};
|
|
|
|
| 481 |
}
|
| 482 |
};
|
| 483 |
|
| 484 |
+
// Add transceivers
|
| 485 |
pc.addTransceiver('video', { direction: 'recvonly' });
|
| 486 |
pc.addTransceiver('audio', { direction: 'recvonly' });
|
| 487 |
|
|
|
|
| 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 |
// Send WHEP request
|
| 508 |
+
const response = await fetch(WHEP_ENDPOINT, {
|
| 509 |
method: 'POST',
|
| 510 |
headers: { 'Content-Type': 'application/sdp' },
|
| 511 |
body: pc.localDescription.sdp
|
|
|
|
| 550 |
try {
|
| 551 |
console.log("π₯ WHIP publish request received");
|
| 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 |
+
const transportId = transport.id;
|
| 565 |
+
transports.set(transportId, transport);
|
| 566 |
|
| 567 |
+
console.log("β
Producer transport created:", transportId);
|
| 568 |
+
|
| 569 |
+
// Parse SDP offer
|
| 570 |
+
const offerSdp = req.body;
|
| 571 |
+
|
| 572 |
+
// Generate answer SDP
|
| 573 |
+
const answerSdp = createAnswerSdp(transport, offerSdp);
|
| 574 |
|
| 575 |
// Set up transport listeners
|
| 576 |
+
transport.on("dtlsstatechange", async (dtlsState) => {
|
| 577 |
console.log("π DTLS state:", dtlsState);
|
| 578 |
+
if (dtlsState === "connected") {
|
| 579 |
+
console.log("β
DTLS connected, ready to produce");
|
| 580 |
}
|
| 581 |
});
|
| 582 |
|
| 583 |
+
// Store for later producer creation
|
| 584 |
+
transport.appData = { offerSdp };
|
| 585 |
+
|
| 586 |
res.setHeader("Content-Type", "application/sdp");
|
| 587 |
+
res.setHeader("Location", `/whip/${transportId}`);
|
| 588 |
+
res.status(201).send(answerSdp);
|
| 589 |
|
| 590 |
console.log("β
WHIP answer sent");
|
| 591 |
|
|
|
|
| 600 |
try {
|
| 601 |
console.log("ποΈ WHEP view request received");
|
| 602 |
|
| 603 |
+
// Check if we have any producers
|
| 604 |
+
const hasProducers = producers.size > 0;
|
| 605 |
+
if (!hasProducers) {
|
| 606 |
console.log("β οΈ No active stream");
|
| 607 |
return res.status(404).send("No stream available");
|
| 608 |
}
|
| 609 |
|
|
|
|
|
|
|
|
|
|
| 610 |
// Create transport for consumer
|
| 611 |
+
const transport = await router.createWebRtcTransport({
|
| 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 |
+
const transportId = transport.id;
|
| 622 |
+
transports.set(transportId, transport);
|
| 623 |
|
| 624 |
+
console.log("β
Consumer transport created:", transportId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 625 |
|
| 626 |
+
const offerSdp = req.body;
|
| 627 |
|
| 628 |
+
// Generate answer SDP
|
| 629 |
+
const answerSdp = createAnswerSdp(transport, offerSdp);
|
|
|
|
|
|
|
| 630 |
|
| 631 |
+
transport.on("dtlsstatechange", (dtlsState) => {
|
| 632 |
console.log("π Consumer DTLS state:", dtlsState);
|
|
|
|
|
|
|
|
|
|
| 633 |
});
|
| 634 |
|
| 635 |
res.setHeader("Content-Type", "application/sdp");
|
| 636 |
+
res.setHeader("Location", `/whep/${transportId}`);
|
| 637 |
+
res.status(201).send(answerSdp);
|
| 638 |
|
| 639 |
console.log("β
WHEP answer sent");
|
| 640 |
|
|
|
|
| 647 |
// Status endpoint
|
| 648 |
app.get("/status", (req, res) => {
|
| 649 |
res.json({
|
| 650 |
+
hasStream: producers.size > 0,
|
| 651 |
workerPid: worker.pid,
|
| 652 |
+
producers: producers.size,
|
| 653 |
+
consumers: consumers.size,
|
| 654 |
+
transports: transports.size,
|
| 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}
|
|
|
|
| 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(`π WHIP/WHEP Server running on port ${PORT}`);
|
| 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 |
});
|