cacophony 0.3.0

A high-performance Discord voice runtime with Opus, UDP/RTP transport, and DAVE support.
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
use std::{
    fmt,
    net::{AddrParseError, SocketAddr},
    str::Utf8Error,
    time::Duration,
};

use dave::{
    Codec, CreateKeyPackageError, DecryptError, EncryptError, FrameDecryptError, InitError,
    ProcessCommitError, ProcessProposalsError, ProcessWelcomeError, SetExternalSenderError,
    UpdateRatchetsError,
};
use thiserror::Error as ThisError;

use crate::{
    observer::{ConnectStage, ReceiveDecodeErrorKind},
    rtp::RtpPayloadType,
    state::{EncryptionMode, OfferedEncryptionMode},
};

#[derive(Debug)]
pub enum Error {
    InvalidInput(InvalidInputError),
    Pcm(PcmError),
    Protocol(ProtocolError),
    Timeout {
        stage: Option<ConnectStage>,
        duration: Duration,
    },
    Io(std::io::Error),
    Json(serde_json::Error),
    WebSocket(tokio_tungstenite::tungstenite::Error),
    Opus(OpusError),
    UnsupportedCodec(UnsupportedCodecError),
    Dave(DaveError),
    Rtp(RtpError),
    TransportCrypto(TransportCryptoError),
    PayloadTooLarge {
        kind: PayloadKind,
        len: usize,
        max_len: usize,
    },
    Backpressure(BackpressureError),
    Closed,
    Join(ConnectionJoinError),
}

impl Error {
    pub fn receive_decode_kind(&self) -> Option<ReceiveDecodeErrorKind> {
        match self {
            Self::Rtp(_) => Some(ReceiveDecodeErrorKind::MalformedRtp),
            Self::UnsupportedCodec(_) => Some(ReceiveDecodeErrorKind::UnsupportedCodec),
            Self::TransportCrypto(_) => Some(ReceiveDecodeErrorKind::TransportDecryptFailed),
            Self::Dave(DaveError::Decrypt(error)) => Some(error.receive_decode_kind()),
            Self::Opus(_) => Some(ReceiveDecodeErrorKind::OpusDecodeFailed),
            _ => None,
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidInput(error) => write!(f, "{error}"),
            Self::Pcm(error) => write!(f, "{error}"),
            Self::Protocol(error) => write!(f, "{error}"),
            Self::Timeout { stage, duration } => {
                if let Some(stage) = stage {
                    write!(f, "voice {} timed out after {duration:?}", stage.label())
                } else {
                    write!(f, "voice operation timed out after {duration:?}")
                }
            }
            Self::Io(error) => write!(f, "{error}"),
            Self::Json(error) => write!(f, "{error}"),
            Self::WebSocket(error) => write!(f, "{error}"),
            Self::Opus(error) => write!(f, "{error}"),
            Self::UnsupportedCodec(error) => write!(f, "{error}"),
            Self::Dave(error) => write!(f, "{error}"),
            Self::Rtp(error) => write!(f, "{error}"),
            Self::TransportCrypto(error) => write!(f, "{error}"),
            Self::PayloadTooLarge { kind, len, max_len } => {
                write!(
                    f,
                    "voice {kind} is {len} bytes, exceeding requested max_len {max_len}"
                )
            }
            Self::Backpressure(error) => write!(f, "{error}"),
            Self::Closed => f.write_str("voice connection is closed"),
            Self::Join(error) => write!(f, "{error}"),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Pcm(error) => Some(error),
            Self::Protocol(error) => error.source(),
            Self::Io(error) => Some(error),
            Self::Json(error) => Some(error),
            Self::WebSocket(error) => Some(error),
            Self::UnsupportedCodec(error) => Some(error),
            Self::Dave(error) => Some(error),
            Self::Rtp(error) => Some(error),
            Self::TransportCrypto(error) => Some(error),
            Self::Join(error) => error.source(),
            _ => None,
        }
    }
}

impl From<std::io::Error> for Error {
    fn from(error: std::io::Error) -> Self {
        Self::Io(error)
    }
}

