@@ -378,35 +378,24 @@ export class Socket extends EventEmitter {
378
378
onPacket ( packet : Buffer , from : UDPFrom ) : void {
379
379
if ( ! this . receiveEnabled ) { return ; }
380
380
if ( from . address !== this . remote . ip || from . port !== this . remote . port ) {
381
- this . emit ( SocketEvents . WARN , new MediaPacketError (
382
- 'Received a packet from an unknown IP/Port' ,
383
- from ,
384
- packet ,
385
- ) ) ;
381
+ const error = new MediaPacketError ( 'Received a packet from an unknown IP/Port' , from , packet ) ;
382
+ this . emit ( SocketEvents . WARN , error ) ;
386
383
return ;
387
384
}
388
385
if ( ! this . key ) {
389
- this . emit ( SocketEvents . WARN , new MediaPacketError (
390
- 'Received a packet before the Session Description' ,
391
- from ,
392
- packet ,
393
- ) ) ;
386
+ const error = new MediaPacketError ( 'Received a packet before the Session Description' , from , packet ) ;
387
+ this . emit ( SocketEvents . WARN , error ) ;
394
388
return ;
395
389
}
396
390
if ( packet . length <= 12 ) {
397
- this . emit ( SocketEvents . WARN , new MediaPacketError (
398
- 'Received an rtp packet that\'s way too small to be valid' ,
399
- from ,
400
- packet ,
401
- ) ) ;
391
+ const error = new MediaPacketError ( 'Received an rtp packet that\'s way too small to be valid' , from , packet ) ;
392
+ this . emit ( SocketEvents . WARN , error ) ;
402
393
return ;
403
394
}
404
395
if ( ! isValidRTPHeader ( packet ) ) {
405
- this . emit ( SocketEvents . WARN , new MediaPacketError (
406
- 'Invalid RTP Packet' ,
407
- from ,
408
- packet ,
409
- ) ) ;
396
+ const error = new MediaPacketError ( 'Invalid RTP Packet' , from , packet ) ;
397
+ this . emit ( SocketEvents . WARN , error ) ;
398
+ return ;
410
399
}
411
400
412
401
const packetType = packet . readUIntBE ( 1 , 1 ) ;
@@ -427,12 +416,8 @@ export class Socket extends EventEmitter {
427
416
}
428
417
*/
429
418
if ( ! RTP_PAYLOAD_TYPES . includes ( payloadType ) ) {
430
- this . emit ( SocketEvents . WARN , new MediaRTPError (
431
- 'Unknown RTP Packet Payload Type' ,
432
- from ,
433
- packet ,
434
- rtp ,
435
- ) ) ;
419
+ const error = new MediaRTPError ( 'Unknown RTP Packet Payload Type' , from , packet , rtp ) ;
420
+ this . emit ( SocketEvents . WARN , error ) ;
436
421
return ;
437
422
}
438
423
@@ -458,22 +443,13 @@ export class Socket extends EventEmitter {
458
443
}
459
444
460
445
if ( format === MediaCodecTypes . VIDEO && ! this . videoEnabled ) {
461
- this . emit ( SocketEvents . LOG , new MediaRTPError (
462
- 'Dropping video packet due to video not being enabled' ,
463
- from ,
464
- packet ,
465
- rtp ,
466
- ) ) ;
446
+ const error = new MediaRTPError ( 'Dropping video packet due to video not being enabled' , from , packet , rtp ) ;
447
+ this . emit ( SocketEvents . LOG , error ) ;
467
448
return ;
468
449
}
469
450
470
451
rtp . nonce = Buffer . alloc ( 24 ) ;
471
452
switch ( this . mode ) {
472
- case MediaEncryptionModes . PLAIN : {
473
- // I assume theres no nonce?
474
- // only included cuz the docs have it in the examples lol
475
- rtp . payload = packet . slice ( 12 ) ;
476
- } ; break ;
477
453
case MediaEncryptionModes . XSALSA20_POLY1305_LITE : {
478
454
// last 4 bytes
479
455
packet . copy ( rtp . nonce , 0 , packet . length - 4 ) ;
@@ -491,33 +467,20 @@ export class Socket extends EventEmitter {
491
467
rtp . payload = packet . slice ( 12 ) ;
492
468
} ; break ;
493
469
default : {
494
- this . emit ( SocketEvents . WARN , new MediaRTPError (
495
- `${ this . mode } is not supported for decoding.` ,
496
- from ,
497
- packet ,
498
- rtp ,
499
- ) ) ;
470
+ const error = new MediaRTPError ( `${ this . mode } is not supported for decoding.` , from , packet , rtp ) ;
471
+ this . emit ( SocketEvents . WARN , error ) ;
500
472
return ;
501
473
} ;
502
474
}
503
475
504
- let data : Buffer | null = null ;
505
- if ( this . mode === MediaEncryptionModes . PLAIN ) {
506
- data = rtp . payload ;
507
- } else {
508
- data = RTPCrypto . decrypt (
509
- < Uint8Array > this . key ,
510
- < Buffer > rtp . payload ,
511
- < Buffer > rtp . nonce ,
512
- ) ;
513
- }
514
- if ( data === null ) {
515
- this . emit ( SocketEvents . WARN , new MediaRTPError (
516
- 'Packet failed to decrypt' ,
517
- from ,
518
- packet ,
519
- rtp ,
520
- ) ) ;
476
+ let data : Buffer | null = RTPCrypto . decrypt (
477
+ < Uint8Array > this . key ,
478
+ < Buffer > rtp . payload ,
479
+ < Buffer > rtp . nonce ,
480
+ ) ;
481
+ if ( ! data ) {
482
+ const error = new MediaRTPError ( 'Packet failed to decrypt' , from , packet , rtp ) ;
483
+ this . emit ( SocketEvents . WARN , error ) ;
521
484
return ;
522
485
}
523
486
@@ -567,12 +530,8 @@ export class Socket extends EventEmitter {
567
530
// using two bytes, 0x10 and 0x00 instead
568
531
// if appbits is all 0s, ignore, so rn ignore this packet
569
532
570
- this . emit ( SocketEvents . LOG , new MediaRTPError (
571
- 'Received Two Byte header with appbits being 0, ignoring' ,
572
- from ,
573
- packet ,
574
- rtp ,
575
- ) ) ;
533
+ const error = new MediaRTPError ( 'Received Two Byte header with appbits being 0, ignoring' , from , packet , rtp ) ;
534
+ this . emit ( SocketEvents . LOG , error ) ;
576
535
return ;
577
536
/*
578
537
// handle the two byte
@@ -749,9 +708,6 @@ export class Socket extends EventEmitter {
749
708
750
709
let nonce : Buffer ;
751
710
switch ( this . mode ) {
752
- case MediaEncryptionModes . PLAIN : {
753
- nonce = Buffer . alloc ( 0 ) ;
754
- } ; break ;
755
711
case MediaEncryptionModes . XSALSA20_POLY1305_LITE : {
756
712
if ( ! useCache && options . nonce === undefined ) {
757
713
throw new Error ( `You must use cache if you do not send in an incrementing nonce with the Encryption mode being ${ this . mode } ` ) ;
0 commit comments