Debian Codegraph @

docpages/advanced_reference/voice_model.md

raw symbols SCIP
1 \page voice-model Voice Model
2
3 # High Level Summary
4
5 Discord's audio system consists of several layers and inter-related systems as shown in the flow chart below.
6
7 At the top level, connecting to a voice server requires the library to request the details of a voice server from Discord via the websocket for the shard where the
8 server is located. Performing this request will make Discord reply with a websocket URI and an ephemeral token (not the bot token) which are used to establish an
9 initial connection to this secondary websocket. Every connection to a voice channel creates a separate secondary websocket.
10
11 Once connected to this websocket, the library negotiates which protocols it supports and what encryption schemes to use. If you enabled DAVE (Discord's end-to-end
12 encryption scheme) this is negotiated first. An MLS (message layer security) group is joined or created. If you did not enable DAVE, this step is bypassed.
13
14 The secondary websocket then gives the library a shared encryption secret and the hostname of an RTP server, which is used to encrypt RTP packets using libssl.
15 This is stored for later.
16
17 The next step is to send an initial packet to the RTP server so that the library can detect the public IP where the bot is running. Once the RTP server replies,
18 the bot may tell the websocket what encryption protocols it is going to use to encrypt the RTP packet contents (leaving the RTP header somwhat intact).
19
20 The library is now in an initialised state and will accept method calls for `send_audio_raw()` and `send_audio_opus()`. If you send raw audio, it will first be
21 encoded as OPUS using libopus, and potentially repacketized to fit into UDP packets, with larger streams being split into multiple smaller packets that are scheduled
22 to be sent in the future.
23
24 If at this point DAVE is enabled, the contents of the OPUS encoded audio are encrypted using the AES 128 bit AEAD cipher and using the bot's MLS ratchet, derived
25 during the MLS negotiation which was carried out earlier. All valid participants in the voice channel may use their private key, and the public key derived from
26 their ratchets, to decrypt the OPUS audio.
27
28 Regardless of if DAVE is enabled or not, the OPUS stream (encrypted by DAVE, or "plaintext") is placed into an RTP packet, and then encrypted using the shared secret
29 given by the websocket, known only to you and Discord, using the xchacha20 poly1305 cipher.
30
31 The completed packet, potentially with two separate layers of encryption (one with a key only you and Discord know, and one with a key only you and participants in the
32 voice chat know!), plus opus encoded audio is sent on its way via UDP to the RTP server, where Discord promptly distribute it to all participants in the chat.
33
34 \image html audioframe.svg
35
36 After reading all this, go get a coffee or something, you deserve it! ☕
37
38 # Flow Diagram
39
40 \dot
41 digraph "Example Directory" {
42 graph [ranksep=1];
43 node [colorscheme="blues9", fontname="helvetica"];
44
45 "Your bot" [style=filled, color=1, shape=rect]
46 "Discord" [style=filled, color=1, shape=rect]
47
48 subgraph cluster_0 {
49 style=filled;
50 color=lightgrey;
51 node [style=filled, color=3, shape=rect]
52 "guild::connect_member_voice";
53 "discord_client::connect_voice";
54
55 "guild::connect_member_voice" -> "discord_client::connect_voice";
56
57 label = "This is the front-end of D++.\n'connect_voice' will now queue a JSON message.";
58 }
59
60 subgraph cluster_1 {
61 style=filled;
62 color=lightgrey;
63 node [style=filled, color=2, shape=rect]
64 "message_queue";
65
66 label = "This holds all our messages.\n'one_second_timer' reads this data";
67 }
68
69 subgraph cluster_2 {
70 style=filled;
71 color=lightgrey;
72 node [style=filled, color=3, shape=rect]
73 "discord_client::one_second_timer";
74 "websocket_client::write";
75 "ssl_connection::write";
76
77 "discord_client::one_second_timer" -> "websocket_client::write";
78 "websocket_client::write" -> "ssl_connection::write";
79 "ssl_connection::write" -> "Discord";
80
81 label = "This is where we start sending\nwebsocket connections to Discord.";
82 }
83
84 subgraph cluster_3 {
85 style=filled;
86 color=lightgrey;
87 node [style=filled, color=3, shape=rect]
88 "ssl_connection::read_loop";
89 "Response from Discord?";
90 "No";
91 "HTTP/1.1 204 No Content...";
92 "HTTP/1.1 101 Switching Protocols";
93
94 "ssl_connection::read_loop" -> "Response from Discord?";
95 "Response from Discord?" -> "No";
96 "Response from Discord?" -> "HTTP/1.1 204 No Content...";
97 "Response from Discord?" -> "HTTP/1.1 101 Switching Protocols";
98 "No" -> "ssl_connection::read_loop";
99
100 "Discord" -> "HTTP/1.1 204 No Content...";
101 "Discord" -> "HTTP/1.1 101 Switching Protocols";
102
103 label = "Now, we're waiting for a response from Discord.\nIf we receive 204, we'll start initiating voiceconn. However, if we receive 101, then we can do all the voice stuff.";
104 }
105
106 subgraph cluster_4 {
107 style=filled;
108 color=lightgrey;
109 node [style=filled, color=3, shape=rect]
110 "voice_state_update::handle";
111 "voice_server_update::handle";
112
113 "HTTP/1.1 204 No Content..." -> "voice_state_update::handle";
114 "HTTP/1.1 204 No Content..." -> "voice_server_update::handle";
115
116 label = "These events can fire in any order. Discord picks whatever it likes.";
117 }
118
119 subgraph cluster_5 {
120 style=filled;
121 color=lightgrey;
122 node [style=filled, color=3, shape=rect]
123 "voiceconn::connect";
124 "new discord_voice_client"
125 "websocket_client::connect"
126 "discord_voice_client::run"
127 "discord_voice_client::thread_run"
128
129 "voiceconn::connect" -> "new discord_voice_client";
130 "new discord_voice_client" -> "websocket_client::connect";
131 "websocket_client::connect" -> "websocket_client::write";
132
133 "voiceconn::connect" -> "discord_voice_client::run" [label="Once websocket_client has finished"];
134 "discord_voice_client::run" -> "discord_voice_client::thread_run";
135 "discord_voice_client::thread_run" -> "ssl_connection::read_loop";
136
137 label = "Voice initalisation.\nThis will only fire when 'voice_server_update' AND 'voice_state_update' has fired.\nIf everything goes well, Discord should send back '101 Switching Protocals'.";
138 }
139
140 subgraph cluster_6 {
141 style=filled;
142 color=lightgrey;
143 node [style=filled, color=3, shape=rect]
144 "discord_voice_client::handle_frame";
145
146 "HTTP/1.1 101 Switching Protocols" -> "discord_voice_client::handle_frame";
147
148 label = "Do the voice stuff.";
149
150 "discord_voice_client::handle_frame"-> "DAVE enabled";
151 "discord_voice_client::handle_frame"-> "DAVE disabled";
152 "DAVE disabled"->"discord_voice_client::send_audio_*()";
153 "discord_voice_client::send_audio_*()" -> "Dave encryption on";
154 "discord_voice_client::send_audio_*()" -> "Dave encryption off";
155 "Dave encryption on" -> "AES AEAD encryption\nof OPUS stream\nusing ratchet";
156 "AES AEAD encryption\nof OPUS stream\nusing ratchet" -> "XChaCha20-Poly1305 encryption";
157 "Dave encryption off" -> "XChaCha20-Poly1305 encryption";
158 "XChaCha20-Poly1305 encryption" -> "UDP sendto";
159 "UDP sendto" -> "Discord RTP server";
160 "DAVE enabled" -> "MLS send key package";
161 "MLS send key package" -> "MLS receive external sender";
162 "MLS receive external sender" -> "MLS proposals";
163 "MLS proposals" -> "MLS Welcome";
164 "MLS proposals" -> "MLS Commit";
165 "MLS Commit" -> "DAVE begin transition";
166 "MLS Welcome" -> "DAVE begin transition";
167 "DAVE begin transition" -> "Dave execute transition";
168 "Dave execute transition" -> "discord_voice_client::send_audio_*()";
169 }
170
171 "Your bot" -> "guild::connect_member_voice";
172
173 "discord_client::connect_voice" -> "message_queue";
174
175 "message_queue" -> "discord_client::one_second_timer";
176 "discord_client::one_second_timer" -> "message_queue";
177
178 "voice_state_update::handle" -> "voiceconn::connect";
179 "voice_server_update::handle" -> "voiceconn::connect";
180 }
181 \enddot