impl From<serde_json::Error> for Error {
    fn from(error: serde_json::Error) -> Self {
        Self::Json(error)
    }
}

impl From<tokio_tungstenite::tungstenite::Error> for Error {
    fn from(error: tokio_tungstenite::tungstenite::Error) -> Self {
        Self::WebSocket(error)
    }
}

impl From<DaveError> for Error {
    fn from(error: DaveError) -> Self {
        Self::Dave(error)
    }
}

impl From<UnsupportedCodecError> for Error {
    fn from(error: UnsupportedCodecError) -> Self {
        Self::UnsupportedCodec(error)
    }
}

impl From<PcmError> for Error {
    fn from(error: PcmError) -> Self {
        Self::Pcm(error)
    }
}

impl From<RtpError> for Error {
    fn from(error: RtpError) -> Self {
        Self::Rtp(error)
    }
}

impl From<TransportCryptoError> for Error {
    fn from(error: TransportCryptoError) -> Self {
        Self::TransportCrypto(error)
    }
}

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Clone, Debug, PartialEq, Eq, ThisError)]
pub enum PcmError {
    #[error("PCM sample rate must be greater than zero")]
    SampleRateZero,
    #[error("PCM channel count must be greater than zero")]
    ChannelCountZero,
    #[error("{encoding} PCM byte length {byte_len} is not aligned to {sample_bytes}-byte samples")]
    SampleAlignment {
        encoding: &'static str,
        byte_len: usize,
        sample_bytes: usize,
    },
    #[error("PCM sample count {samples} is not aligned to {channels} channels")]
    ChannelAlignment { channels: usize, samples: usize },
    #[error("PCM encoding `{0}` is not supported")]
    UnsupportedEncoding(String),
    #[error("PCM resampler chunk frame count must be greater than zero")]
    ResamplerChunkFramesZero,
    #[error("PCM resampler is not initialized")]
    ResamplerNotInitialized,
    #[error("PCM resampler error: {0}")]
    Resampler(String),
}

