veltrixcode commited on
Commit
7dea6c4
Β·
verified Β·
1 Parent(s): 08c86a2

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +41 -18
server.js CHANGED
@@ -31,16 +31,7 @@ const router = await worker.createRouter({
31
  kind: "video",
32
  mimeType: "video/VP8",
33
  clockRate: 90000,
34
- },
35
- {
36
- kind: "video",
37
- mimeType: "video/H264",
38
- clockRate: 90000,
39
- parameters: {
40
- "level-asymmetry-allowed": 1,
41
- "packetization-mode": 1,
42
- "profile-level-id": "42e01f",
43
- },
44
  },
45
  ],
46
  });
@@ -196,10 +187,6 @@ app.get("/", (req, res) => {
196
 
197
  const { rtpParameters, id: consumerId } = await consumeResponse.json();
198
 
199
- // Add track info to answer
200
- const answer = await pc.createAnswer();
201
- await pc.setRemoteDescription({ type: 'answer', sdp: answer.sdp });
202
-
203
  console.log('βœ… Viewing started');
204
 
205
  } catch (error) {
@@ -393,6 +380,16 @@ app.get("/publish", (req, res) => {
393
  const videoTrack = stream.getVideoTracks()[0];
394
  if (videoTrack) {
395
  const videoParams = extractTrackParameters(pc.localDescription.sdp, 'video');
 
 
 
 
 
 
 
 
 
 
396
  const videoResponse = await fetch('/api/produce', {
397
  method: 'POST',
398
  headers: { 'Content-Type': 'application/json' },
@@ -402,6 +399,12 @@ app.get("/publish", (req, res) => {
402
  rtpParameters: videoParams
403
  })
404
  });
 
 
 
 
 
 
405
  const { id } = await videoResponse.json();
406
  videoProducerId = id;
407
  console.log('βœ… Video producer created:', id);
@@ -411,6 +414,9 @@ app.get("/publish", (req, res) => {
411
  const audioTrack = stream.getAudioTracks()[0];
412
  if (audioTrack) {
413
  const audioParams = extractTrackParameters(pc.localDescription.sdp, 'audio');
 
 
 
414
  const audioResponse = await fetch('/api/produce', {
415
  method: 'POST',
416
  headers: { 'Content-Type': 'application/json' },
@@ -420,6 +426,12 @@ app.get("/publish", (req, res) => {
420
  rtpParameters: audioParams
421
  })
422
  });
 
 
 
 
 
 
423
  const { id } = await audioResponse.json();
424
  audioProducerId = id;
425
  console.log('βœ… Audio producer created:', id);
@@ -501,7 +513,7 @@ app.get("/publish", (req, res) => {
501
  const channels = rtpmapMatch[4] ? parseInt(rtpmapMatch[4]) : undefined;
502
 
503
  // Only include codecs that match our kind
504
- if ((kind === 'video' && (codecName === 'VP8' || codecName === 'H264')) ||
505
  (kind === 'audio' && codecName.toLowerCase() === 'opus')) {
506
 
507
  const codec = {
@@ -521,9 +533,12 @@ app.get("/publish", (req, res) => {
521
  codec.parameters = {};
522
  const params = fmtpMatch[2].split(';');
523
  for (const param of params) {
524
- const [key, value] = param.trim().split('=');
525
- if (key && value) {
526
- codec.parameters[key.trim()] = value.trim();
 
 
 
527
  }
528
  }
529
  }
@@ -531,10 +546,15 @@ app.get("/publish", (req, res) => {
531
 
532
  codecs.push(codec);
533
  payloadTypes.add(payloadType);
 
534
  }
535
  }
536
  }
537
 
 
 
 
 
538
  // Extract SSRC
539
  const ssrcMatch = mediaSection.find(l => l.startsWith('a=ssrc:'));
540
  const ssrc = ssrcMatch ? parseInt(ssrcMatch.split(':')[1].split(' ')[0]) : Math.floor(Math.random() * 1000000000);
@@ -653,6 +673,9 @@ app.post("/api/produce", async (req, res) => {
653
  return res.status(404).json({ error: "Transport not found" });
654
  }
655
 
 
 
 
656
  const producer = await transport.produce({ kind, rtpParameters });
657
  producers.set(producer.id, producer);
658
 
 
31
  kind: "video",
32
  mimeType: "video/VP8",
33
  clockRate: 90000,
34
+ parameters: {},
 
 
 
 
 
 
 
 
 
35
  },
36
  ],
37
  });
 
187
 
188
  const { rtpParameters, id: consumerId } = await consumeResponse.json();
189
 
 
 
 
 
190
  console.log('βœ… Viewing started');
191
 
192
  } catch (error) {
 
380
  const videoTrack = stream.getVideoTracks()[0];
381
  if (videoTrack) {
382
  const videoParams = extractTrackParameters(pc.localDescription.sdp, 'video');
383
+
384
+ // Force VP8 codec if available
385
+ const vp8Codec = videoParams.codecs.find(c => c.mimeType === 'video/VP8');
386
+ if (vp8Codec) {
387
+ videoParams.codecs = [vp8Codec];
388
+ console.log('🎬 Using VP8 codec');
389
+ }
390
+
391
+ console.log('πŸ“€ Sending video params:', JSON.stringify(videoParams, null, 2));
392
+
393
  const videoResponse = await fetch('/api/produce', {
394
  method: 'POST',
395
  headers: { 'Content-Type': 'application/json' },
 
399
  rtpParameters: videoParams
400
  })
401
  });
402
+
403
+ if (!videoResponse.ok) {
404
+ const error = await videoResponse.json();
405
+ throw new Error('Video producer failed: ' + error.error);
406
+ }
407
+
408
  const { id } = await videoResponse.json();
409
  videoProducerId = id;
410
  console.log('βœ… Video producer created:', id);
 
414
  const audioTrack = stream.getAudioTracks()[0];
415
  if (audioTrack) {
416
  const audioParams = extractTrackParameters(pc.localDescription.sdp, 'audio');
417
+
418
+ console.log('πŸ“€ Sending audio params:', JSON.stringify(audioParams, null, 2));
419
+
420
  const audioResponse = await fetch('/api/produce', {
421
  method: 'POST',
422
  headers: { 'Content-Type': 'application/json' },
 
426
  rtpParameters: audioParams
427
  })
428
  });
429
+
430
+ if (!audioResponse.ok) {
431
+ const error = await audioResponse.json();
432
+ throw new Error('Audio producer failed: ' + error.error);
433
+ }
434
+
435
  const { id } = await audioResponse.json();
436
  audioProducerId = id;
437
  console.log('βœ… Audio producer created:', id);
 
513
  const channels = rtpmapMatch[4] ? parseInt(rtpmapMatch[4]) : undefined;
514
 
515
  // Only include codecs that match our kind
516
+ if ((kind === 'video' && (codecName === 'VP8' || codecName === 'VP9' || codecName === 'H264')) ||
517
  (kind === 'audio' && codecName.toLowerCase() === 'opus')) {
518
 
519
  const codec = {
 
533
  codec.parameters = {};
534
  const params = fmtpMatch[2].split(';');
535
  for (const param of params) {
536
+ const parts = param.trim().split('=');
537
+ if (parts.length === 2) {
538
+ const key = parts[0].trim();
539
+ const value = parts[1].trim();
540
+ // Convert numeric values
541
+ codec.parameters[key] = isNaN(value) ? value : parseInt(value);
542
  }
543
  }
544
  }
 
546
 
547
  codecs.push(codec);
548
  payloadTypes.add(payloadType);
549
+ break; // Use first matching codec
550
  }
551
  }
552
  }
553
 
554
+ if (codecs.length === 0) {
555
+ throw new Error(\`No compatible \${kind} codec found\`);
556
+ }
557
+
558
  // Extract SSRC
559
  const ssrcMatch = mediaSection.find(l => l.startsWith('a=ssrc:'));
560
  const ssrc = ssrcMatch ? parseInt(ssrcMatch.split(':')[1].split(' ')[0]) : Math.floor(Math.random() * 1000000000);
 
673
  return res.status(404).json({ error: "Transport not found" });
674
  }
675
 
676
+ // Log the incoming parameters for debugging
677
+ console.log(`πŸ“ Attempting to create ${kind} producer with parameters:`, JSON.stringify(rtpParameters, null, 2));
678
+
679
  const producer = await transport.produce({ kind, rtpParameters });
680
  producers.set(producer.id, producer);
681