rvoip-core-traits 0.3.5

Pure-data trait + type surface for the rvoip ecosystem. Holds ID newtypes and Identity data types so consumer crates (auth-core, vcon, harness, adapters) can depend on the type surface without pulling in rvoip-core's implementation. Carved out to break the rvoip-core → rvoip-vcon → rvoip-auth-core → rvoip-core dependency cycle (GAP_PLAN V2.A).
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
use std::collections::BTreeMap;
use std::fmt;

use serde::{Deserialize, Serialize};

use crate::identity::IdentityAssurance;

// =====================================================================
// Codec types
// =====================================================================

/// Legacy flat-fields codec entry — used internally by SIP/RTP adapters
/// that need the parsed `clock_rate_hz` / `channels` numbers directly.
/// Bridges to/from [`Codec`] (the spec wire shape) via `From`/`TryFrom`.
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct CodecInfo {
    pub name: String,
    pub clock_rate_hz: u32,
    pub channels: u8,
    pub fmtp: Option<String>,
}

impl fmt::Debug for CodecInfo {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("CodecInfo")
            .field("name_present", &!self.name.is_empty())
            .field("name_bytes", &self.name.len())
            .field("clock_rate_hz", &self.clock_rate_hz)
            .field("channels", &self.channels)
            .field("fmtp_present", &self.fmtp.is_some())
            .field(
                "fmtp_bytes",
                &self.fmtp.as_ref().map_or(0, std::string::String::len),
            )
            .finish()
    }
}

/// Reasonable default for adapter and orchestrator paths that need a
/// codec descriptor before negotiation has run (e.g. `Orchestrator::fanout_frame`
/// allocating a subscriber-side MediaStream before the publisher's
/// negotiated codec has propagated). Matches the codec the v0 default
/// CapabilityDescriptor advertises first.
pub fn default_audio_codec() -> CodecInfo {
    CodecInfo {
        name: "opus".into(),
        clock_rate_hz: 48_000,
        channels: 1,
        fmtp: None,
    }
}

impl CodecInfo {
    /// Build a `CodecInfo` from just the codec name, using
    /// standards-defined defaults for `clock_rate_hz` / `channels`.
    /// Used by the multi-party fanout path (plan B1) where the wire
    /// catalog only records the chosen codec name; richer params would
    /// require carrying the full negotiation result through more layers.
    /// Falls back to the `name`/48k/mono shape for codecs not in the
    /// table — fanout still works, the client just sees an audio stream
    /// it may or may not be able to decode (B2 codec-mismatch refusal
    /// is the right place to surface that).
    pub fn from_name_with_defaults(name: &str) -> Self {
        let (clock_rate_hz, channels) = match name {
            "opus" => (48_000, 1),
            "g.711-mu" | "PCMU" | "pcmu" => (8_000, 1),
            "g.711-a" | "PCMA" | "pcma" => (8_000, 1),
            "g.722" => (16_000, 1),
            "g.729" => (8_000, 1),
            "pcm_s16le" | "PCM_S16LE" => (16_000, 1),
            _ => (48_000, 1),
        };
        Self {
            name: name.to_string(),
            clock_rate_hz,
            channels,
            fmtp: None,
        }
    }
}

/// One codec entry on the wire, matching CONVERSATION_PROTOCOL.md §8's
/// `{"name": "opus", "params": {"sample_rate": 48000, ...}}` shape.
/// Distinct from [`CodecInfo`] — the flat-fields shape can't represent
/// the spec wire format losslessly. Conversion helpers below.
#[derive(Clone, Serialize, Deserialize)]
pub struct Codec {
    pub name: String,
    #[serde(default)]
    pub params: BTreeMap<String, serde_json::Value>,
}

impl fmt::Debug for Codec {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("Codec")
            .field("name_present", &!self.name.is_empty())
            .field("name_bytes", &self.name.len())
            .field("parameter_count", &self.params.len())
            .finish()
    }
}

impl Codec {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            params: BTreeMap::new(),
        }
    }
}

impl From<CodecInfo> for Codec {
    fn from(c: CodecInfo) -> Self {
        let mut params = BTreeMap::new();
        params.insert("sample_rate".into(), serde_json::json!(c.clock_rate_hz));
        params.insert("channels".into(), serde_json::json!(c.channels));
        if let Some(fmtp) = c.fmtp {
            params.insert("fmtp".into(), serde_json::Value::String(fmtp));
        }
        Self {
            name: c.name,
            params,
        }
    }
}