#[derive(Debug, ThisError)]
pub enum InvalidInputError {
    #[error("connection tuning field {field} must be greater than zero")]
    ConnectionTuningZero { field: &'static str },
    #[error("connection tuning field {field} must be at least {min}, got {actual}")]
    ConnectionTuningTooSmall {
        field: &'static str,
        min: usize,
        actual: usize,
    },
    #[error("connection tuning duration {field} must be nonzero")]
    ConnectionTuningDurationZero { field: &'static str },
    #[error("Discord voice gateway version {version} is unsupported")]
    UnsupportedGatewayVersion { version: u8 },
    #[error("{} cannot be configured as a Discord video codec", codec.as_str())]
    VideoCodecPreferenceNotVideo { codec: Codec },
    #[error("{} appears more than once in Discord video codec preferences", codec.as_str())]
    DuplicateVideoCodecPreference { codec: Codec },
    #[error("max_len must be greater than zero")]
    ZeroMaxLen,
    #[error("{} frame must not be empty", codec.as_str())]
    EmptyPayload { codec: Codec },
    #[error("Discord PCM block must contain {expected} interleaved f32 samples, got {actual}")]
    PcmBlockSampleCount { expected: usize, actual: usize },
    #[error("Discord PCM block must contain {expected} samples per channel, got {actual}")]
    PcmBlockFrameCount { expected: usize, actual: usize },
    #[error("captured PCM audio must not be empty")]
    PcmArchiveEmpty,
    #[error("Ogg Opus vendor must not be empty")]
    OggOpusVendorEmpty,
    #[error("Ogg Opus archive encoding does not support {sample_rate_hz} Hz captured audio")]
    OggOpusUnsupportedSampleRate { sample_rate_hz: u32 },
    #[error("Discord PCM playback received mixed sample rates: {existing} and {actual}")]
    DiscordPcmMixedSampleRates { existing: u32, actual: u32 },
    #[error("Discord PCM playback received mixed channel counts: {existing} and {actual}")]
    DiscordPcmMixedChannelCounts { existing: usize, actual: usize },
    #[error("Discord PCM playback received mixed PCM encodings")]
    DiscordPcmMixedEncoding,
    #[error("Discord PCM encoder was not initialized")]
    DiscordPcmEncoderUninitialized,
    #[error("Opus bitrate {bitrate_bps} bps exceeds the encoder limit")]
    OpusBitrateTooLarge { bitrate_bps: u32 },
    #[error("Opus output buffer must be at least {min_len} bytes, got {len}")]
    OpusOutputBufferTooSmall { min_len: usize, len: usize },
}

#[derive(Debug, ThisError)]
pub enum ProtocolError {
    #[error("DAVE MLS key package and commit/welcome use binary websocket frames")]
    TextPayloadRequiresBinaryDaveMlsCommand,
    #[error("voice discovery packet must be at least {min_len} bytes, got {len}")]
    UdpDiscoveryPacketTooShort { len: usize, min_len: usize },
    #[error(
        "unexpected voice discovery packet type {packet_type}; expected {expected_packet_type}"
    )]
    UnexpectedUdpDiscoveryPacketType {
        packet_type: u16,
        expected_packet_type: u16,
    },
    #[error(
        "unexpected voice discovery packet length {packet_len}; expected {expected_packet_len}"
    )]
    UnexpectedUdpDiscoveryPacketLen {
        packet_len: u16,
        expected_packet_len: u16,
    },
    #[error("invalid voice discovery ip: {0}")]
    InvalidUdpDiscoveryIp(#[source] Utf8Error),
    #[error("voice ready payload did not include encryption modes")]
    ReadyMissingEncryptionModes,
    #[error("required voice encryption mode {required_mode} was not offered: {modes:?}")]
    RequiredEncryptionModeUnavailable {
        required_mode: EncryptionMode,
        modes: Vec<OfferedEncryptionMode>,
    },
    #[error("voice ready payload did not include a supported encryption mode: {modes:?}")]
    ReadyMissingSupportedEncryptionMode { modes: Vec<OfferedEncryptionMode> },
    #[error("resolve voice websocket endpoint {host}:{port}: {source}")]
    ResolveWebSocketEndpoint {
        host: String,
        port: u16,
        #[source]
        source: std::io::Error,
    },
    #[error("voice websocket endpoint {host}:{port} did not resolve to any addresses")]
    WebSocketEndpointNoAddresses { host: String, port: u16 },
    #[error("voice websocket connect to {address} timed out after {duration:?}")]
    WebSocketAddressConnectTimeout {
        address: SocketAddr,
        duration: Duration,
    },
    #[error(
        "voice websocket connect to {host}:{port} failed across {address_count} resolved addresses: {}",
        errors.join("; ")
    )]
    WebSocketAllAddressesFailed {
        host: String,
        port: u16,
        address_count: usize,
        errors: Vec<String>,
    },
    #[error("tcp connect {address}: {source}")]
    TcpConnect {
        address: SocketAddr,
        #[source]
        source: std::io::Error,
    },
    #[error("voice websocket URL did not include a host")]
    WebSocketUrlMissingHost,
    #[error("voice websocket URL did not include a usable scheme")]
    WebSocketUrlMissingUsableScheme,
    #[error("invalid Discord voice UDP IP {remote_ip:?}: {source}")]
    InvalidDiscordVoiceUdpIp {
        remote_ip: String,
        #[source]
        source: AddrParseError,
    },
    #[error("voice heartbeat ACK timed out")]
    HeartbeatAckTimeout,
    #[error("missing voice session description")]
    MissingSessionDescription,
    #[error("Discord voice session did not negotiate {} transmission; negotiated {negotiated_codec:?}", codec.as_str())]
    MediaCodecNotNegotiated {
        codec: Codec,
        negotiated_codec: Option<Codec>,
    },
    #[error("Discord voice ready payload did not include a video SSRC for {}", codec.as_str())]
    MissingVideoSsrc { codec: Codec },
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OpusOperation {
    CreateEncoder,
    EncodeFrame,
    CreateMonoDecoder,
    CreateDecoder,
    DecodeFrame,
}

