concord 2.1.2

A terminal user interface client for Discord
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
#[cfg(test)]
use std::num::NonZeroU16;
use std::{
    collections::HashMap,
    fmt,
    sync::{Arc, RwLock},
    time::{Duration, Instant},
};

#[cfg(feature = "voice-playback")]
mod audio_buffer;
mod dave;
mod gateway;
mod info;
#[cfg(any(test, feature = "voice-playback"))]
mod microphone;
mod opus;
mod playback;
mod rtp;
mod runtime;
mod state;

#[cfg(all(feature = "voice-playback", not(test)))]
use gateway::voice_speaking_payload;
#[cfg(test)]
use gateway::*;
#[cfg(not(test))]
use gateway::{run_voice_gateway_session, send_voice_binary, send_voice_text};
pub use info::{VoiceConnectionStatus, VoiceServerInfo, VoiceSoundKind, VoiceStateInfo};
#[cfg(all(feature = "voice-playback", target_os = "linux", not(test)))]
use microphone::log_captured_alsa_errors;
#[cfg(all(feature = "voice-playback", not(test)))]
use microphone::run_voice_udp_transmit;
#[cfg(test)]
use microphone::*;
#[cfg(test)]
use runtime::{VoiceRuntimeAction, VoiceRuntimeState};
pub(crate) use runtime::{forward_app_event, run_voice_runtime};
pub(in crate::discord) use state::VoiceState;
pub use state::{CurrentVoiceConnectionState, VoiceParticipantState};

use self::opus::VoiceOpusDecode;
#[cfg(any(test, feature = "voice-playback"))]
use self::opus::VoiceOpusEncode;
#[cfg(test)]
use self::opus::mix_voice_decoded_samples;
#[cfg(test)]
use ::opus::{Channels, Decoder as OpusDecoder};
#[cfg(all(test, feature = "voice-playback"))]
use audio_buffer::VoiceAudioBuffer;
use dave::{VoiceDaveState, VoiceMediaPayload, voice_speaking_microphone_active};
#[cfg(test)]
use dave::{VoiceSpeakingState, looks_like_dave_media_frame};
#[cfg(feature = "voice-playback")]
use playback::VoiceAudioOutput;
#[cfg(test)]
use playback::VoicePlaybackPlayoutBuffer;
use playback::{VoicePlaybackFrame, VoicePlaybackGate};
#[cfg(test)]
use playback::{VoicePlaybackPostProcess, VoicePlayoutFrame};
#[cfg(all(test, feature = "voice-playback"))]
use playback::{
    voice_sample_to_i16, voice_sample_to_u8, voice_sample_to_u16, write_voice_output_frame,
};
use rtp::{
    RtpHeader, VoiceOutboundRtpState, VoiceRtpDecryptor, VoiceRtpEncryptor, looks_like_rtcp_packet,
    parse_rtp_header, rtcp_sender_ssrc,
};

#[cfg(test)]
use aes_gcm::{
    Aes256Gcm, Nonce as AesGcmNonce,
    aead::{Aead, KeyInit, Payload},
};
#[cfg(test)]
use chacha20poly1305::{XChaCha20Poly1305, XNonce};
#[cfg(feature = "voice-playback")]
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use futures::{SinkExt, StreamExt};
use serde_json::{Value, json};
#[cfg(feature = "voice-playback")]
use std::sync::Mutex as StdMutex;
#[cfg(feature = "voice-playback")]
use std::sync::atomic::{AtomicBool, AtomicU8, AtomicU64, Ordering};
#[cfg(feature = "voice-playback")]
use std::sync::mpsc::{Receiver as StdReceiver, SyncSender, TryRecvError, sync_channel};
#[cfg(feature = "voice-playback")]
use tokio::time::MissedTickBehavior;
use tokio::{
    net::UdpSocket,
    sync::{Mutex, Mutex as AsyncMutex, mpsc, watch},
    task::JoinHandle,
    time::{sleep, timeout},
};
use tokio_tungstenite::{connect_async, tungstenite::Message as WsMessage};

use crate::config::{MicrophoneSensitivityDb, VoiceVolumePercent};
use crate::discord::{
    DiscordState, SequencedAppEvent, SnapshotRevision,
    ids::{
        Id,
        marker::{ChannelMarker, GuildMarker, UserMarker},
    },
};
use crate::logging;

