Spaces:
Paused
Paused
Update server.js
Browse files
server.js
CHANGED
|
@@ -144,7 +144,27 @@ app.get("/", (req, res) => {
|
|
| 144 |
consumeTransportId = transportData.id;
|
| 145 |
|
| 146 |
// Create peer connection
|
| 147 |
-
pc = new RTCPeerConnection(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
// Add transceivers
|
| 150 |
pc.addTransceiver('video', { direction: 'recvonly' });
|
|
@@ -152,7 +172,9 @@ app.get("/", (req, res) => {
|
|
| 152 |
|
| 153 |
pc.ontrack = (event) => {
|
| 154 |
console.log('πΉ Received track:', event.track.kind);
|
| 155 |
-
video.srcObject
|
|
|
|
|
|
|
| 156 |
status.className = 'status live';
|
| 157 |
status.textContent = 'π΄ LIVE';
|
| 158 |
};
|
|
@@ -161,6 +183,22 @@ app.get("/", (req, res) => {
|
|
| 161 |
const offer = await pc.createOffer();
|
| 162 |
await pc.setLocalDescription(offer);
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
// Connect transport with fixed DTLS parameters
|
| 165 |
await fetch('/api/connectConsumerTransport', {
|
| 166 |
method: 'POST',
|
|
@@ -579,11 +617,21 @@ app.get("/api/rtpCapabilities", (req, res) => {
|
|
| 579 |
app.post("/api/createProducerTransport", async (req, res) => {
|
| 580 |
try {
|
| 581 |
console.log("π₯ Creating producer transport");
|
|
|
|
|
|
|
|
|
|
|
|
|
| 582 |
const transport = await router.createWebRtcTransport({
|
| 583 |
-
listenIps: [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 584 |
enableUdp: true,
|
| 585 |
enableTcp: true,
|
| 586 |
preferUdp: true,
|
|
|
|
| 587 |
});
|
| 588 |
|
| 589 |
transports.set(transport.id, transport);
|
|
@@ -605,11 +653,21 @@ app.post("/api/createProducerTransport", async (req, res) => {
|
|
| 605 |
app.post("/api/createConsumerTransport", async (req, res) => {
|
| 606 |
try {
|
| 607 |
console.log("ποΈ Creating consumer transport");
|
|
|
|
|
|
|
|
|
|
|
|
|
| 608 |
const transport = await router.createWebRtcTransport({
|
| 609 |
-
listenIps: [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 610 |
enableUdp: true,
|
| 611 |
enableTcp: true,
|
| 612 |
preferUdp: true,
|
|
|
|
| 613 |
});
|
| 614 |
|
| 615 |
transports.set(transport.id, transport);
|
|
|
|
| 144 |
consumeTransportId = transportData.id;
|
| 145 |
|
| 146 |
// Create peer connection
|
| 147 |
+
pc = new RTCPeerConnection({
|
| 148 |
+
iceServers: [
|
| 149 |
+
{ urls: 'stun:stun.l.google.com:19302' }
|
| 150 |
+
]
|
| 151 |
+
});
|
| 152 |
+
|
| 153 |
+
// Monitor ICE connection state
|
| 154 |
+
pc.oniceconnectionstatechange = () => {
|
| 155 |
+
console.log('π§ ICE connection state:', pc.iceConnectionState);
|
| 156 |
+
if (pc.iceConnectionState === 'failed' || pc.iceConnectionState === 'disconnected') {
|
| 157 |
+
status.className = 'status error';
|
| 158 |
+
status.textContent = 'β Connection failed - check network';
|
| 159 |
+
}
|
| 160 |
+
if (pc.iceConnectionState === 'connected') {
|
| 161 |
+
console.log('β
ICE connected');
|
| 162 |
+
}
|
| 163 |
+
};
|
| 164 |
+
|
| 165 |
+
pc.onconnectionstatechange = () => {
|
| 166 |
+
console.log('π Connection state:', pc.connectionState);
|
| 167 |
+
};
|
| 168 |
|
| 169 |
// Add transceivers
|
| 170 |
pc.addTransceiver('video', { direction: 'recvonly' });
|
|
|
|
| 172 |
|
| 173 |
pc.ontrack = (event) => {
|
| 174 |
console.log('πΉ Received track:', event.track.kind);
|
| 175 |
+
if (!video.srcObject) {
|
| 176 |
+
video.srcObject = event.streams[0];
|
| 177 |
+
}
|
| 178 |
status.className = 'status live';
|
| 179 |
status.textContent = 'π΄ LIVE';
|
| 180 |
};
|
|
|
|
| 183 |
const offer = await pc.createOffer();
|
| 184 |
await pc.setLocalDescription(offer);
|
| 185 |
|
| 186 |
+
// Wait for ICE gathering to complete
|
| 187 |
+
if (pc.iceGatheringState !== 'complete') {
|
| 188 |
+
await new Promise(resolve => {
|
| 189 |
+
const checkState = () => {
|
| 190 |
+
if (pc.iceGatheringState === 'complete') {
|
| 191 |
+
pc.removeEventListener('icegatheringstatechange', checkState);
|
| 192 |
+
resolve();
|
| 193 |
+
}
|
| 194 |
+
};
|
| 195 |
+
pc.addEventListener('icegatheringstatechange', checkState);
|
| 196 |
+
checkState();
|
| 197 |
+
});
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
console.log('β
ICE gathering complete');
|
| 201 |
+
|
| 202 |
// Connect transport with fixed DTLS parameters
|
| 203 |
await fetch('/api/connectConsumerTransport', {
|
| 204 |
method: 'POST',
|
|
|
|
| 617 |
app.post("/api/createProducerTransport", async (req, res) => {
|
| 618 |
try {
|
| 619 |
console.log("π₯ Creating producer transport");
|
| 620 |
+
|
| 621 |
+
// Get the host from request headers for announced IP
|
| 622 |
+
const host = req.headers.host?.split(':')[0] || req.hostname;
|
| 623 |
+
|
| 624 |
const transport = await router.createWebRtcTransport({
|
| 625 |
+
listenIps: [
|
| 626 |
+
{
|
| 627 |
+
ip: "0.0.0.0",
|
| 628 |
+
announcedIp: host // Use the actual domain/IP that clients connect to
|
| 629 |
+
}
|
| 630 |
+
],
|
| 631 |
enableUdp: true,
|
| 632 |
enableTcp: true,
|
| 633 |
preferUdp: true,
|
| 634 |
+
initialAvailableOutgoingBitrate: 1000000,
|
| 635 |
});
|
| 636 |
|
| 637 |
transports.set(transport.id, transport);
|
|
|
|
| 653 |
app.post("/api/createConsumerTransport", async (req, res) => {
|
| 654 |
try {
|
| 655 |
console.log("ποΈ Creating consumer transport");
|
| 656 |
+
|
| 657 |
+
// Get the host from request headers for announced IP
|
| 658 |
+
const host = req.headers.host?.split(':')[0] || req.hostname;
|
| 659 |
+
|
| 660 |
const transport = await router.createWebRtcTransport({
|
| 661 |
+
listenIps: [
|
| 662 |
+
{
|
| 663 |
+
ip: "0.0.0.0",
|
| 664 |
+
announcedIp: host // Use the actual domain/IP that clients connect to
|
| 665 |
+
}
|
| 666 |
+
],
|
| 667 |
enableUdp: true,
|
| 668 |
enableTcp: true,
|
| 669 |
preferUdp: true,
|
| 670 |
+
initialAvailableOutgoingBitrate: 1000000,
|
| 671 |
});
|
| 672 |
|
| 673 |
transports.set(transport.id, transport);
|