impl fmt::Display for OpusOperation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::CreateEncoder => f.write_str("create Opus encoder"),
            Self::EncodeFrame => f.write_str("encode Opus frame"),
            Self::CreateMonoDecoder => f.write_str("create mono Opus decoder"),
            Self::CreateDecoder => f.write_str("create Opus decoder"),
            Self::DecodeFrame => f.write_str("decode Opus frame"),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq, ThisError)]
pub enum OpusError {
    #[error("failed to {operation}: {reason}")]
    OperationFailed {
        operation: OpusOperation,
        reason: &'static str,
    },
    #[error("unsupported voice codec {}", codec.as_str())]
    UnsupportedVoiceCodec { codec: Codec },
    #[error("unsupported Opus channel count {channels}")]
    UnsupportedChannelCount { channels: usize },
    #[error("invalid Opus packet: {reason}")]
    InvalidPacket { reason: &'static str },
    #[error(
        "Discord Opus packet must contain {expected_samples_per_channel} samples per channel, got {actual_samples_per_channel}"
    )]
    UnsupportedDiscordPacketDuration {
        expected_samples_per_channel: usize,
        actual_samples_per_channel: usize,
    },
    #[error("Opus resampler is not initialized")]
    ResamplerNotInitialized,
    #[error("Opus resampler error: {0}")]
    Resampler(String),
    #[error("Opus frame is empty")]
    EmptyFrame,
    #[error("Ogg Opus asset is empty")]
    OggOpusEmpty,
    #[error("failed to read Ogg Opus packet: {0}")]
    OggOpusRead(String),
    #[error("Ogg Opus asset contains no packets")]
    OggOpusNoPackets,
    #[error("first Ogg Opus packet is not marked as the start of stream")]
    OggOpusHeaderNotStart,
    #[error("Ogg stream is not Opus")]
    OggOpusMissingHead,
    #[error("unsupported OpusHead version {version}")]
    OggOpusUnsupportedVersion { version: u8 },
    #[error("Ogg Opus channel count must be 1 or 2, got {channels}")]
    OggOpusUnsupportedChannelCount { channels: u8 },
    #[error("Ogg Opus mapping family must be 0, got {mapping_family}")]
    OggOpusUnsupportedMappingFamily { mapping_family: u8 },
    #[error("Ogg Opus asset is missing OpusTags packet")]
    OggOpusMissingTags,
    #[error("Ogg Opus asset contains multiple logical streams")]
    OggOpusMultipleLogicalStreams,
    #[error("Ogg Opus asset contains no audio packets")]
    OggOpusNoAudioPackets,
    #[error("Ogg Opus asset contains no audio after pre-skip")]
    OggOpusNoAudioAfterPreSkip,
    #[error("Ogg Opus asset decoded to empty audio")]
    OggOpusDecodedEmpty,
    #[error("Ogg Opus output sample rate {sample_rate_hz} exceeds the decoder limit")]
    OggOpusOutputSampleRateTooLarge { sample_rate_hz: u32 },
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, ThisError)]
pub enum BackpressureError {
    #[error("voice connection command queue is full")]
    CommandQueueFull,
    #[error("voice connection media queue is full")]
    MediaQueueFull,
    #[error("voice connection already has an active Opus playout")]
    ActiveOpusPlayout,
    #[error("voice connection already has an active media frame stream")]
    ActiveFrameStream,
}

#[derive(Debug, ThisError)]
pub enum ConnectionJoinError {
    #[error("voice control task join failed: {0}")]
    ControlTaskJoinFailed(#[source] tokio::task::JoinError),
    #[error("voice join task is closed")]
    JoinTaskClosed,
    #[error("voice join task stopped before replying")]
    JoinTaskStoppedBeforeReply,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PayloadKind {
    RawUdpPacket,
    RtpPacket,
    Frame,
}

#[derive(Clone, Debug, PartialEq, Eq, ThisError)]
pub enum UnsupportedCodecError {
    #[error("unsupported Discord voice audio codec {codec:?}; only Opus is supported")]
    UnsupportedAudioCodec { codec: String },
    #[error("unsupported Discord voice video codec {codec:?}")]
    UnsupportedVideoCodec { codec: String },
    #[error(
        "unsupported Discord voice RTP payload type {payload_type}; expected one of {expected_payload_types:?}"
    )]
    UnsupportedRtpPayloadType {
        payload_type: RtpPayloadType,
        expected_payload_types: Vec<RtpPayloadType>,
    },
    #[error(
        "Discord voice RTP payload type {payload_type} is {}; negotiated payload types are {expected_payload_types:?}",
        codec.as_str()
    )]
    UnexpectedRtpPayloadCodec {
        payload_type: RtpPayloadType,
        codec: Codec,
        expected_payload_types: Vec<RtpPayloadType>,
    },
}