use super::{client::publish_app_event, events::AppEvent};

const VOICE_GATEWAY_VERSION: u8 = 9;
const VOICE_WEBSOCKET_CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
const UDP_DISCOVERY_PACKET_LEN: usize = 74;
const UDP_DISCOVERY_TIMEOUT: Duration = Duration::from_secs(5);
const RTP_HEADER_MIN_LEN: usize = 12;
const RTP_VERSION: u8 = 2;
const DISCORD_VOICE_PAYLOAD_TYPE: u8 = 0x78;
const RTP_HEADER_EXTENSION_BYTES: usize = 4;
const RTP_EXTENSION_WORD_BYTES: usize = 4;
const RTP_AEAD_TAG_BYTES: usize = 16;
const RTP_AEAD_NONCE_SUFFIX_BYTES: usize = 4;
const RTCP_MIN_PACKET_BYTES: usize = 4;
const RTCP_SENDER_SSRC_OFFSET: usize = 4;
const RTCP_SENDER_SSRC_BYTES: usize = 4;
const DAVE_MIN_SUPPLEMENTAL_BYTES: usize = 11;
const DAVE_MAGIC_MARKER: [u8; 2] = [0xfa, 0xfa];
const DISCORD_VOICE_SAMPLE_RATE: u32 = 48_000;
const DISCORD_VOICE_CHANNELS: u16 = 2;
#[cfg(feature = "voice-playback")]
const DISCORD_VOICE_CHANNELS_USIZE: usize = DISCORD_VOICE_CHANNELS as usize;
// These outbound helpers are intentionally not wired into the runtime yet.
// They let tests prove packet shapes before any live transmit path is added.
#[allow(dead_code)]
const DISCORD_OPUS_FRAME_SAMPLES_PER_CHANNEL: usize = 960;
#[allow(dead_code)]
const DISCORD_OPUS_20MS_STEREO_SAMPLES: usize =
    DISCORD_OPUS_FRAME_SAMPLES_PER_CHANNEL * DISCORD_VOICE_CHANNELS as usize;