impl TryFrom<Codec> for CodecInfo {
    type Error = &'static str;
    fn try_from(c: Codec) -> Result<Self, Self::Error> {
        let clock_rate_hz = c
            .params
            .get("sample_rate")
            .and_then(|v| v.as_u64())
            .ok_or("missing or invalid sample_rate")? as u32;
        let channels = c
            .params
            .get("channels")
            .and_then(|v| v.as_u64())
            .unwrap_or(1) as u8;
        let fmtp = c
            .params
            .get("fmtp")
            .and_then(|v| v.as_str())
            .map(String::from);
        Ok(Self {
            name: c.name,
            clock_rate_hz,
            channels,
            fmtp,
        })
    }
}

// =====================================================================
// CapabilityDescriptor (expanded per CONVERSATION_PROTOCOL.md §8 +
// INTERFACE_DESIGN.md §9)
// =====================================================================

/// Capability descriptor that round-trips through CONVERSATION_PROTOCOL.md
/// §8's JSON shape. Field order matches the spec for readability.
///
/// `supports_dtmf_rfc4733` is a **method** (derived from `dtmf_modes`),
/// not a field — `dtmf_modes` is the single source of truth on the wire
/// and the boolean would silently desync from a custom serde round-trip.
#[derive(Clone, Default, Serialize, Deserialize)]
pub struct CapabilityDescriptor {
    #[serde(default)]
    pub audio_codecs: Vec<CodecInfo>,

    #[serde(default)]
    pub video_codecs: Vec<CodecInfo>,

    #[serde(default)]
    pub data_protocols: Vec<DataProtocol>,

    #[serde(default)]
    pub dtmf_modes: Vec<DtmfMode>,

    #[serde(default)]
    pub max_streams_per_connection: u16,

    #[serde(default)]
    pub transport_features: Vec<TransportFeature>,

    /// Gatewayable interop targets (`["sip", "webrtc"]`). Empty when the
    /// endpoint is UCTP-only.
    #[serde(default)]
    pub interop: Vec<InteropTarget>,

    /// IdentityAssurance the peer is offering. Defaults to
    /// `Anonymous` when not declared.
    #[serde(default = "default_assurance_offered")]
    pub identity_assurance_offered: AssuranceLevel,

    /// Minimum IdentityAssurance the peer requires from its counterpart.
    /// `None` means no constraint.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub identity_assurance_required: Option<IdentityAssuranceRequirement>,

    /// Legacy boolean retained from the original narrow `CapabilityDescriptor`
    /// for back-compat with consumers that check messaging support
    /// directly. Independent of `dtmf_modes` / `data_protocols`.
    #[serde(default)]
    pub supports_message_text: bool,

    /// Legacy boolean retained from the original narrow `CapabilityDescriptor`.
    /// Independent of `transport_features`.
    #[serde(default)]
    pub supports_srtp: bool,
}

impl fmt::Debug for CapabilityDescriptor {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("CapabilityDescriptor")
            .field("audio_codec_count", &self.audio_codecs.len())
            .field("video_codec_count", &self.video_codecs.len())
            .field("data_protocols", &self.data_protocols)
            .field("dtmf_modes", &self.dtmf_modes)
            .field(
                "max_streams_per_connection",
                &self.max_streams_per_connection,
            )
            .field("transport_features", &self.transport_features)
            .field("interop", &self.interop)
            .field(
                "identity_assurance_offered",
                &self.identity_assurance_offered,
            )
            .field(
                "identity_assurance_required",
                &self.identity_assurance_required,
            )
            .field("supports_message_text", &self.supports_message_text)
            .field("supports_srtp", &self.supports_srtp)
            .finish()
    }
}

fn default_assurance_offered() -> AssuranceLevel {
    AssuranceLevel::Anonymous
}

impl CapabilityDescriptor {
    /// True when `dtmf_modes` includes `Rfc4733`. Defined as a method
    /// (not a field) so `dtmf_modes` is the single source of truth.
    pub fn supports_dtmf_rfc4733(&self) -> bool {
        self.dtmf_modes.contains(&DtmfMode::Rfc4733)
    }
}

// =====================================================================
// Capability catalog enums
// =====================================================================

/// `data_protocols` catalog per CONVERSATION_PROTOCOL.md §8.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DataProtocol {
    Text,
    Json,
    Binary,
}

/// `dtmf_modes` catalog per CONVERSATION_PROTOCOL.md §8.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum DtmfMode {
    #[serde(rename = "rfc4733")]
    Rfc4733,
    #[serde(rename = "info")]
    Info,
}