impl fmt::Display for PayloadKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::RawUdpPacket => f.write_str("raw UDP packet"),
            Self::RtpPacket => f.write_str("RTP packet"),
            Self::Frame => f.write_str("voice frame"),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq, ThisError)]
pub enum RtpError {
    #[error("voice RTP packet is shorter than 12 bytes: {len}")]
    PacketTooShort { len: usize },
    #[error("unsupported voice RTP version {version}; expected 2")]
    UnsupportedVersion { version: u8 },
    #[error(
        "voice RTP packet has truncated CSRC list: {len} bytes for {expected_header_len} byte header"
    )]
    TruncatedCsrcList {
        len: usize,
        expected_header_len: usize,
    },
    #[error(
        "voice RTP packet has truncated extension header: {len} bytes for {expected_header_len} byte header"
    )]
    TruncatedExtensionHeader {
        len: usize,
        expected_header_len: usize,
    },
    #[error(
        "voice RTP packet has truncated extension payload: {len} bytes for {expected_header_len} byte header"
    )]
    TruncatedExtensionPayload {
        len: usize,
        expected_header_len: usize,
    },
    #[error("voice RTP packet has truncated encrypted extension")]
    TruncatedEncryptedExtension,
    #[error("voice RTP packet has empty padded payload")]
    EmptyPaddedPayload,
    #[error(
        "voice RTP packet has invalid padding length {padding} for payload length {payload_len}"
    )]
    InvalidPadding { padding: usize, payload_len: usize },
    #[error(
        "{} RTP payload is {payload_len} bytes, exceeding the per-packet media payload limit {max_payload_len}",
        codec.as_str()
    )]
    PayloadTooLarge {
        codec: Codec,
        payload_len: usize,
        max_payload_len: usize,
    },
    #[error("malformed {} RTP payload: {reason}", codec.as_str())]
    MalformedPayload { codec: Codec, reason: &'static str },
}

#[derive(Clone, Debug, PartialEq, Eq, ThisError)]
pub enum TransportCryptoError {
    #[error("voice secret_key must be 32 bytes, got {len}")]
    InvalidSecretKeyLen { len: usize },
    #[error(
        "voice RTP packet is missing the RTP-size nonce suffix: {packet_len} bytes for minimum {min_len}"
    )]
    MissingRtpSizeNonce { packet_len: usize, min_len: usize },
    #[error("failed to initialize voice nonce from OS randomness")]
    NonceRandomnessUnavailable,
    #[error("invalid AES-GCM voice secret key")]
    InvalidAesGcmKey,
    #[error("invalid XChaCha20-Poly1305 voice secret key")]
    InvalidXChaCha20Poly1305Key,
    #[error("failed to decrypt AES-GCM voice packet")]
    AesGcmDecryptFailed,
    #[error("failed to encrypt AES-GCM voice packet")]
    AesGcmEncryptFailed,
    #[error("failed to decrypt XChaCha20-Poly1305 voice packet")]
    XChaCha20Poly1305DecryptFailed,
    #[error("failed to encrypt XChaCha20-Poly1305 voice packet")]
    XChaCha20Poly1305EncryptFailed,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TransportCryptoDirection {
    Session,
    Send,
    Receive,
}

impl fmt::Display for TransportCryptoDirection {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Session => f.write_str("session"),
            Self::Send => f.write_str("send"),
            Self::Receive => f.write_str("receive"),
        }
    }
}