#[allow(dead_code)]
const DISCORD_OPUS_TIMESTAMP_INCREMENT: u32 = DISCORD_OPUS_FRAME_SAMPLES_PER_CHANNEL as u32;
#[allow(dead_code)]
const DISCORD_OPUS_SILENCE_FRAME: [u8; 3] = [0xf8, 0xff, 0xfe];
#[allow(dead_code)]
const DISCORD_TRAILING_SILENCE_FRAMES: usize = 5;
#[allow(dead_code)]
const OPUS_MAX_ENCODED_FRAME_BYTES: usize = 4000;
#[cfg(feature = "voice-playback")]
const VOICE_MIC_PCM_FRAME_QUEUE: usize = 4;
#[cfg(feature = "voice-playback")]
const VOICE_MIC_PREFERRED_BUFFER_FRAMES: u32 = 480;
#[cfg(feature = "voice-playback")]
const VOICE_MIC_GATE_HANGOVER_FRAMES: u8 = 8;
#[cfg(feature = "voice-playback")]
const VOICE_MIC_OVERLOAD_RECOVERY_FRAMES: u8 = 8;
#[cfg(feature = "voice-playback")]
const VOICE_MIC_HANDLING_NOISE_SUPPRESSION_FRAMES: u8 = 12;
#[cfg(any(test, feature = "voice-playback"))]
const VOICE_MIC_OVERLOAD_MIN_CLIPPED_SAMPLES: usize = 8;
#[cfg(any(test, feature = "voice-playback"))]
const VOICE_MIC_OVERLOAD_SEVERE_CLIPPED_SAMPLES: usize = DISCORD_OPUS_20MS_STEREO_SAMPLES / 20;
#[cfg(any(test, feature = "voice-playback"))]
const VOICE_MIC_OVERLOAD_EXTREME_CLIPPED_SAMPLES: usize = DISCORD_OPUS_20MS_STEREO_SAMPLES / 8;
#[cfg(any(test, feature = "voice-playback"))]
const VOICE_MIC_HANDLING_NOISE_DELTA: i32 = 42_000;
#[cfg(any(test, feature = "voice-playback"))]
const VOICE_MIC_OVERLOAD_CLIPPED_STEP_DELTA: i32 = 32_000;
#[cfg(any(test, feature = "voice-playback"))]
const VOICE_MIC_OVERLOAD_IMPULSE_DELTA: i32 = 36_000;
#[cfg(any(test, feature = "voice-playback"))]
const VOICE_MIC_OVERLOAD_ATTENUATION_GAIN: f32 = 0.35;
#[cfg(any(test, feature = "voice-playback"))]
const VOICE_MIC_HANDLING_NOISE_GAIN: f32 = 0.0;
#[cfg(any(test, feature = "voice-playback"))]
const VOICE_MIC_OVERLOAD_TRANSIENT_GAIN: f32 = 0.03;
#[cfg(feature = "voice-playback")]
const VOICE_MIC_OVERLOAD_RECOVERY_START_GAIN: f32 = 0.15;
#[allow(dead_code)]
#[cfg(any(test, feature = "voice-playback"))]
const VOICE_MIC_TRANSMIT_BOOST_GAIN: f32 = 1.15;
#[cfg(any(test, feature = "voice-playback"))]
const VOICE_MIC_SOFT_LIMIT_THRESHOLD: f32 = 0.85;
#[cfg(any(test, feature = "voice-playback"))]
const VOICE_MIC_SOFT_LIMIT_CEILING: f32 = 0.95;
#[cfg(any(test, feature = "voice-playback"))]
const VOICE_MIC_SOFT_LIMIT_CURVE: f32 = 4.0;
const OPUS_MAX_FRAME_SAMPLES_PER_CHANNEL: usize = 5760;
const VOICE_PLAYBACK_FRAME_QUEUE: usize = 256;
const VOICE_PLAYBACK_FRAME_DURATION: Duration = Duration::from_millis(20);
#[cfg(feature = "voice-playback")]
const VOICE_TRANSMIT_STATS_LOG_INTERVAL: Duration = Duration::from_secs(5);
const VOICE_PLAYBACK_JITTER_BUFFER_FRAMES: usize = 3;
const VOICE_PLAYBACK_JITTER_BUFFER_DELAY: Duration = Duration::from_millis(60);
const VOICE_PLAYBACK_MAX_BUFFERED_FRAMES_PER_SSRC: usize = 32;
const VOICE_PLAYBACK_MAX_CONSECUTIVE_PLC_FRAMES: usize = 5;
#[cfg(feature = "voice-playback")]
const VOICE_OUTPUT_UNDERRUN_FADE_MILLIS: u32 = 5;
const VOICE_OUTPUT_LOW_PASS_CUTOFF_HZ: f32 = 8_000.0;
#[cfg(feature = "voice-playback")]
const VOICE_AUDIO_OUTPUT_QUEUE: usize = 64;
const AEAD_AES256_GCM_RTPSIZE: &str = "aead_aes256_gcm_rtpsize";
const AEAD_XCHACHA20_POLY1305_RTPSIZE: &str = "aead_xchacha20_poly1305_rtpsize";
const VOICE_REMOTE_SPEAKING_TTL: Duration = Duration::from_millis(500);
const VOICE_REMOTE_SPEAKING_SWEEP_INTERVAL: Duration = Duration::from_millis(250);

const VOICE_OP_READY: u8 = 2;
const VOICE_OP_SESSION_DESCRIPTION: u8 = 4;
const VOICE_OP_SPEAKING: u8 = 5;
const VOICE_OP_HEARTBEAT_ACK: u8 = 6;
const VOICE_OP_HELLO: u8 = 8;
const VOICE_OP_CLIENTS_CONNECT: u8 = 11;
const VOICE_OP_CLIENT_DISCONNECT: u8 = 13;
const VOICE_OP_MEDIA_SINK_WANTS: u8 = 15;
const VOICE_OP_CLIENT_FLAGS: u8 = 18;
const VOICE_OP_CLIENT_PLATFORM: u8 = 20;
const VOICE_OP_DAVE_PREPARE_TRANSITION: u8 = 21;
const VOICE_OP_DAVE_EXECUTE_TRANSITION: u8 = 22;
const VOICE_OP_DAVE_TRANSITION_READY: u8 = 23;
const VOICE_OP_DAVE_PREPARE_EPOCH: u8 = 24;
const VOICE_OP_DAVE_MLS_EXTERNAL_SENDER: u8 = 25;
const VOICE_OP_DAVE_MLS_KEY_PACKAGE: u8 = 26;
const VOICE_OP_DAVE_MLS_PROPOSALS: u8 = 27;
const VOICE_OP_DAVE_MLS_COMMIT_WELCOME: u8 = 28;
const VOICE_OP_DAVE_MLS_ANNOUNCE_COMMIT_TRANSITION: u8 = 29;
const VOICE_OP_DAVE_MLS_WELCOME: u8 = 30;
const VOICE_OP_DAVE_MLS_INVALID_COMMIT_WELCOME: u8 = 31;

