Spaces:
Paused
Paused
Update server.js
Browse files
server.js
CHANGED
|
@@ -160,7 +160,7 @@ app.get("/", (req, res) => {
|
|
| 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' },
|
|
@@ -203,8 +203,16 @@ app.get("/", (req, res) => {
|
|
| 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:
|
| 208 |
fingerprints: [{
|
| 209 |
algorithm: fingerprintLine[1],
|
| 210 |
value: fingerprintLine[2]
|
|
@@ -358,7 +366,7 @@ app.get("/publish", (req, res) => {
|
|
| 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
|
|
@@ -433,8 +441,16 @@ app.get("/publish", (req, res) => {
|
|
| 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:
|
| 438 |
fingerprints: [{
|
| 439 |
algorithm: fingerprintLine[1],
|
| 440 |
value: fingerprintLine[2]
|
|
@@ -443,26 +459,79 @@ app.get("/publish", (req, res) => {
|
|
| 443 |
}
|
| 444 |
|
| 445 |
function extractTrackParameters(sdp, kind) {
|
| 446 |
-
//
|
| 447 |
-
const
|
| 448 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 449 |
|
| 450 |
-
const
|
| 451 |
-
|
| 452 |
-
(kind === 'audio' && m[2] === 'opus')
|
| 453 |
-
);
|
| 454 |
|
| 455 |
-
|
| 456 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 457 |
}
|
| 458 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 459 |
return {
|
| 460 |
-
codecs:
|
| 461 |
-
|
| 462 |
-
clockRate: parseInt(codecMatch[3]),
|
| 463 |
-
payloadType: parseInt(codecMatch[1])
|
| 464 |
-
}],
|
| 465 |
-
encodings: [{ ssrc: Math.floor(Math.random() * 1000000000) }]
|
| 466 |
};
|
| 467 |
}
|
| 468 |
</script>
|
|
|
|
| 160 |
const offer = await pc.createOffer();
|
| 161 |
await pc.setLocalDescription(offer);
|
| 162 |
|
| 163 |
+
// Connect transport with fixed DTLS parameters
|
| 164 |
await fetch('/api/connectConsumerTransport', {
|
| 165 |
method: 'POST',
|
| 166 |
headers: { 'Content-Type': 'application/json' },
|
|
|
|
| 203 |
function extractDtlsParameters(sdp) {
|
| 204 |
const fingerprintLine = sdp.match(/a=fingerprint:(\\S+) (\\S+)/);
|
| 205 |
const setupLine = sdp.match(/a=setup:(\\S+)/);
|
| 206 |
+
let role = 'auto';
|
| 207 |
+
if (setupLine) {
|
| 208 |
+
const setup = setupLine[1];
|
| 209 |
+
// mediasoup only accepts 'client', 'server', or 'auto'
|
| 210 |
+
if (setup === 'active') role = 'client';
|
| 211 |
+
else if (setup === 'passive') role = 'server';
|
| 212 |
+
else if (setup === 'actpass') role = 'auto';
|
| 213 |
+
}
|
| 214 |
return {
|
| 215 |
+
role: role,
|
| 216 |
fingerprints: [{
|
| 217 |
algorithm: fingerprintLine[1],
|
| 218 |
value: fingerprintLine[2]
|
|
|
|
| 366 |
const offer = await pc.createOffer();
|
| 367 |
await pc.setLocalDescription(offer);
|
| 368 |
|
| 369 |
+
// Extract DTLS parameters with proper role conversion
|
| 370 |
const dtlsParameters = extractDtlsParameters(pc.localDescription.sdp);
|
| 371 |
|
| 372 |
// Connect transport
|
|
|
|
| 441 |
function extractDtlsParameters(sdp) {
|
| 442 |
const fingerprintLine = sdp.match(/a=fingerprint:(\\S+) (\\S+)/);
|
| 443 |
const setupLine = sdp.match(/a=setup:(\\S+)/);
|
| 444 |
+
let role = 'auto';
|
| 445 |
+
if (setupLine) {
|
| 446 |
+
const setup = setupLine[1];
|
| 447 |
+
// mediasoup only accepts 'client', 'server', or 'auto'
|
| 448 |
+
if (setup === 'active') role = 'client';
|
| 449 |
+
else if (setup === 'passive') role = 'server';
|
| 450 |
+
else if (setup === 'actpass') role = 'auto';
|
| 451 |
+
}
|
| 452 |
return {
|
| 453 |
+
role: role,
|
| 454 |
fingerprints: [{
|
| 455 |
algorithm: fingerprintLine[1],
|
| 456 |
value: fingerprintLine[2]
|
|
|
|
| 459 |
}
|
| 460 |
|
| 461 |
function extractTrackParameters(sdp, kind) {
|
| 462 |
+
// Parse SDP to get proper RTP parameters
|
| 463 |
+
const lines = sdp.split('\\r\\n');
|
| 464 |
+
const mediaSection = [];
|
| 465 |
+
let inSection = false;
|
| 466 |
+
|
| 467 |
+
for (const line of lines) {
|
| 468 |
+
if (line.startsWith('m=' + kind)) {
|
| 469 |
+
inSection = true;
|
| 470 |
+
} else if (line.startsWith('m=') && inSection) {
|
| 471 |
+
break;
|
| 472 |
+
}
|
| 473 |
+
if (inSection) {
|
| 474 |
+
mediaSection.push(line);
|
| 475 |
+
}
|
| 476 |
+
}
|
| 477 |
+
|
| 478 |
+
// Extract codec info
|
| 479 |
+
const rtpmapRegex = /a=rtpmap:(\\d+) ([^\\/\\s]+)\\/(\\d+)(?:\\/(\\d+))?/;
|
| 480 |
+
const fmtpRegex = /a=fmtp:(\\d+) (.+)/;
|
| 481 |
|
| 482 |
+
const codecs = [];
|
| 483 |
+
const payloadTypes = new Set();
|
|
|
|
|
|
|
| 484 |
|
| 485 |
+
for (const line of mediaSection) {
|
| 486 |
+
const rtpmapMatch = line.match(rtpmapRegex);
|
| 487 |
+
if (rtpmapMatch) {
|
| 488 |
+
const payloadType = parseInt(rtpmapMatch[1]);
|
| 489 |
+
const codecName = rtpmapMatch[2];
|
| 490 |
+
const clockRate = parseInt(rtpmapMatch[3]);
|
| 491 |
+
const channels = rtpmapMatch[4] ? parseInt(rtpmapMatch[4]) : undefined;
|
| 492 |
+
|
| 493 |
+
// Only include codecs that match our kind
|
| 494 |
+
if ((kind === 'video' && (codecName === 'VP8' || codecName === 'H264')) ||
|
| 495 |
+
(kind === 'audio' && codecName.toLowerCase() === 'opus')) {
|
| 496 |
+
|
| 497 |
+
const codec = {
|
| 498 |
+
mimeType: \`\${kind}/\${codecName}\`,
|
| 499 |
+
clockRate: clockRate,
|
| 500 |
+
payloadType: payloadType
|
| 501 |
+
};
|
| 502 |
+
|
| 503 |
+
if (channels) {
|
| 504 |
+
codec.channels = channels;
|
| 505 |
+
}
|
| 506 |
+
|
| 507 |
+
// Look for matching fmtp line
|
| 508 |
+
for (const fmtpLine of mediaSection) {
|
| 509 |
+
const fmtpMatch = fmtpLine.match(fmtpRegex);
|
| 510 |
+
if (fmtpMatch && parseInt(fmtpMatch[1]) === payloadType) {
|
| 511 |
+
codec.parameters = {};
|
| 512 |
+
const params = fmtpMatch[2].split(';');
|
| 513 |
+
for (const param of params) {
|
| 514 |
+
const [key, value] = param.trim().split('=');
|
| 515 |
+
if (key && value) {
|
| 516 |
+
codec.parameters[key.trim()] = value.trim();
|
| 517 |
+
}
|
| 518 |
+
}
|
| 519 |
+
}
|
| 520 |
+
}
|
| 521 |
+
|
| 522 |
+
codecs.push(codec);
|
| 523 |
+
payloadTypes.add(payloadType);
|
| 524 |
+
}
|
| 525 |
+
}
|
| 526 |
}
|
| 527 |
|
| 528 |
+
// Extract SSRC
|
| 529 |
+
const ssrcMatch = mediaSection.find(l => l.startsWith('a=ssrc:'));
|
| 530 |
+
const ssrc = ssrcMatch ? parseInt(ssrcMatch.split(':')[1].split(' ')[0]) : Math.floor(Math.random() * 1000000000);
|
| 531 |
+
|
| 532 |
return {
|
| 533 |
+
codecs: codecs,
|
| 534 |
+
encodings: [{ ssrc: ssrc }]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 535 |
};
|
| 536 |
}
|
| 537 |
</script>
|