#[derive(Debug, ThisError)]
pub enum DaveError {
    #[error("unsupported DAVE protocol version {version}")]
    InvalidProtocolVersion { version: u16 },
    #[error("failed to create DAVE session: {0}")]
    CreateSession(#[source] InitError),
    #[error("failed to set DAVE external sender: {0}")]
    SetExternalSender(#[source] SetExternalSenderError),
    #[error("failed to create DAVE key package: {0}")]
    CreateKeyPackage(#[source] CreateKeyPackageError),
    #[error("failed to process DAVE proposals: {0}")]
    ProcessProposals(#[source] ProcessProposalsError),
    #[error("failed to process DAVE welcome: {0}")]
    ProcessWelcome(#[source] ProcessWelcomeError),
    #[error("failed to process DAVE commit: {0}")]
    ProcessCommit(#[source] ProcessCommitError),
    #[error("failed to update DAVE media ratchets: {0}")]
    UpdateRatchets(#[source] UpdateRatchetsError),
    #[error(
        "failed to recover invalid DAVE group after {operation} error ({original}): {recovery}"
    )]
    RecoverInvalidGroup {
        operation: &'static str,
        #[source]
        original: Box<DaveError>,
        recovery: Box<Error>,
    },
    #[error("invalid DAVE gateway payload: {0}")]
    InvalidGatewayPayload(#[source] DaveGatewayPayloadError),
    #[error("invalid DAVE proposals payload: {0}")]
    InvalidProposalsPayload(#[source] DaveProposalsPayloadError),
    #[error("{0}")]
    Encrypt(#[source] EncryptError),
    #[error("{0}")]
    Decrypt(#[source] DaveDecryptError),
}

impl DaveError {
    pub(crate) fn recover_invalid_group(
        operation: &'static str,
        original: Self,
        recovery: Error,
    ) -> Error {
        match recovery {
            Error::Dave(_) => Self::RecoverInvalidGroup {
                operation,
                original: Box::new(original),
                recovery: Box::new(recovery),
            }
            .into(),
            _ => recovery,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq, ThisError)]
pub enum DaveGatewayPayloadError {
    #[error("opcode {opcode} payload is {len} bytes, expected at least {min_len}")]
    PayloadTooShort {
        opcode: u8,
        len: usize,
        min_len: usize,
    },
}

#[derive(Clone, Debug, PartialEq, Eq, ThisError)]
pub enum DaveProposalsPayloadError {
    #[error("missing proposals operation byte")]
    MissingOperation,
    #[error("invalid proposals operation byte {operation}")]
    InvalidOperation { operation: u8 },
}

#[derive(Clone, Debug, PartialEq, Eq, ThisError)]
pub enum DaveDecryptError {
    #[error("DAVE frame decrypt requires mapped user_id")]
    MissingUser,
    #[error("{0}")]
    Source(#[source] DecryptError),
}

impl DaveDecryptError {
    pub(crate) fn receive_decode_kind(&self) -> ReceiveDecodeErrorKind {
        match self {
            Self::MissingUser => ReceiveDecodeErrorKind::MissingDaveUser,
            Self::Source(DecryptError::NoDecryptorForUser { .. }) => {
                ReceiveDecodeErrorKind::DaveNoDecryptorForUser
            }
            Self::Source(DecryptError::Frame(FrameDecryptError::NoValidCryptor { .. })) => {
                ReceiveDecodeErrorKind::DaveNoValidCryptor
            }
            Self::Source(DecryptError::Frame(FrameDecryptError::PassthroughDisabled)) => {
                ReceiveDecodeErrorKind::DaveUnencryptedWhenPassthroughDisabled
            }
            Self::Source(DecryptError::Frame(
                FrameDecryptError::MalformedFrame
                | FrameDecryptError::ReplayedNonce
                | FrameDecryptError::MissingCryptor { .. }
                | FrameDecryptError::Aead { .. }
                | FrameDecryptError::InvalidKey,
            )) => ReceiveDecodeErrorKind::DaveOtherDecryptError,
        }
    }

    pub(crate) fn is_no_valid_cryptor(&self) -> bool {
        matches!(
            self,
            Self::Source(DecryptError::Frame(
                FrameDecryptError::NoValidCryptor { .. }
            ))
        )
    }
}

impl From<DecryptError> for DaveDecryptError {
    fn from(error: DecryptError) -> Self {
        Self::Source(error)
    }
}