11'use strict' ;
22
33const EventEmitter = require ( 'events' ) ;
4+ const Speaking = require ( '../../../util/Speaking' ) ;
45const secretbox = require ( '../util/Secretbox' ) ;
6+ const { SILENCE_FRAME } = require ( '../util/Silence' ) ;
57
68// The delay between packets when a user is considered to have stopped speaking
79// https://github.com/discordjs/discord.js/issues/3524#issuecomment-540373200
@@ -84,12 +86,30 @@ class PacketHandler extends EventEmitter {
8486 const userStat = this . connection . ssrcMap . get ( ssrc ) ;
8587 if ( ! userStat ) return ;
8688
89+ let opusPacket ;
90+ const streamInfo = this . streams . get ( userStat . userID ) ;
91+ // If the user is in video, we need to check if the packet is just silence
92+ if ( userStat . hasVideo ) {
93+ opusPacket = this . parseBuffer ( buffer ) ;
94+ if ( opusPacket instanceof Error ) {
95+ // Only emit an error if we were actively receiving packets from this user
96+ if ( streamInfo ) {
97+ this . emit ( 'error' , opusPacket ) ;
98+ return ;
99+ }
100+ }
101+ if ( SILENCE_FRAME . equals ( opusPacket ) ) {
102+ // If this is a silence frame, pretend we never received it
103+ return ;
104+ }
105+ }
106+
87107 let speakingTimeout = this . speakingTimeouts . get ( ssrc ) ;
88108 if ( typeof speakingTimeout === 'undefined' ) {
89109 // Ensure at least the speaking bit is set.
90110 // As the object is by reference, it's only needed once per client re-connect.
91111 if ( userStat . speaking === 0 ) {
92- userStat . speaking = 1 ;
112+ userStat . speaking = Speaking . FLAGS . SPEAKING ;
93113 }
94114 this . connection . onSpeaking ( { user_id : userStat . userID , ssrc : ssrc , speaking : userStat . speaking } ) ;
95115 speakingTimeout = this . receiver . connection . client . setTimeout ( ( ) => {
@@ -106,15 +126,17 @@ class PacketHandler extends EventEmitter {
106126 speakingTimeout . refresh ( ) ;
107127 }
108128
109- let stream = this . streams . get ( userStat . userID ) ;
110- if ( ! stream ) return ;
111- stream = stream . stream ;
112- const opusPacket = this . parseBuffer ( buffer ) ;
113- if ( opusPacket instanceof Error ) {
114- this . emit ( 'error' , opusPacket ) ;
115- return ;
129+ if ( streamInfo ) {
130+ const { stream } = streamInfo ;
131+ if ( ! opusPacket ) {
132+ opusPacket = this . parseBuffer ( buffer ) ;
133+ if ( opusPacket instanceof Error ) {
134+ this . emit ( 'error' , opusPacket ) ;
135+ return ;
136+ }
137+ }
138+ stream . push ( opusPacket ) ;
116139 }
117- stream . push ( opusPacket ) ;
118140 }
119141}
120142
0 commit comments