fix: check if opus created (#1166)

* fix: check is opus created

* fix: careful concurrency fixes
This commit is contained in:
Andrei Khodko 2022-05-14 22:52:33 +03:00 committed by GitHub
parent c29e0d740f
commit fa14e19ad5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -120,9 +120,9 @@ func (v *VoiceConnection) ChangeChannel(channelID string, mute, deaf bool) (err
v.log(LogInformational, "called") v.log(LogInformational, "called")
data := voiceChannelJoinOp{4, voiceChannelJoinData{&v.GuildID, &channelID, mute, deaf}} data := voiceChannelJoinOp{4, voiceChannelJoinData{&v.GuildID, &channelID, mute, deaf}}
v.wsMutex.Lock() v.session.wsMutex.Lock()
err = v.session.wsConn.WriteJSON(data) err = v.session.wsConn.WriteJSON(data)
v.wsMutex.Unlock() v.session.wsMutex.Unlock()
if err != nil { if err != nil {
return return
} }
@ -323,7 +323,9 @@ func (v *VoiceConnection) open() (err error) {
} }
data := voiceHandshakeOp{0, voiceHandshakeData{v.GuildID, v.UserID, v.sessionID, v.token}} data := voiceHandshakeOp{0, voiceHandshakeData{v.GuildID, v.UserID, v.sessionID, v.token}}
v.wsMutex.Lock()
err = v.wsConn.WriteJSON(data) err = v.wsConn.WriteJSON(data)
v.wsMutex.Unlock()
if err != nil { if err != nil {
v.log(LogWarning, "error sending init packet, %s", err) v.log(LogWarning, "error sending init packet, %s", err)
return return
@ -829,7 +831,12 @@ func (v *VoiceConnection) opusReceiver(udpConn *net.UDPConn, close <-chan struct
p.SSRC = binary.BigEndian.Uint32(recvbuf[8:12]) p.SSRC = binary.BigEndian.Uint32(recvbuf[8:12])
// decrypt opus data // decrypt opus data
copy(nonce[:], recvbuf[0:12]) 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 // extension bit set, and not a RTCP packet
if ((recvbuf[0] & 0x10) == 0x10) && ((recvbuf[1] & 0x80) == 0) { if ((recvbuf[0] & 0x10) == 0x10) && ((recvbuf[1] & 0x80) == 0) {
@ -870,7 +877,11 @@ func (v *VoiceConnection) reconnect() {
v.reconnecting = true v.reconnecting = true
v.Unlock() v.Unlock()
defer func() { v.reconnecting = false }() defer func() {
v.Lock()
v.reconnecting = false
v.Unlock()
}()
// Close any currently open connections // Close any currently open connections
v.Close() v.Close()