type VoiceGatewayStream =
    tokio_tungstenite::WebSocketStream<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>>;
type VoiceWriter = Arc<Mutex<futures::stream::SplitSink<VoiceGatewayStream, WsMessage>>>;

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) enum VoiceRuntimeEvent {
    Requested(Option<CurrentVoiceConnectionState>),
    CurrentUserReady(Option<Id<UserMarker>>),
    VoiceState(VoiceStateInfo),
    VoiceServer(VoiceServerInfo),
    ConnectionEnded {
        guild_id: Id<GuildMarker>,
        channel_id: Id<ChannelMarker>,
        session_id: String,
        endpoint: String,
    },
    Shutdown,
}

#[derive(Clone)]
pub(crate) struct VoiceStatusPublisher {
    effects_tx: mpsc::Sender<SequencedAppEvent>,
    snapshots_tx: watch::Sender<SnapshotRevision>,
    state: Arc<RwLock<DiscordState>>,
    revision: Arc<RwLock<SnapshotRevision>>,
    publish_lock: Arc<AsyncMutex<()>>,
}

#[derive(Clone, Eq, PartialEq)]
struct VoiceGatewaySession {
    guild_id: Id<GuildMarker>,
    channel_id: Id<ChannelMarker>,
    user_id: Id<UserMarker>,
    session_id: String,
    endpoint: String,
    token: String,
}

impl VoiceStatusPublisher {
    pub(crate) fn new(
        effects_tx: mpsc::Sender<SequencedAppEvent>,
        snapshots_tx: watch::Sender<SnapshotRevision>,
        state: Arc<RwLock<DiscordState>>,
        revision: Arc<RwLock<SnapshotRevision>>,
        publish_lock: Arc<AsyncMutex<()>>,
    ) -> Self {
        Self {
            effects_tx,
            snapshots_tx,
            state,
            revision,
            publish_lock,
        }
    }

    async fn publish(
        &self,
        session: &VoiceGatewaySession,
        status: VoiceConnectionStatus,
        message: impl Into<String>,
    ) {
        publish_app_event(
            &self.effects_tx,
            &self.snapshots_tx,
            &self.state,
            &self.revision,
            &self.publish_lock,
            &AppEvent::VoiceConnectionStatusChanged {
                guild_id: session.guild_id,
                channel_id: Some(session.channel_id),
                status,
                message: Some(message.into()),
            },
        )
        .await;
    }

    async fn publish_speaking(
        &self,
        session: &VoiceGatewaySession,
        user_id: Id<UserMarker>,
        speaking: bool,
    ) {
        publish_app_event(
            &self.effects_tx,
            &self.snapshots_tx,
            &self.state,
            &self.revision,
            &self.publish_lock,
            &AppEvent::VoiceSpeakingUpdate {
                guild_id: session.guild_id,
                channel_id: session.channel_id,
                user_id,
                speaking,
            },
        )
        .await;
    }
}

impl VoiceGatewaySession {
    fn matches_connection_end(
        &self,
        guild_id: Id<GuildMarker>,
        channel_id: Id<ChannelMarker>,
        session_id: &str,
        endpoint: &str,
    ) -> bool {
        self.guild_id == guild_id
            && self.channel_id == channel_id
            && self.session_id == session_id
            && self.endpoint == endpoint
    }

    fn connection_ended_event(&self) -> VoiceRuntimeEvent {
        VoiceRuntimeEvent::ConnectionEnded {
            guild_id: self.guild_id,
            channel_id: self.channel_id,
            session_id: self.session_id.clone(),
            endpoint: self.endpoint.clone(),
        }
    }
}

