Browse Source

Revert all voice receive changes

This won't be making its way into 2.0 and needs a ton of work. Now lint passes!
pull/10109/head
dolfies 3 years ago
parent
commit
cfe58eeb1f
  1. 4
      discord/abc.py
  2. 2
      discord/ext/commands/converter.py
  3. 61
      discord/gateway.py
  4. 2
      discord/guild.py
  5. 12
      discord/player.py
  6. 51
      discord/recorder.py
  7. 416
      discord/voice_client.py

4
discord/abc.py

@ -1876,10 +1876,10 @@ class Connectable(Protocol):
raise ClientException('Already connected to a voice channel') raise ClientException('Already connected to a voice channel')
if cls is MISSING: if cls is MISSING:
cls = VoiceClient cls = VoiceClient # type: ignore
# The type checker doesn't understand that VoiceClient *is* T here. # The type checker doesn't understand that VoiceClient *is* T here.
voice: T = cls(state.client, channel) # type: ignore voice: T = cls(state.client, channel)
if not isinstance(voice, VoiceProtocol): if not isinstance(voice, VoiceProtocol):
raise TypeError('Type must meet VoiceProtocol abstract base class') raise TypeError('Type must meet VoiceProtocol abstract base class')

2
discord/ext/commands/converter.py

@ -369,7 +369,7 @@ class PartialMessageConverter(Converter[discord.PartialMessage]):
return None return None
return guild._resolve_channel(channel_id) return guild._resolve_channel(channel_id)
return ctx.bot.get_channel(channel_id) return ctx.bot.get_channel(channel_id) # type: ignore
async def convert(self, ctx: Context[BotT], argument: str) -> discord.PartialMessage: async def convert(self, ctx: Context[BotT], argument: str) -> discord.PartialMessage:
guild_id, message_id, channel_id = self._get_id_matches(ctx, argument) guild_id, message_id, channel_id = self._get_id_matches(ctx, argument)

61
discord/gateway.py

