Skip to content

Commit

Permalink
fix: check if opus created (#1166)
Browse files Browse the repository at this point in the history
* fix: check is opus created

* fix: careful concurrency fixes
  • Loading branch information
khodand committed May 14, 2022
1 parent c29e0d7 commit fa14e19
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions voice.go
Expand Up @@ -120,9 +120,9 @@ func (v *VoiceConnection) ChangeChannel(channelID string, mute, deaf bool) (err
v.log(LogInformational, "called")

data := voiceChannelJoinOp{4, voiceChannelJoinData{&v.GuildID, &channelID, mute, deaf}}
v.wsMutex.Lock()
v.session.wsMutex.Lock()
err = v.session.wsConn.WriteJSON(data)
v.wsMutex.Unlock()
v.session.wsMutex.Unlock()
if err != nil {
return
}
Expand Down Expand Up @@ -323,7 +323,9 @@ func (v *VoiceConnection) open() (err error) {
}
data := voiceHandshakeOp{0, voiceHandshakeData{v.GuildID, v.UserID, v.sessionID, v.token}}

v.wsMutex.Lock()
err = v.wsConn.WriteJSON(data)
v.wsMutex.Unlock()
if err != nil {
v.log(LogWarning, "error sending init packet, %s", err)
return
Expand Down Expand Up @@ -829,7 +831,12 @@ func (v *VoiceConnection) opusReceiver(udpConn *net.UDPConn, close <-chan struct
p.SSRC = binary.BigEndian.Uint32(recvbuf[8:12])
// decrypt opus data
copy(nonce[:], recvbuf[0:12])
p.Opus, _ = secretbox.Open(nil, recvbuf[12:rlen], &nonce, &v.op4.SecretKey)

if opus, ok := secretbox.Open(nil, recvbuf[12:rlen], &nonce, &v.op4.SecretKey); ok {
p.Opus = opus
} else {
return
}

// extension bit set, and not a RTCP packet
if ((recvbuf[0] & 0x10) == 0x10) && ((recvbuf[1] & 0x80) == 0) {
Expand Down Expand Up @@ -870,7 +877,11 @@ func (v *VoiceConnection) reconnect() {
v.reconnecting = true
v.Unlock()

defer func() { v.reconnecting = false }()
defer func() {
v.Lock()
v.reconnecting = false
v.Unlock()
}()

// Close any currently open connections
v.Close()
Expand Down

0 comments on commit fa14e19

Please sign in to comment.