veltrixcode commited on
Commit
2d55333
·
verified ·
1 Parent(s): 6e12074

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +88 -19
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: setupLine ? setupLine[1] : 'auto',
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: setupLine ? (setupLine[1] === 'active' ? 'client' : 'server') : 'auto',
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
- // 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>
 
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>