impl VoiceSpeakingTracker {
    fn record_remote(
        &mut self,
        user_id: Id<UserMarker>,
        speaking: bool,
        now: Instant,
    ) -> Option<bool> {
        if speaking {
            let was_active = self.remote_deadlines.contains_key(&user_id);
            self.remote_deadlines
                .insert(user_id, now + VOICE_REMOTE_SPEAKING_TTL);
            return (!was_active).then_some(true);
        }
        if self.remote_deadlines.remove(&user_id).is_some() {
            Some(false)
        } else {
            None
        }
    }

    fn record_local(&mut self, speaking: bool) -> Option<bool> {
        if self.local_speaking == speaking {
            return None;
        }
        self.local_speaking = speaking;
        Some(speaking)
    }

    fn expire_remote(&mut self, now: Instant) -> Vec<Id<UserMarker>> {
        let expired = self
            .remote_deadlines
            .iter()
            .filter_map(|(user_id, deadline)| (*deadline <= now).then_some(*user_id))
            .collect::<Vec<_>>();
        for user_id in &expired {
            self.remote_deadlines.remove(user_id);
        }
        expired
    }

    fn clear_all(&mut self, local_user_id: Id<UserMarker>) -> Vec<Id<UserMarker>> {
        let mut cleared = self.remote_deadlines.keys().copied().collect::<Vec<_>>();
        self.remote_deadlines.clear();
        if self.local_speaking {
            self.local_speaking = false;
            if !cleared.contains(&local_user_id) {
                cleared.push(local_user_id);
            }
        }
        cleared
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct VoiceTransportSession {
    ssrc: u32,
    ip: String,
    port: u16,
    modes: Vec<String>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct DiscoveredVoiceAddress {
    address: String,
    port: u16,
}

#[derive(Clone, Eq, PartialEq)]
struct VoiceSessionDescription {
    mode: String,
    secret_key: Vec<u8>,
    dave_protocol_version: Option<u64>,
}

impl fmt::Debug for VoiceSessionDescription {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("VoiceSessionDescription")
            .field("mode", &self.mode)
            .field("secret_key", &"<redacted>")
            .field("secret_key_len", &self.secret_key.len())
            .field("dave_protocol_version", &self.dave_protocol_version)
            .finish()
    }
}

#[allow(dead_code)]
struct VoiceFakeOutboundSendState {
    rtp: VoiceOutboundRtpState,
    encryptor: VoiceRtpEncryptor,
    nonce_suffix: u32,
    allow_microphone_transmit: bool,
    self_mute: bool,
    dave_active: bool,
    speaking: bool,
    logged_block_reason: Option<VoiceFakeSendBlockReason>,
    events: Vec<VoiceFakeOutboundEvent>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
#[allow(dead_code)]
enum VoiceFakeOutboundEvent {
    Speaking { speaking: bool, ssrc: u32 },
    Packet { bytes: Vec<u8> },
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[allow(dead_code)]
enum VoiceFakeSendOutcome {
    Noop,
    Sent,
    Blocked(VoiceFakeSendBlockReason),
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[allow(dead_code)]
#[allow(clippy::enum_variant_names)]
enum VoiceFakeSendBlockReason {
    DaveOutboundUnsupported,
    DaveOutboundMissingSession,
    DaveOutboundNotReady,
    DaveOutboundEncryptFailed,
}

#[derive(Default)]
struct VoiceSpeakingTracker {
    remote_deadlines: HashMap<Id<UserMarker>, Instant>,
    local_speaking: bool,
}

#[derive(Default)]
struct VoiceChildTasks {
    heartbeat: Option<JoinHandle<()>>,
    udp_receive: Option<JoinHandle<()>>,
    #[cfg(feature = "voice-playback")]
    udp_transmit: Option<JoinHandle<()>>,
    #[cfg(feature = "voice-playback")]
    transmit_gate: Option<watch::Sender<VoiceCaptureGate>>,
    #[cfg(feature = "voice-playback")]
    playback_enabled: Option<Arc<AtomicBool>>,
    #[cfg(feature = "voice-playback")]
    playback_volume: Option<Arc<AtomicU8>>,
    #[cfg(feature = "voice-playback")]
    microphone_pcm_tx: Option<SyncSender<Vec<i16>>>,
    opus_decode: Option<JoinHandle<()>>,
    #[cfg(feature = "voice-playback")]
    audio_output: Option<VoiceAudioOutput>,
    #[cfg(feature = "voice-playback")]
    microphone_capture: Option<VoiceMicrophoneCapture>,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct VoiceCaptureGate {
    enabled: bool,
    microphone_sensitivity: MicrophoneSensitivityDb,
    microphone_volume: VoiceVolumePercent,
}

#[cfg(feature = "voice-playback")]
struct VoiceUdpTransmitContext {
    udp_socket: Arc<UdpSocket>,
    writer: VoiceWriter,
    description: VoiceSessionDescription,
    ssrc: u32,
    dave_state: Arc<Mutex<VoiceDaveState>>,
    local_speaking_tx: mpsc::UnboundedSender<bool>,
}

#[cfg(feature = "voice-playback")]
struct VoiceMicrophoneCapture {
    _stream: cpal::Stream,
    stats: Arc<VoiceMicrophoneCaptureStats>,
}

#[cfg(feature = "voice-playback")]
struct VoiceMicrophonePcmFrames {
    frames_tx: SyncSender<Vec<i16>>,
    stats: Arc<VoiceMicrophoneCaptureStats>,
    source_sample_rate: u32,
    source_pending: Vec<i16>,
    output_pending: Vec<i16>,
    next_source_frame: f64,
}

#[cfg(feature = "voice-playback")]
struct VoiceMicrophoneCaptureStats {
    chunks: AtomicU64,
    frames: AtomicU64,
    min_callback_frames: AtomicU64,
    max_callback_frames: AtomicU64,
    queued_frames: AtomicU64,
    dropped_frames: AtomicU64,
    peak_sample: AtomicU64,
    clipped_samples: AtomicU64,
}

#[cfg(feature = "voice-playback")]
#[derive(Default)]
struct VoiceUdpTransmitStats {
    sent_packets: u64,
    stale_frames_drained: u64,
    empty_ticks_while_speaking: u64,
    overload_smoothed_frames: u64,
    limited_samples: u64,
    max_tick_gap_ms: u128,
    last_tick_at: Option<Instant>,
}

#[cfg(any(test, feature = "voice-playback"))]
#[allow(dead_code)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum VoiceMicrophoneOverloadKind {
    HandlingNoise,
    Transient,
    Attenuated,
    Recovery,
}

#[cfg(any(test, feature = "voice-playback"))]
#[derive(Clone, Copy, Debug)]
struct VoiceMicrophoneOverloadDecision {
    kind: VoiceMicrophoneOverloadKind,
    gain: f32,
}

#[cfg(feature = "voice-playback")]
#[derive(Default)]
struct VoiceMicrophoneGateState {
    hangover_frames: u8,
    overload_recovery_frames: u8,
    handling_noise_suppression_frames: u8,
}

#[cfg(feature = "voice-playback")]
#[derive(Debug, Eq, PartialEq)]
enum VoiceMicrophonePcmRead {
    Frame(Vec<i16>),
    Empty,
    Disconnected,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct VoiceBinaryFrame<'a> {
    sequence: i64,
    opcode: u8,
    payload: &'a [u8],
}

impl fmt::Debug for VoiceGatewaySession {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("VoiceGatewaySession")
            .field("guild_id", &self.guild_id)
            .field("channel_id", &self.channel_id)
            .field("user_id", &self.user_id)
            .field("session_id", &"<redacted>")
            .field("endpoint", &self.endpoint)
            .field("token", &"<redacted>")
            .finish()
    }
}

impl VoiceChildTasks {
    fn replace_heartbeat(&mut self, task: JoinHandle<()>) {
        if let Some(task) = self.heartbeat.take() {
            logging::debug("voice", "aborting previous voice heartbeat task");
            task.abort();
        }
        self.heartbeat = Some(task);
    }

    fn replace_udp_receive(&mut self, task: JoinHandle<()>) {
        if let Some(task) = self.udp_receive.take() {
            logging::debug("voice", "aborting previous voice UDP receive task");
            task.abort();
        }
        self.udp_receive = Some(task);
    }

    #[cfg(feature = "voice-playback")]
    fn replace_udp_transmit(
        &mut self,
        task: JoinHandle<()>,
        gate: watch::Sender<VoiceCaptureGate>,
        microphone_pcm_tx: SyncSender<Vec<i16>>,
    ) {
        if let Some(task) = self.udp_transmit.take() {
            logging::debug("voice", "stopping previous voice UDP transmit task");
            self.signal_udp_transmit_stop();
            drop(task);
        }
        self.udp_transmit = Some(task);
        self.transmit_gate = Some(gate);
        self.microphone_pcm_tx = Some(microphone_pcm_tx);
    }

    #[cfg(feature = "voice-playback")]
    fn signal_udp_transmit_stop(&mut self) {
        if let Some(gate) = self.transmit_gate.as_ref() {
            let _ = gate.send(VoiceCaptureGate {
                enabled: false,
                microphone_sensitivity: MicrophoneSensitivityDb::default(),
                microphone_volume: VoiceVolumePercent::default(),
            });
        }
        self.microphone_capture = None;
        self.microphone_pcm_tx = None;
        self.transmit_gate = None;
    }

    fn replace_opus_decode(&mut self, opus_decode: VoiceOpusDecode) {
        if let Some(task) = self.opus_decode.take() {
            logging::debug("voice", "aborting previous voice Opus decode task");
            task.abort();
        }
        #[cfg(feature = "voice-playback")]
        {
            self.audio_output = opus_decode.audio_output;
            self.playback_enabled = Some(opus_decode.playback_enabled);
            self.playback_volume = Some(opus_decode.playback_volume);
        }
        self.opus_decode = Some(opus_decode.task);
    }

    fn abort_all(&mut self) {
        if let Some(task) = self.heartbeat.take() {
            logging::debug("voice", "aborting voice heartbeat task");
            task.abort();
        }
        if let Some(task) = self.udp_receive.take() {
            logging::debug("voice", "aborting voice UDP receive task");
            task.abort();
        }
        #[cfg(feature = "voice-playback")]
        if let Some(task) = self.udp_transmit.take() {
            logging::debug("voice", "stopping voice UDP transmit task");
            self.signal_udp_transmit_stop();
            drop(task);
        }
        if let Some(task) = self.opus_decode.take() {
            logging::debug("voice", "aborting voice Opus decode task");
            task.abort();
        }
        #[cfg(feature = "voice-playback")]
        {
            self.audio_output = None;
            self.playback_enabled = None;
            self.microphone_capture = None;
        }
    }

    #[allow(dead_code)]
    fn set_microphone_capture_enabled(&mut self, enabled: bool) {
        #[cfg(feature = "voice-playback")]
        {
            match (enabled, self.microphone_capture.is_some()) {
                (true, false) => {
                    match VoiceMicrophoneCapture::start(self.microphone_pcm_tx.clone()) {
                        Ok(capture) => self.microphone_capture = Some(capture),
                        Err(error) => logging::error(
                            "voice",
                            format!("voice microphone capture unavailable: {error}"),
                        ),
                    }
                }
                (false, true) => {
                    logging::debug("voice", "stopping voice microphone capture");
                    self.microphone_capture = None;
                }
                _ => {}
            }
        }
        #[cfg(not(feature = "voice-playback"))]
        {
            let _ = enabled;
        }
    }

    fn set_voice_transmit_gate(&mut self, capture_gate: VoiceCaptureGate) {
        #[cfg(feature = "voice-playback")]
        {
            if let Some(gate) = self.transmit_gate.as_ref() {
                let _ = gate.send(capture_gate);
            }
            self.set_microphone_capture_enabled(
                capture_gate.enabled && self.microphone_pcm_tx.is_some(),
            );
        }
        #[cfg(not(feature = "voice-playback"))]
        {
            let _ = capture_gate;
        }
    }

    fn set_voice_playback_gate(&mut self, playback_gate: VoicePlaybackGate) {
        #[cfg(feature = "voice-playback")]
        {
            if let Some(playback_enabled) = self.playback_enabled.as_ref() {
                playback_enabled.store(playback_gate.enabled, Ordering::Relaxed);
            }
            if let Some(playback_volume) = self.playback_volume.as_ref() {
                playback_volume.store(playback_gate.volume.value(), Ordering::Relaxed);
            }
        }
        #[cfg(not(feature = "voice-playback"))]
        {
            let _ = playback_gate;
        }
    }
}

impl Drop for VoiceChildTasks {
    fn drop(&mut self) {
        self.abort_all();
    }
}

#[cfg(test)]
mod tests;