@ -40,7 +40,6 @@ from . import utils
from .activity import BaseActivity from .activity import BaseActivity
from .enums import SpeakingState from .enums import SpeakingState
from .errors import ConnectionClosed from .errors import ConnectionClosed
from .recorder import SSRC
_log = logging.getLogger(__name__) _log = logging.getLogger(__name__)
@ -835,10 +834,10 @@ class DiscordVoiceWebSocket:
Receive only. Tells the websocket that the initial connection has completed. Receive only. Tells the websocket that the initial connection has completed.
HEARTBEAT HEARTBEAT
Send only. Keeps your websocket connection alive. Send only. Keeps your websocket connection alive.
SELECT_PROTOCOL_ACK SESSION_DESCRIPTION
Receive only. Gives you the secret key required for voice. Receive only. Gives you the secret key required for voice.
SPEAKING SPEAKING
Send and receive. Notifies the client if anyone begins speaking. Send only. Notifies the client if you are currently speaking.
HEARTBEAT_ACK HEARTBEAT_ACK
Receive only. Tells you your heartbeat has been acknowledged. Receive only. Tells you your heartbeat has been acknowledged.
RESUME RESUME
@ -847,8 +846,10 @@ class DiscordVoiceWebSocket:
Receive only. Tells you that your websocket connection was acknowledged. Receive only. Tells you that your websocket connection was acknowledged.
RESUMED RESUMED
Sent only. Tells you that your RESUME request has succeeded. Sent only. Tells you that your RESUME request has succeeded.
CLIENT_CONNECT
Indicates a user has connected to voice.
CLIENT_DISCONNECT CLIENT_DISCONNECT
Receive only. Indicates a user has disconnected from voice. Receive only. Indicates a user has disconnected from voice.
""" """
if TYPE_CHECKING: if TYPE_CHECKING:
@ -858,21 +859,18 @@ class DiscordVoiceWebSocket:
_max_heartbeat_timeout: float _max_heartbeat_timeout: float
# fmt: off # fmt: off
IDENTIFY = 0 IDENTIFY = 0
SELECT_PROTOCOL = 1 SELECT_PROTOCOL = 1
READY = 2 READY = 2
HEARTBEAT = 3 HEARTBEAT = 3
SELECT_PROTOCOL_ACK = 4 SESSION_DESCRIPTION = 4
SPEAKING = 5 SPEAKING = 5
HEARTBEAT_ACK = 6 HEARTBEAT_ACK = 6
RESUME = 7 RESUME = 7
HELLO = 8 HELLO = 8
RESUMED = 9 RESUMED = 9
VIDEO = 12 CLIENT_CONNECT = 12
CLIENT_DISCONNECT = 13 CLIENT_DISCONNECT = 13
SESSION_UPDATE = 14
MEDIA_SINK_WANTS = 15
VOICE_BACKEND_VERSION = 16
# fmt: on # fmt: on
def __init__( def __init__(
@ -931,7 +929,7 @@ class DiscordVoiceWebSocket:
"""Creates a voice websocket for the :class:`VoiceClient`.""" """Creates a voice websocket for the :class:`VoiceClient`."""
gateway = 'wss://' + client.endpoint + '/?v=4' gateway = 'wss://' + client.endpoint + '/?v=4'
http = client._state.http http = client._state.http
socket = await http.ws_connect(gateway, compress=15, host=client.endpoint) socket = await http.ws_connect(gateway, compress=15)
ws = cls(socket, loop=client.loop, hook=hook) ws = cls(socket, loop=client.loop, hook=hook)
ws.gateway = gateway ws.gateway = gateway
ws._connection = client ws._connection = client
@ -962,7 +960,7 @@ class DiscordVoiceWebSocket:
async def client_connect(self) -> None: async def client_connect(self) -> None:
payload = { payload = {
'op': self.VIDEO, 'op': self.CLIENT_CONNECT,
'd': { 'd': {
'audio_ssrc': self._connection.ssrc, 'audio_ssrc': self._connection.ssrc,
}, },
@ -993,26 +991,13 @@ class DiscordVoiceWebSocket:
self._keep_alive.ack() self._keep_alive.ack()
elif op == self.RESUMED: elif op == self.RESUMED:
_log.info('Voice RESUME succeeded.') _log.info('Voice RESUME succeeded.')
self.secret_key = self._connection.secret_key elif op == self.SESSION_DESCRIPTION:
elif op == self.SELECT_PROTOCOL_ACK:
self._connection.mode = data['mode'] self._connection.mode = data['mode']
await self.load_secret_key(data) await self.load_secret_key(data)
elif op == self.HELLO: elif op == self.HELLO:
interval = data['heartbeat_interval'] / 1000.0 interval = data['heartbeat_interval'] / 1000.0
self._keep_alive = VoiceKeepAliveHandler(ws=self, interval=min(interval, 5.0)) self._keep_alive = VoiceKeepAliveHandler(ws=self, interval=min(interval, 5.0))
self._keep_alive.start() self._keep_alive.start()
elif op == self.SPEAKING:
state = self._connection
user_id = int(data['user_id'])
speaking = data['speaking']
ssrc = state._flip_ssrc(user_id)
if ssrc is None:
state._set_ssrc(user_id, SSRC(data['ssrc'], speaking))
else:
ssrc.speaking = speaking
# item = state.guild or state._state
# item._update_speaking_status(user_id, speaking)
await self._hook(self, msg) await self._hook(self, msg)
@ -1030,15 +1015,15 @@ class DiscordVoiceWebSocket:
recv = await self.loop.sock_recv(state.socket, 70) recv = await self.loop.sock_recv(state.socket, 70)
_log.debug('Received packet in initial_connection: %s.', recv) _log.debug('Received packet in initial_connection: %s.', recv)
# The IP is ascii starting at the 4th byte and ending at the first null # the ip is ascii starting at the 4th byte and ending at the first null
ip_start = 4 ip_start = 4
ip_end = recv.index(0, ip_start) ip_end = recv.index(0, ip_start)
state.ip = recv[ip_start:ip_end].decode('ascii') state.ip = recv[ip_start:ip_end].decode('ascii')
state.port = struct.unpack_from('>H', recv, len(recv) - 2)[0] state.port = struct.unpack_from('>H', recv, len(recv) - 2)[0]
_log.debug('detected ip: %s port: %s', state.ip, state.port) _log.debug('Detected ip: %s, port: %s.', state.ip, state.port)
# There *should* always be at least one supported mode (xsalsa20_poly1305) # there *should* always be at least one supported mode (xsalsa20_poly1305)
modes = [mode for mode in data['modes'] if mode in self._connection.supported_modes] modes = [mode for mode in data['modes'] if mode in self._connection.supported_modes]
_log.debug('Received supported encryption modes: %s.', ", ".join(modes)) _log.debug('Received supported encryption modes: %s.', ", ".join(modes))

2
discord/guild.py

@ -342,7 +342,7 @@ class Guild(Hashable):
def __init__(self, *, data: Union[GuildPayload, GuildPreviewPayload], state: ConnectionState) -> None: def __init__(self, *, data: Union[GuildPayload, GuildPreviewPayload], state: ConnectionState) -> None:
self._chunked = False self._chunked = False
self._cs_joined = None self._cs_joined: Optional[bool] = None
self._roles: Dict[int, Role] = {} self._roles: Dict[int, Role] = {}
self._channels: Dict[int, GuildChannel] = {} self._channels: Dict[int, GuildChannel] = {}
self._members: Dict[int, Member] = {} self._members: Dict[int, Member] = {}

12
discord/player.py

@ -47,7 +47,7 @@ from .utils import MISSING
if TYPE_CHECKING: if TYPE_CHECKING:
from typing_extensions import Self from typing_extensions import Self
from .voice_client import Player from .voice_client import VoiceClient
AT = TypeVar('AT', bound='AudioSource') AT = TypeVar('AT', bound='AudioSource')
@ -637,21 +637,21 @@ class AudioPlayer(threading.Thread):
def __init__( def __init__(
self, self,
source: AudioSource, source: AudioSource,
client: Player, client: VoiceClient,
*, *,
after: Optional[Callable[[Optional[Exception]], Any]] = None, after: Optional[Callable[[Optional[Exception]], Any]] = None,
) -> None: ) -> None:
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.daemon: bool = True self.daemon: bool = True
self.source: AudioSource = source self.source: AudioSource = source
self.client: Player = client self.client: VoiceClient = client
self.after: Optional[Callable[[Optional[Exception]], Any]] = after self.after: Optional[Callable[[Optional[Exception]], Any]] = after
self._end: threading.Event = threading.Event() self._end: threading.Event = threading.Event()
self._resumed: threading.Event = threading.Event() self._resumed: threading.Event = threading.Event()
self._resumed.set() # We are not paused self._resumed.set() # we are not paused
self._current_error: Optional[Exception] = None self._current_error: Optional[Exception] = None
self._connected: threading.Event = client.client._connected self._connected: threading.Event = client._connected
self._lock: threading.Lock = threading.Lock() self._lock: threading.Lock = threading.Lock()
if after is not None and not callable(after): if after is not None and not callable(after):
@ -662,7 +662,7 @@ class AudioPlayer(threading.Thread):
self._start = time.perf_counter() self._start = time.perf_counter()
# getattr lookup speed ups # getattr lookup speed ups
play_audio = self.client.send play_audio = self.client.send_audio_packet
self._speak(SpeakingState.voice) self._speak(SpeakingState.voice)
while not self._end.is_set(): while not self._end.is_set():

51
discord/recorder.py

@ -1,51 +0,0 @@
"""
The MIT License (MIT)
Copyright (c) 2015-present Who do I put here???
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from __future__ import annotations
import struct
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from .voice_client import VoiceClient
unpacker = struct.Struct('>xxHII')
class SSRC:
def __init__(self, ssrc: int, speaking: bool) -> None:
self._ssrc = ssrc
self.speaking = speaking
def __repr__(self) -> str:
return str(self._ssrc)
class VoicePacket: # IN-PROGRESS
def __init__(self, client: VoiceClient, data: bytes):
self.client = client
_data = bytearray(data)
self.data: bytearray = data[12:]
self.header: bytearray = data[:12]

416
discord/voice_client.py

@ -40,12 +40,11 @@ Some documentation to refer to:
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
from dataclasses import dataclass
import socket import socket
import logging import logging
import struct import struct
import threading import threading
from typing import Any, Callable, Dict, List, Optional, TYPE_CHECKING, Tuple, Union from typing import Any, Callable, List, Optional, TYPE_CHECKING, Tuple, Union
from . import opus, utils from . import opus, utils
from .backoff import ExponentialBackoff from .backoff import ExponentialBackoff
@ -60,7 +59,7 @@ if TYPE_CHECKING:
from .state import ConnectionState from .state import ConnectionState
from .user import ClientUser from .user import ClientUser
from .opus import Encoder from .opus import Encoder
from .channel import StageChannel, VoiceChannel from .channel import StageChannel, VoiceChannel, DMChannel, GroupChannel
from . import abc from . import abc
from .types.voice import ( from .types.voice import (
@ -69,6 +68,8 @@ if TYPE_CHECKING:
SupportedModes, SupportedModes,
) )
VocalChannel = Union[VoiceChannel, StageChannel, DMChannel, GroupChannel]
has_nacl: bool has_nacl: bool
@ -85,6 +86,7 @@ __all__ = (
'VoiceClient', 'VoiceClient',
) )
_log = logging.getLogger(__name__) _log = logging.getLogger(__name__)
@ -105,13 +107,13 @@ class VoiceProtocol:
------------ ------------
client: :class:`Client` client: :class:`Client`
The client (or its subclasses) that started the connection request. The client (or its subclasses) that started the connection request.
channel: :class:`abc.Connectable` channel: Union[:class:`VoiceChannel`, :class:`StageChannel`, :class:`DMChannel`, :class:`GroupChannel`]
The voice channel that is being connected to. The voice channel that is being connected to.
""" """
def __init__(self, client: Client, channel: abc.Connectable) -> None: def __init__(self, client: Client, channel: VocalChannel) -> None:
self.client: Client = client self.client: Client = client
self.channel: abc.Connectable = channel self.channel: VocalChannel = channel
async def on_voice_state_update(self, data: GuildVoiceStatePayload) -> None: async def on_voice_state_update(self, data: GuildVoiceStatePayload) -> None:
"""|coro| """|coro|
@ -198,159 +200,6 @@ class VoiceProtocol:
self.client._connection._remove_voice_client(key_id) self.client._connection._remove_voice_client(key_id)
class Player:
def __init__(self, client: VoiceClient) -> None:
self.client = client
self.loop: asyncio.AbstractEventLoop = client.loop
self.encoder: Encoder = MISSING
self._player: AudioPlayer = MISSING
def send(self, data: bytes, encode: bool = True) -> None:
"""Sends an audio packet composed of the data.
You must be connected to play audio.
Parameters
----------
data: :class:`bytes`
The :term:`py:bytes-like object` denoting PCM or Opus voice data.
encode: :class:`bool`
Indicates if ``data`` should be encoded into Opus.
Raises
-------
ClientException
You are not connected.
opus.OpusError
Encoding the data failed.
"""
if encode:
data = self.encoder.encode(data, self.encoder.SAMPLES_PER_FRAME)
self.client.send_audio_packet(data)
@property
def ws(self) -> DiscordVoiceWebSocket:
return self.client.ws
@property
def source(self) -> Optional[AudioSource]:
"""Optional[:class:`AudioSource`]: The audio source being played, if playing.
This property can also be used to change the audio source currently being played.
"""
return self._player.source if self._player else None
@source.setter
def source(self, value) -> None:
if not isinstance(value, AudioSource):
raise TypeError('Expected AudioSource not {0.__class__.__name__}'.format(value))
if self._player is None:
raise ValueError('Not playing anything')
self._player._set_source(value)
def is_playing(self) -> bool:
"""Indicates if we're currently playing audio."""
return self._player and self._player.is_playing()
def is_paused(self) -> bool:
"""Indicates if we're playing audio, but if we're paused."""
return self._player and self._player.is_paused()
def play(self, source: AudioSource, *, after: Callable[[Optional[Exception]], Any] = None) -> None:
"""Plays an :class:`AudioSource`.
The finalizer, ``after`` is called after the source has been exhausted
or an error occurred.
If an error happens while the audio player is running, the exception is
caught and the audio player is then stopped. If no after callback is
passed, any caught exception will be displayed as if it were raised.
Parameters
-----------
source: :class:`AudioSource`
The audio source we're reading from.
after: Callable[[:class:`Exception`], Any]
The finalizer that is called after the stream is exhausted.
This function must have a single parameter, ``error``, that
denotes an optional exception that was raised during playing.
Raises
-------
ClientException
Already playing audio or not connected.
TypeError
Source is not a :class:`AudioSource` or after is not a callable.
OpusNotLoaded
Source is not opus encoded and opus is not loaded.
"""
if not self.client.is_connected():
raise ClientException('Not connected to voice')
if self.is_playing():
raise ClientException('Already playing audio')
if not isinstance(source, AudioSource):
raise TypeError(f'source must be an AudioSource not {source.__class__.__name__}')
if not self.encoder and not source.is_opus():
self.encoder = opus.Encoder()
self._player = AudioPlayer(source, self, after=after)
self._player.start()
def pause(self) -> None:
"""Pauses the audio playing."""
if self._player:
self._player.pause()
def resume(self) -> None:
"""Resumes the audio playing."""
if self._player:
self._player.resume()
def stop(self) -> None:
"""Stops playing audio."""
if self._player:
self._player.stop()
self._player = MISSING
class Listener:
def __init__(self, client: VoiceClient) -> None:
self.client = client
self.loop: asyncio.AbstractEventLoop = client.loop
self.decoder = None
self._listener = None
@property
def ws(self) -> DiscordVoiceWebSocket:
return self.client.ws
def is_listening(self) -> bool:
"""Indicates if we're currently listening."""
return self._listener is not None and self._listener.is_listening()
def is_paused(self) -> bool:
"""Indicates if we're listening, but we're paused."""
return self._listener is not None and self._listener.is_paused()
def listen(self, sink, *, callback=None) -> None:
if not self.client.is_connected():
raise ClientException('Not connected to voice')
if self.is_listening():
raise ClientException('Already listening')
if not isinstance(sink, AudioSink):
raise TypeError(f'sink must an AudioSink not {sink.__class__.__name__}')
class VoiceClient(VoiceProtocol): class VoiceClient(VoiceProtocol):
"""Represents a Discord voice connection. """Represents a Discord voice connection.
@ -372,18 +221,19 @@ class VoiceClient(VoiceProtocol):
The voice connection token. The voice connection token.
endpoint: :class:`str` endpoint: :class:`str`
The endpoint we are connecting to. The endpoint we are connecting to.
channel: :class:`abc.Connectable` channel: Union[:class:`VoiceChannel`, :class:`StageChannel`, :class:`DMChannel`, :class:`GroupChannel`]
The voice channel connected to. The voice channel connected to.
""" """
channel: abc.Connectable channel: VocalChannel
endpoint_ip: str endpoint_ip: str
voice_port: int voice_port: int
ip: str ip: str
port: int port: int
secret_key: Optional[str] secret_key: List[int]
ssrc: int
def __init__(self, client: Client, channel: abc.Connectable): def __init__(self, client: Client, channel: VocalChannel) -> None:
if not has_nacl: if not has_nacl:
raise RuntimeError("PyNaCl library needed in order to use voice") raise RuntimeError("PyNaCl library needed in order to use voice")
@ -394,7 +244,7 @@ class VoiceClient(VoiceProtocol):
self.socket = MISSING self.socket = MISSING
self.loop: asyncio.AbstractEventLoop = state.loop self.loop: asyncio.AbstractEventLoop = state.loop
self._state: ConnectionState = state self._state: ConnectionState = state
# This will be used in the threads # this will be used in the AudioPlayer thread
self._connected: threading.Event = threading.Event() self._connected: threading.Event = threading.Event()
self._handshaking: bool = False self._handshaking: bool = False
@ -406,14 +256,12 @@ class VoiceClient(VoiceProtocol):
self._connections: int = 0 self._connections: int = 0
self.sequence: int = 0 self.sequence: int = 0
self.timestamp: int = 0 self.timestamp: int = 0
self.player = Player(self)
self.listener = Listener(self)
self.timeout: float = 0 self.timeout: float = 0
self._runner: asyncio.Task = MISSING self._runner: asyncio.Task = MISSING
self._player: Optional[AudioPlayer] = None
self.encoder: Encoder = MISSING
self._lite_nonce: int = 0 self._lite_nonce: int = 0
self.ws: DiscordVoiceWebSocket = MISSING self.ws: DiscordVoiceWebSocket = MISSING
self.idrcs: Dict[int, int] = {}
self.ssids: Dict[int, int] = {}
warn_nacl: bool = not has_nacl warn_nacl: bool = not has_nacl
supported_modes: Tuple[SupportedModes, ...] = ( supported_modes: Tuple[SupportedModes, ...] = (
@ -422,16 +270,6 @@ class VoiceClient(VoiceProtocol):
'xsalsa20_poly1305', 'xsalsa20_poly1305',
) )
@property
def ssrc(self) -> int:
""":class:`str`: Our ssrc."""
return self.idrcs.get(self.user.id) # type: ignore
@ssrc.setter
def ssrc(self, value):
self.idrcs[self.user.id] = value
self.ssids[value] = self.user.id
@property @property
def guild(self) -> Optional[Guild]: def guild(self) -> Optional[Guild]:
"""Optional[:class:`Guild`]: The guild we're connected to, if applicable.""" """Optional[:class:`Guild`]: The guild we're connected to, if applicable."""
@ -442,8 +280,6 @@ class VoiceClient(VoiceProtocol):
""":class:`ClientUser`: The user connected to voice (i.e. ourselves).""" """:class:`ClientUser`: The user connected to voice (i.e. ourselves)."""
return self._state.user # type: ignore return self._state.user # type: ignore
# Connection related
def checked_add(self, attr: str, value: int, limit: int) -> None: def checked_add(self, attr: str, value: int, limit: int) -> None:
val = getattr(self, attr) val = getattr(self, attr)
if val + value > limit: if val + value > limit:
@ -451,6 +287,8 @@ class VoiceClient(VoiceProtocol):
else: else:
setattr(self, attr, val + value) setattr(self, attr, val + value)
# connection related
async def on_voice_state_update(self, data: GuildVoiceStatePayload) -> None: async def on_voice_state_update(self, data: GuildVoiceStatePayload) -> None:
self.session_id: str = data['session_id'] self.session_id: str = data['session_id']
channel_id = data['channel_id'] channel_id = data['channel_id']
@ -463,11 +301,7 @@ class VoiceClient(VoiceProtocol):
# We're being disconnected so cleanup # We're being disconnected so cleanup
await self.disconnect() await self.disconnect()
else: else:
guild = self.guild self.channel = channel_id and self.guild.get_channel(int(channel_id)) # type: ignore
if guild is not None:
self.channel = channel_id and guild.get_channel(int(channel_id)) # type: ignore # This won't be None
else:
self.channel = channel_id and self._state._get_private_channel(int(channel_id)) # type: ignore # This won't be None
else: else:
self._voice_state_complete.set() self._voice_state_complete.set()
@ -476,11 +310,8 @@ class VoiceClient(VoiceProtocol):
_log.info('Ignoring extraneous voice server update.') _log.info('Ignoring extraneous voice server update.')
return return
if 'token' in data: self.token = data['token']
self.token = data['token'] self.server_id = int(data['guild_id'])
self.server_id = server_id = utils._get_as_snowflake(data, 'guild_id')
if server_id is None:
self.server_id = utils._get_as_snowflake(data, 'channel_id')
endpoint = data.get('endpoint') endpoint = data.get('endpoint')
if endpoint is None or self.token is None: if endpoint is None or self.token is None:
@ -495,33 +326,35 @@ class VoiceClient(VoiceProtocol):
# Just in case, strip it off since we're going to add it later # Just in case, strip it off since we're going to add it later
self.endpoint: str = self.endpoint[6:] self.endpoint: str = self.endpoint[6:]
# This gets set later
self.endpoint_ip = MISSING self.endpoint_ip = MISSING
self.socket: socket.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.socket: socket.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.socket.setblocking(False) self.socket.setblocking(False)
if not self._handshaking: if not self._handshaking:
# If we're not handshaking then we need to terminate our previous connection to the websocket # If we're not handshaking then we need to terminate our previous connection in the websocket
await self.ws.close(4000) await self.ws.close(4000)
return return
self._voice_server_complete.set() self._voice_server_complete.set()
async def voice_connect(self) -> None: async def voice_connect(self) -> None:
channel = await self.channel._get_channel() if self.channel else None channel = self.channel
if self.guild: if self.guild:
await self.guild.change_voice_state(channel=channel) await self.guild.change_voice_state(channel=channel)
else: else:
await self._state.client.change_voice_state(channel=channel) await self._state.client.change_voice_state(channel=channel)
async def voice_disconnect(self) -> None: async def voice_disconnect(self) -> None:
guild = self.guild
_log.info( _log.info(
'The voice handshake is being terminated for channel ID %s (guild ID %s).', 'The voice handshake is being terminated for channel ID %s (guild ID %s).',
(await self.channel._get_channel()).id, self.channel.id,
getattr(self.guild, 'id', None), getattr(guild, 'id', None),
) )
if self.guild: if guild:
await self.guild.change_voice_state(channel=None) await guild.change_voice_state(channel=None)
else: else:
await self._state.client.change_voice_state(channel=None) await self._state.client.change_voice_state(channel=None)
@ -529,17 +362,17 @@ class VoiceClient(VoiceProtocol):
self._voice_state_complete.clear() self._voice_state_complete.clear()
self._voice_server_complete.clear() self._voice_server_complete.clear()
self._handshaking = True self._handshaking = True
_log.info('Starting voice handshake (connection attempt %d)...', self._connections + 1) _log.info('Starting voice handshake... (connection attempt %d)', self._connections + 1)
self._connections += 1 self._connections += 1
def finish_handshake(self) -> None: def finish_handshake(self) -> None:
_log.info('Voice handshake complete. Endpoint found: %s.', self.endpoint) _log.info('Voice handshake complete. Endpoint found %s', self.endpoint)
self._handshaking = False self._handshaking = False
self._voice_server_complete.clear() self._voice_server_complete.clear()
self._voice_state_complete.clear() self._voice_state_complete.clear()
async def connect_websocket(self, resume=False) -> DiscordVoiceWebSocket: async def connect_websocket(self) -> DiscordVoiceWebSocket:
ws = await DiscordVoiceWebSocket.from_client(self, resume=resume) ws = await DiscordVoiceWebSocket.from_client(self)
self._connected.clear() self._connected.clear()
while ws.secret_key is None: while ws.secret_key is None:
await ws.poll_event() await ws.poll_event()
@ -575,12 +408,11 @@ class VoiceClient(VoiceProtocol):
break break
except (ConnectionClosed, asyncio.TimeoutError): except (ConnectionClosed, asyncio.TimeoutError):
if reconnect: if reconnect:
_log.exception('Failed to connect to voice. Retrying...') _log.exception('Failed to connect to voice... Retrying...')
await asyncio.sleep(1 + i * 2.0) await asyncio.sleep(1 + i * 2.0)
await self.voice_disconnect() await self.voice_disconnect()
continue continue
else: else:
await self.disconnect(force=True)
raise raise
if self._runner is MISSING: if self._runner is MISSING:
@ -608,20 +440,6 @@ class VoiceClient(VoiceProtocol):
else: else:
return True return True
async def potential_resume(self) -> bool:
# Attempt to stop the player thread from playing early
self._connected.clear()
self._potentially_reconnecting = True
try:
self.ws = await self.connect_websocket(resume=True)
except (ConnectionClosed, asyncio.TimeoutError):
return False # Reconnect normally if RESUME failed
else:
return True
finally:
self._potentially_reconnecting = False
@property @property
def latency(self) -> float: def latency(self) -> float:
""":class:`float`: Latency between a HEARTBEAT and a HEARTBEAT_ACK in seconds. """:class:`float`: Latency between a HEARTBEAT and a HEARTBEAT_ACK in seconds.
@ -650,21 +468,16 @@ class VoiceClient(VoiceProtocol):
await self.ws.poll_event() await self.ws.poll_event()
except (ConnectionClosed, asyncio.TimeoutError) as exc: except (ConnectionClosed, asyncio.TimeoutError) as exc:
if isinstance(exc, ConnectionClosed): if isinstance(exc, ConnectionClosed):
if exc.code == 1000: # The following close codes are undocumented so I will document them here.
# 1000 - normal closure (obviously)
# 4014 - voice channel has been deleted.
# 4015 - voice server has crashed
if exc.code in (1000, 4015):
_log.info('Disconnecting from voice normally, close code %d.', exc.code) _log.info('Disconnecting from voice normally, close code %d.', exc.code)
await self.disconnect() await self.disconnect()
break break
if exc.code == 4015:
_log.info('Disconnected from voice (close code %d), potentially RESUMEing...', exc.code)
successful = await self.potential_resume()
if not successful:
_log.info('RESUME was unsuccessful, disconnecting from voice normally...')
await self.disconnect()
break
else:
continue
if exc.code == 4014: if exc.code == 4014:
_log.info('Disconnected from voice by force (close code %d)... potentially reconnecting.', exc.code) _log.info('Disconnected from voice by force... potentially reconnecting.')
successful = await self.potential_reconnect() successful = await self.potential_reconnect()
if not successful: if not successful:
_log.info('Reconnect was unsuccessful, disconnecting from voice normally...') _log.info('Reconnect was unsuccessful, disconnecting from voice normally...')
@ -681,11 +494,12 @@ class VoiceClient(VoiceProtocol):
_log.exception('Disconnected from voice... Reconnecting in %.2fs.', retry) _log.exception('Disconnected from voice... Reconnecting in %.2fs.', retry)
self._connected.clear() self._connected.clear()
await asyncio.sleep(retry) await asyncio.sleep(retry)
await self.voice_disconnect()
try: try:
await self.connect(reconnect=True, timeout=self.timeout) await self.connect(reconnect=True, timeout=self.timeout)
except asyncio.TimeoutError: except asyncio.TimeoutError:
# We've retried 5 times, let's continue the loop # at this point we've retried 5 times... let's continue the loop.
_log.warning('Could not connect to voice. Retrying...') _log.warning('Could not connect to voice... Retrying...')
continue continue
async def disconnect(self, *, force: bool = False) -> None: async def disconnect(self, *, force: bool = False) -> None:
@ -696,7 +510,7 @@ class VoiceClient(VoiceProtocol):
if not force and not self.is_connected(): if not force and not self.is_connected():
return return
self.player.stop() # TODO: Stop listener self.stop()
self._connected.clear() self._connected.clear()
try: try:
@ -716,8 +530,8 @@ class VoiceClient(VoiceProtocol):
Parameters Parameters
----------- -----------
channel: Optional[:class:`abc.Snowflake`] channel: Optional[:class:`~abc.Snowflake`]
The channel to move to. Must be a :class:`abc.Connectable`. The channel to move to. Must be a voice channel.
""" """
if self.guild: if self.guild:
await self.guild.change_voice_state(channel=channel) await self.guild.change_voice_state(channel=channel)
@ -728,37 +542,12 @@ class VoiceClient(VoiceProtocol):
"""Indicates if the voice client is connected to voice.""" """Indicates if the voice client is connected to voice."""
return self._connected.is_set() return self._connected.is_set()
# Audio related # audio related
def _flip_ssrc(self, query) -> Optional[int]: def _get_voice_packet(self, data):
value = self.idrcs.get(query)
if value is None:
value = self.ssids.get(query)
return value
def _set_ssrc(self, user_id, ssrc) -> None:
self.idrcs[user_id] = ssrc
self.ssids[ssrc] = user_id
def _checked_add(self, attr, value, limit) -> None:
val = getattr(self, attr)
if val + value > limit:
setattr(self, attr, 0)
else:
setattr(self, attr, val + value)
@staticmethod
def _strip_header(data) -> bytes:
if data[0] == 0xBE and data[1] == 0xDE and len(data) > 4:
_, length = struct.unpack_from('>HH', data)
offset = 4 + length * 4
data = data[offset:]
return data
def _get_voice_packet(self, data) -> bytes:
header = bytearray(12) header = bytearray(12)
# Formulate RTP header # Formulate rtp header
header[0] = 0x80 header[0] = 0x80
header[1] = 0x78 header[1] = 0x78
struct.pack_into('>H', header, 2, self.sequence) struct.pack_into('>H', header, 2, self.sequence)
@ -775,44 +564,22 @@ class VoiceClient(VoiceProtocol):
return header + box.encrypt(bytes(data), bytes(nonce)).ciphertext return header + box.encrypt(bytes(data), bytes(nonce)).ciphertext
def _decrypt_xsalsa20_poly1305(self, header, data):
box = nacl.secret.SecretBox(bytes(self.secret_key))
nonce = bytearray(24)
nonce[:12] = header
return self._strip_header(box.decrypt(bytes(data), bytes(nonce)))
def _encrypt_xsalsa20_poly1305_suffix(self, header: bytes, data) -> bytes: def _encrypt_xsalsa20_poly1305_suffix(self, header: bytes, data) -> bytes:
box = nacl.secret.SecretBox(bytes(self.secret_key)) box = nacl.secret.SecretBox(bytes(self.secret_key))
nonce = nacl.utils.random(nacl.secret.SecretBox.NONCE_SIZE) nonce = nacl.utils.random(nacl.secret.SecretBox.NONCE_SIZE)
return header + box.encrypt(bytes(data), nonce).ciphertext + nonce return header + box.encrypt(bytes(data), nonce).ciphertext + nonce
def _decrypt_xsalsa20_poly1305_suffix(self, header, data):
box = nacl.secret.SecretBox(bytes(self.secret_key))
nonce_size = nacl.secret.SecretBox.NONCE_SIZE
nonce = data[-nonce_size:]
return self._strip_header(box.decrypt(bytes(data[:-nonce_size]), nonce))
def _encrypt_xsalsa20_poly1305_lite(self, header: bytes, data) -> bytes: def _encrypt_xsalsa20_poly1305_lite(self, header: bytes, data) -> bytes:
box = nacl.secret.SecretBox(bytes(self.secret_key)) box = nacl.secret.SecretBox(bytes(self.secret_key))
nonce = bytearray(24) nonce = bytearray(24)
nonce[:4] = struct.pack('>I', self._lite_nonce) nonce[:4] = struct.pack('>I', self._lite_nonce)
self._checked_add('_lite_nonce', 1, 4294967295) self.checked_add('_lite_nonce', 1, 4294967295)
return header + box.encrypt(bytes(data), bytes(nonce)).ciphertext + nonce[:4] return header + box.encrypt(bytes(data), bytes(nonce)).ciphertext + nonce[:4]
def _decrypt_xsalsa20_poly1305_lite(self, header, data): def play(self, source: AudioSource, *, after: Optional[Callable[[Optional[Exception]], Any]] = None) -> None:
box = nacl.secret.SecretBox(bytes(self.secret_key))
nonce = bytearray(24)
nonce[:4] = data[-4:]
data = data[:-4]
return self._strip_header(box.decrypt(bytes(data), bytes(nonce)))
def play(self, source: AudioSource, *, after: Callable[[Optional[Exception]], Any] = None) -> None:
"""Plays an :class:`AudioSource`. """Plays an :class:`AudioSource`.
The finalizer, ``after`` is called after the source has been exhausted The finalizer, ``after`` is called after the source has been exhausted
@ -840,10 +607,45 @@ class VoiceClient(VoiceProtocol):
OpusNotLoaded OpusNotLoaded
Source is not opus encoded and opus is not loaded. Source is not opus encoded and opus is not loaded.
""" """
return self.player.play(source, after=after)
def listen(self, *args, **kwargs): if not self.is_connected():
return self.listener.listen(*args, **kwargs) raise ClientException('Not connected to voice.')
if self.is_playing():
raise ClientException('Already playing audio.')
if not isinstance(source, AudioSource):
raise TypeError(f'source must be an AudioSource not {source.__class__.__name__}')
if not self.encoder and not source.is_opus():
self.encoder = opus.Encoder()
self._player = AudioPlayer(source, self, after=after)
self._player.start()
def is_playing(self) -> bool:
"""Indicates if we're currently playing audio."""
return self._player is not None and self._player.is_playing()
def is_paused(self) -> bool:
"""Indicates if we're playing audio, but if we're paused."""
return self._player is not None and self._player.is_paused()
def stop(self) -> None:
"""Stops playing audio."""
if self._player:
self._player.stop()
self._player = None
def pause(self) -> None:
"""Pauses the audio playing."""
if self._player:
self._player.pause()
def resume(self) -> None:
"""Resumes the audio playing."""
if self._player:
self._player.resume()
@property @property
def source(self) -> Optional[AudioSource]: def source(self) -> Optional[AudioSource]:
@ -851,29 +653,19 @@ class VoiceClient(VoiceProtocol):
This property can also be used to change the audio source currently being played. This property can also be used to change the audio source currently being played.
""" """
return self.player.source return self._player.source if self._player else None
@source.setter @source.setter
def source(self, value: AudioSource) -> None: def source(self, value: AudioSource) -> None:
self.player.source = value if not isinstance(value, AudioSource):
raise TypeError(f'expected AudioSource not {value.__class__.__name__}.')
@property
def sink(self) -> Optional[AudioSink]:
"""Optional[:class:`AudioSink`]: Where received audio is being sent.
This property can also be used to change the value. if self._player is None:
""" raise ValueError('Not playing anything.')
return self.listener.sink
@sink.setter self._player._set_source(value)
def sink(self, value):
self.listener.sink = value
# if not isinstance(value, AudioSink):
# raise TypeError('Expected AudioSink not {value.__class__.__name__}')
# if self._recorder is None:
# raise ValueError('Not listening')
def send_audio_packet(self, data: bytes) -> None: def send_audio_packet(self, data: bytes, *, encode: bool = True) -> None:
"""Sends an audio packet composed of the data. """Sends an audio packet composed of the data.
You must be connected to play audio. You must be connected to play audio.
@ -881,7 +673,9 @@ class VoiceClient(VoiceProtocol):
Parameters Parameters
---------- ----------
data: :class:`bytes` data: :class:`bytes`
The :term:`py:bytes-like object` denoting Opus voice data. The :term:`py:bytes-like object` denoting PCM or Opus voice data.
encode: :class:`bool`
Indicates if ``data`` should be encoded into Opus.
Raises Raises
------- -------
@ -890,12 +684,16 @@ class VoiceClient(VoiceProtocol):
opus.OpusError opus.OpusError
Encoding the data failed. Encoding the data failed.
""" """
self._checked_add('sequence', 1, 65535)
packet = self._get_voice_packet(data)
self.checked_add('sequence', 1, 65535)
if encode:
encoded_data = self.encoder.encode(data, self.encoder.SAMPLES_PER_FRAME)
else:
encoded_data = data
packet = self._get_voice_packet(encoded_data)
try: try:
self.socket.sendto(packet, (self.endpoint_ip, self.voice_port)) self.socket.sendto(packet, (self.endpoint_ip, self.voice_port))
except BlockingIOError: except BlockingIOError:
_log.warning('A packet has been dropped (seq: %s, timestamp: %s).', self.sequence, self.timestamp) _log.warning('A packet has been dropped (seq: %s, timestamp: %s)', self.sequence, self.timestamp)
self._checked_add('timestamp', opus.Encoder.SAMPLES_PER_FRAME, 4294967295) self.checked_add('timestamp', opus.Encoder.SAMPLES_PER_FRAME, 4294967295)

Loading…
Cancel
Save