/// `transport_features` catalog per CONVERSATION_PROTOCOL.md §8.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum TransportFeature {
    MediaDatagrams,
    ConnectionMigration,
    SessionResumption,
    #[serde(rename = "0rtt")]
    ZeroRtt,
    #[serde(rename = "transcode-g711-opus")]
    TranscodeG711Opus,
    /// Catch-all for future entries so the wire format stays forward-compat.
    #[serde(other)]
    Unknown,
}

/// `identity_assurance_required` levels per CONVERSATION_PROTOCOL.md §5.6 / §8.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum IdentityAssuranceRequirement {
    None,
    Pseudonymous,
    Identified,
    TaskScoped,
    UserAuthorized,
}

/// Substrate name as it appears on the UCTP wire (CONVERSATION_PROTOCOL.md
/// §8 `interop`). Lowercase kebab-style. Distinct from
/// [`crate::connection::Transport`] (PascalCase Rust enum) because the
/// wire format uses lowercase and is the source of truth for
/// cross-language peers.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum InteropTarget {
    Sip,
    Webrtc,
    Quic,
    Webtransport,
    Websocket,
}

/// Wire form of `identity_assurance_offered`. Maps to the gradient
/// in [`IdentityAssurance`] but flattened to a single string because the
/// wire format does not carry the variant payload.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum AssuranceLevel {
    #[default]
    Anonymous,
    Pseudonymous,
    Identified,
    TaskScoped,
    UserAuthorized,
}

impl AssuranceLevel {
    /// Map the wire-form level to its kebab-case label.
    pub fn to_core(self) -> Option<&'static str> {
        Some(match self {
            AssuranceLevel::Anonymous => "anonymous",
            AssuranceLevel::Pseudonymous => "pseudonymous",
            AssuranceLevel::Identified => "identified",
            AssuranceLevel::TaskScoped => "task-scoped",
            AssuranceLevel::UserAuthorized => "user-authorized",
        })
    }

    /// Derive the wire level from a full [`IdentityAssurance`].
    pub fn from_core(assurance: &IdentityAssurance) -> Self {
        match assurance {
            IdentityAssurance::Anonymous => AssuranceLevel::Anonymous,
            IdentityAssurance::Pseudonymous { .. } => AssuranceLevel::Pseudonymous,
            IdentityAssurance::Identified { .. } => AssuranceLevel::Identified,
            IdentityAssurance::TaskScoped { .. } => AssuranceLevel::TaskScoped,
            IdentityAssurance::UserAuthorized { .. } => AssuranceLevel::UserAuthorized,
            // D2 — DTLS fingerprint is key-binding without a real-world
            // identity, so the closest wire level is Pseudonymous.
            IdentityAssurance::DtlsFingerprint { .. } => AssuranceLevel::Pseudonymous,
        }
    }
}

// =====================================================================
// Existing intersection / negotiation types (retained from the narrow
// CapabilityDescriptor era — used by rvoip-sip and other adapters)
// =====================================================================

#[derive(Clone, Default, Serialize, Deserialize)]
pub struct CapabilityIntersection {
    pub audio: Option<CodecInfo>,
    pub video: Option<CodecInfo>,
    pub dtmf_method: Option<DtmfMethod>,
    pub messaging_enabled: bool,
}

impl fmt::Debug for CapabilityIntersection {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("CapabilityIntersection")
            .field("audio_present", &self.audio.is_some())
            .field("video_present", &self.video.is_some())
            .field("dtmf_method", &self.dtmf_method)
            .field("messaging_enabled", &self.messaging_enabled)
            .finish()
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum DtmfMethod {
    Rfc4733,
    SipInfo,
}

#[derive(Clone, Default, Serialize, Deserialize)]
pub struct NegotiatedCodecs {
    pub audio: Option<CodecInfo>,
    pub video: Option<CodecInfo>,
}

impl fmt::Debug for NegotiatedCodecs {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("NegotiatedCodecs")
            .field("audio_present", &self.audio.is_some())
            .field("video_present", &self.video.is_some())
            .finish()
    }
}

// =====================================================================
// §8.1 negotiation algorithm (relocated from rvoip-uctp)
// =====================================================================

/// Outcome of running [`negotiate_streams`] over an offer/answer pair.
#[derive(Clone)]
pub enum NegotiationOutcome {
    /// Per-Stream chosen codecs. Order matches the input `streams_offered`.
    Ok(Vec<NegotiatedStream>),
    /// Spec §11.2 488: no codecs overlapped on any stream.
    NotAcceptable488,
}

impl fmt::Debug for NegotiationOutcome {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Ok(streams) => formatter
                .debug_struct("Ok")
                .field("stream_count", &streams.len())
                .finish(),
            Self::NotAcceptable488 => formatter.write_str("NotAcceptable488"),
        }
    }
}

