Update voice code to vws V4

- Update internals to be compatible with v4
- Adds multiple encryption mode support.  Previously only `xsalsa20_poly1305` was supported.  Now `xsalsa20_poly1305_suffix` is also supported.
  Note: There is no (nice) way to manually select a mode.  The user needn't worry about this however.
- Fixed speaking state bug.  When you disconnected from a voice channel while a bot was playing, upon reconnect you would be unable to hear the bot.  This was caused by bots not sending their speaking state while transmitting.  Bots will now set their speaking state properly when transmitting.  
  Note: This does not account for sending actual silence, the speaking indicator will still be active.
This commit is contained in:
Imayhaveborkedit
2019-01-16 01:35:31 -05:00
committed by Rapptz
parent 883ae8fe80
commit 9c5259afd7
4 changed files with 86 additions and 24 deletions

View File

@ -102,6 +102,7 @@ class VoiceClient:
self._connected = threading.Event()
self._handshake_complete = asyncio.Event(loop=self.loop)
self.mode = None
self._connections = 0
self.sequence = 0
self.timestamp = 0
@ -110,6 +111,10 @@ class VoiceClient:
self.encoder = opus.Encoder()
warn_nacl = not has_nacl
supported_modes = (
'xsalsa20_poly1305_suffix',
'xsalsa20_poly1305',
)
@property
def guild(self):
@ -288,22 +293,30 @@ class VoiceClient:
def _get_voice_packet(self, data):
header = bytearray(12)
nonce = bytearray(24)
box = nacl.secret.SecretBox(bytes(self.secret_key))
# Formulate header
# Formulate rtp header
header[0] = 0x80
header[1] = 0x78
struct.pack_into('>H', header, 2, self.sequence)
struct.pack_into('>I', header, 4, self.timestamp)
struct.pack_into('>I', header, 8, self.ssrc)
# Copy header to nonce's first 12 bytes
encrypt_packet = getattr(self, '_encrypt_' + self.mode)
return encrypt_packet(header, data)
def _encrypt_xsalsa20_poly1305(self, header, data):
box = nacl.secret.SecretBox(bytes(self.secret_key))
nonce = bytearray(24)
nonce[:12] = header
# Encrypt and return the data
return header + box.encrypt(bytes(data), bytes(nonce)).ciphertext
def _encrypt_xsalsa20_poly1305_suffix(self, header, data):
box = nacl.secret.SecretBox(bytes(self.secret_key))
nonce = nacl.utils.random(nacl.secret.SecretBox.NONCE_SIZE)
return header + box.encrypt(bytes(data), nonce).ciphertext + nonce
def play(self, source, *, after=None):
"""Plays an :class:`AudioSource`.