/// One stream's negotiation result.
#[derive(Clone)]
pub struct NegotiatedStream {
    pub stream_id: String,
    pub kind: String,
    pub direction: String,
    /// `Some(codec_name)` when at least one of the offerer's preferences
    /// matched the answerer's capability; `None` when this individual
    /// stream had no overlap.
    pub chosen_codec: Option<String>,
}

impl fmt::Debug for NegotiatedStream {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("NegotiatedStream")
            .field("stream_id_present", &!self.stream_id.is_empty())
            .field("stream_id_bytes", &self.stream_id.len())
            .field("kind_present", &!self.kind.is_empty())
            .field("kind_bytes", &self.kind.len())
            .field("direction_present", &!self.direction.is_empty())
            .field("direction_bytes", &self.direction.len())
            .field("chosen_codec_present", &self.chosen_codec.is_some())
            .finish()
    }
}

/// Input shape mirroring `connection.offer.streams_offered`.
#[derive(Clone)]
pub struct StreamOffer<'a> {
    pub id: &'a str,
    pub kind: &'a str,
    pub direction: &'a str,
    pub codec_preferences: &'a [String],
}

impl fmt::Debug for StreamOffer<'_> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("StreamOffer")
            .field("id_present", &!self.id.is_empty())
            .field("id_bytes", &self.id.len())
            .field("kind_present", &!self.kind.is_empty())
            .field("kind_bytes", &self.kind.len())
            .field("direction_present", &!self.direction.is_empty())
            .field("direction_bytes", &self.direction.len())
            .field("codec_preference_count", &self.codec_preferences.len())
            .finish()
    }
}

#[cfg(test)]
mod diagnostic_tests {
    use super::*;

    #[test]
    fn internal_pcm_codec_uses_wideband_mono_defaults() {
        let codec = CodecInfo::from_name_with_defaults("pcm_s16le");
        assert_eq!(codec.clock_rate_hz, 16_000);
        assert_eq!(codec.channels, 1);
        assert!(codec.fmtp.is_none());
    }

    #[test]
    fn capability_diagnostics_never_render_peer_strings() {
        const CANARY: &str = "capability-canary\r\nAuthorization: exposed";
        let codec = CodecInfo {
            name: CANARY.into(),
            clock_rate_hz: 48_000,
            channels: 1,
            fmtp: Some(CANARY.into()),
        };
        let descriptor = CapabilityDescriptor {
            audio_codecs: vec![codec.clone()],
            ..CapabilityDescriptor::default()
        };
        let negotiated = NegotiatedStream {
            stream_id: CANARY.into(),
            kind: CANARY.into(),
            direction: CANARY.into(),
            chosen_codec: Some(CANARY.into()),
        };
        for debug in [
            format!("{codec:?}"),
            format!("{:?}", Codec::new(CANARY)),
            format!("{descriptor:?}"),
            format!("{negotiated:?}"),
            format!("{:?}", NegotiationOutcome::Ok(vec![negotiated])),
        ] {
            assert!(!debug.contains(CANARY));
        }
    }
}

/// Run the §8.1 negotiation algorithm on a single offer/answer pair.
///
/// 1. Walks the offerer's `codec_preferences` in order.
/// 2. Picks the first codec the answerer advertises (audio or video).
/// 3. If **no** stream gets a codec, returns
///    [`NegotiationOutcome::NotAcceptable488`].
pub fn negotiate_streams<'a, I>(
    streams_offered: I,
    answerer: &CapabilityDescriptor,
) -> NegotiationOutcome
where
    I: IntoIterator<Item = StreamOffer<'a>>,
{
    let answerer_codecs: std::collections::HashSet<&str> = answerer
        .audio_codecs
        .iter()
        .chain(answerer.video_codecs.iter())
        .map(|c| c.name.as_str())
        .collect();

    let mut results = Vec::new();
    let mut any_match = false;

    for offer in streams_offered {
        let chosen = offer
            .codec_preferences
            .iter()
            .find(|c| answerer_codecs.contains(c.as_str()))
            .cloned();
        if chosen.is_some() {
            any_match = true;
        }
        results.push(NegotiatedStream {
            stream_id: offer.id.to_string(),
            kind: offer.kind.to_string(),
            direction: offer.direction.to_string(),
            chosen_codec: chosen,
        });
    }

    if any_match {
        NegotiationOutcome::Ok(results)
    } else {
        NegotiationOutcome::NotAcceptable488
    }
}