pocketstation 1.0.0

Source-aware desktop audio Session SDK
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
//! Canonical port, media-capability, and edge contracts for the typed graph.
//! Negotiation rules decide which graph ports may connect and what each edge
//! guarantees at runtime.

use crate::frame::SampleFormat;

use crate::graph::signal::{BinaryFormat, Codec, SignalClass, SignalSpec};

/// Default maximum owned payload admitted to one asynchronous signal edge.
/// Large streams must be chunked by their source/connector instead of turning
/// one queue item into an unbounded allocation.
pub const DEFAULT_ASYNC_MAX_PAYLOAD_BYTES: usize = 1_048_576;
pub const MAX_ASYNC_PAYLOAD_BYTES: usize = 16_777_216;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MediaKind {
    AudioPcm,
    AudioEncoded,
    Text,
    Event,
    Metrics,
    Control,
    Binary,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChannelLayout {
    Mono,
    Stereo,
    Any, // wildcard; matches any concrete layout during negotiation
}

impl ChannelLayout {
    pub fn channel_count(self) -> Option<u8> {
        match self {
            Self::Mono => Some(1),
            Self::Stereo => Some(2),
            Self::Any => None,
        }
    }

    pub fn is_compatible_with(self, other: ChannelLayout) -> bool {
        matches!(self, Self::Any) || matches!(other, Self::Any) || self == other
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AudioCaps {
    pub sample_rate_hz: Option<u32>,  // None = any rate accepted
    pub frame_samples: Option<usize>, // None = any frame length accepted
    pub channel_layout: ChannelLayout,
    pub format: SampleFormat,
}

impl AudioCaps {
    pub fn is_compatible_with(&self, other: &AudioCaps) -> bool {
        Self::scalar_compatible(self.sample_rate_hz, other.sample_rate_hz)
            && Self::scalar_compatible(self.frame_samples, other.frame_samples)
            && self.channel_layout.is_compatible_with(other.channel_layout)
            && self.format == other.format
    }

    fn scalar_compatible<T: PartialEq>(lhs: Option<T>, rhs: Option<T>) -> bool {
        match (lhs, rhs) {
            (Some(a), Some(b)) => a == b,
            _ => true,
        }
    }

    fn narrow(&self, other: &AudioCaps) -> AudioCaps {
        AudioCaps {
            sample_rate_hz: self.sample_rate_hz.or(other.sample_rate_hz),
            frame_samples: self.frame_samples.or(other.frame_samples),
            channel_layout: if matches!(self.channel_layout, ChannelLayout::Any) {
                other.channel_layout
            } else {
                self.channel_layout
            },
            format: self.format,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MediaCaps {
    Audio(AudioCaps),
    EncodedAudio(Codec),
    Text,
    Event,
    Metrics,
    Control,
    Binary(BinaryFormat),
    Any, // wildcard port; has no single MediaKind
}

impl MediaCaps {
    pub fn kind(&self) -> Option<MediaKind> {
        match self {
            Self::Audio(_) => Some(MediaKind::AudioPcm),
            Self::EncodedAudio(_) => Some(MediaKind::AudioEncoded),
            Self::Text => Some(MediaKind::Text),
            Self::Event => Some(MediaKind::Event),
            Self::Metrics => Some(MediaKind::Metrics),
            Self::Control => Some(MediaKind::Control),
            Self::Binary(_) => Some(MediaKind::Binary),
            Self::Any => None,
        }
    }

    pub fn is_compatible_with(&self, other: &MediaCaps) -> bool {
        match (self, other) {
            (Self::Any, _) | (_, Self::Any) => true,
            (Self::Audio(a), Self::Audio(b)) => a.is_compatible_with(b),
            (Self::EncodedAudio(a), Self::EncodedAudio(b)) => a == b,
            (Self::Text, Self::Text) => true,
            (Self::Event, Self::Event) => true,
            (Self::Metrics, Self::Metrics) => true,
            (Self::Control, Self::Control) => true,
            (Self::Binary(a), Self::Binary(b)) => a == b,
            _ => false,
        }
    }

    pub fn negotiate(&self, other: &MediaCaps) -> Option<MediaCaps> {
        match (self, other) {
            (Self::Any, narrower) | (narrower, Self::Any) => Some(*narrower),
            (Self::Audio(a), Self::Audio(b)) if a.is_compatible_with(b) => {
                Some(Self::Audio(a.narrow(b)))
            }
            (Self::EncodedAudio(a), Self::EncodedAudio(b)) if a == b => {
                Some(Self::EncodedAudio(*a))
            }
            (Self::Text, Self::Text) => Some(Self::Text),
            (Self::Event, Self::Event) => Some(Self::Event),
            (Self::Metrics, Self::Metrics) => Some(Self::Metrics),
            (Self::Control, Self::Control) => Some(Self::Control),
            (Self::Binary(a), Self::Binary(b)) if a == b => Some(Self::Binary(*a)),
            _ => None,
        }
    }

    pub fn supports_signal(&self, signal: &SignalSpec) -> bool {
        match (&signal.class, self) {
            (_, Self::Any) | (SignalClass::Any, _) => true,
            (SignalClass::PcmAudio, Self::Audio(_)) => true,
            (SignalClass::EncodedAudio(signal_codec), Self::EncodedAudio(media_codec)) => {
                signal_codec == media_codec
            }
            (SignalClass::Text(_), Self::Text) => true,
            (SignalClass::Event(_), Self::Event) => true,
            (SignalClass::Metrics, Self::Metrics) => true,
            (SignalClass::Control, Self::Control) => true,
            (SignalClass::Binary(signal_format), Self::Binary(media_format)) => {
                signal_format == media_format
            }
            (SignalClass::Custom(_), Self::Binary(_)) => signal.schema.is_some(),
            _ => false,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PortDirection {
    Input,
    Output,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Multiplicity {
    One,
    Many,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PortSpec {
    pub(crate) name: String,
    pub(crate) direction: PortDirection,
    pub(crate) signal: SignalSpec,
    pub(crate) media: MediaCaps,
    pub(crate) multiplicity: Multiplicity,
    pub(crate) required: bool,
}

impl PortSpec {
    pub fn new(
        name: impl Into<String>,
        direction: PortDirection,
        signal: SignalSpec,
        media: MediaCaps,
        multiplicity: Multiplicity,
        required: bool,
    ) -> Result<Self, PortSpecError> {
        let name = name.into();
        if name.trim().is_empty() {
            return Err(PortSpecError::EmptyName);
        }
        signal
            .validate()
            .map_err(|_| PortSpecError::InvalidSignal)?;
        if !media.supports_signal(&signal) {
            return Err(PortSpecError::SignalMediaMismatch);
        }
        Ok(Self {
            name,
            direction,
            signal,
            media,
            multiplicity,
            required,
        })
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub const fn direction(&self) -> PortDirection {
        self.direction
    }

    pub const fn signal(&self) -> &SignalSpec {
        &self.signal
    }

    pub const fn media(&self) -> MediaCaps {
        self.media
    }

    pub const fn multiplicity(&self) -> Multiplicity {
        self.multiplicity
    }

    pub const fn required(&self) -> bool {
        self.required
    }
}

#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum PortSpecError {
    #[error("port name cannot be empty")]
    EmptyName,
    #[error("port SignalSpec is invalid")]
    InvalidSignal,
    #[error("port signal and media representation are incompatible")]
    SignalMediaMismatch,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ClockDomain {
    Capture,
    Playback,
    Network,
    /// Preserve the clock carried by the producer's signal envelope.
    Inherited,
    Wallclock,
}

impl ClockDomain {
    pub fn is_realtime(self) -> bool {
        matches!(self, Self::Capture | Self::Playback)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BackpressurePolicy {
    DropNewest,     // shed the incoming frame; preserves already-queued order
    DropOldest,     // evict head to admit newest; freshest-wins for realtime audio
    BoundedQueue,   // block producer only via capacity; never drops silently
    BlockForbidden, // producer must never block; overflow is a hard error upstream
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeliverySemantics {
    BestEffortRealtime,
    Ordered,
    ExactlyOnceNotRealtime,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CopyPolicy {
    MoveExclusive,
    ShareReadOnly,
    CopyToBranchPool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LossPolicy {
    ConcealForAudio,   // PLC-eligible; dropped audio is concealed downstream
    MustDeliverOrFail, // terminal output must be delivered or the branch fails visibly
    DropAllowed,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EdgeObservabilityLevel {
    Off,
    Counters,
    Full,
}

impl EdgeObservabilityLevel {
    pub fn rank(self) -> u8 {
        match self {
            Self::Off => 0,
            Self::Counters => 1,
            Self::Full => 2,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EdgeContract {
    pub(crate) media: MediaCaps,
    pub(crate) clock: ClockDomain,
    pub(crate) latency_budget_ms: Option<u32>,
    pub(crate) jitter_budget_ms: Option<u32>,
    pub(crate) backpressure: BackpressurePolicy,
    pub(crate) delivery: DeliverySemantics,
    pub(crate) loss: LossPolicy,
    pub(crate) copy_policy: CopyPolicy,
    pub(crate) observability: EdgeObservabilityLevel,
    pub(crate) max_payload_bytes: Option<usize>,
}

impl EdgeContract {
    pub const fn media(&self) -> MediaCaps {
        self.media
    }

    pub const fn clock(&self) -> ClockDomain {
        self.clock
    }

    pub const fn latency_budget_ms(&self) -> Option<u32> {
        self.latency_budget_ms
    }

    pub const fn jitter_budget_ms(&self) -> Option<u32> {
        self.jitter_budget_ms
    }

    pub const fn backpressure(&self) -> BackpressurePolicy {
        self.backpressure
    }

    pub const fn delivery(&self) -> DeliverySemantics {
        self.delivery
    }

    pub const fn loss(&self) -> LossPolicy {
        self.loss
    }

    pub const fn copy_policy(&self) -> CopyPolicy {
        self.copy_policy
    }

    pub const fn observability(&self) -> EdgeObservabilityLevel {
        self.observability
    }

    pub const fn max_payload_bytes(&self) -> Option<usize> {
        self.max_payload_bytes
    }

    pub fn with_media(mut self, media: MediaCaps) -> Self {
        self.media = media;
        self
    }

    pub fn with_backpressure(mut self, backpressure: BackpressurePolicy) -> Self {
        self.backpressure = backpressure;
        self
    }

    pub fn with_copy_policy(mut self, copy_policy: CopyPolicy) -> Self {
        self.copy_policy = copy_policy;
        self
    }

    pub fn with_jitter_budget_ms(mut self, jitter_budget_ms: Option<u32>) -> Self {
        self.jitter_budget_ms = jitter_budget_ms;
        self
    }

    pub fn with_max_payload_bytes(mut self, max_payload_bytes: usize) -> Self {
        self.max_payload_bytes = Some(max_payload_bytes);
        self
    }
    /// Generic realtime PCM edge. Concrete sample rate, frame size, and
    /// channel layout are negotiated from connected ports.
    pub fn realtime_audio() -> Self {
        Self {
            media: MediaCaps::Audio(AudioCaps {
                sample_rate_hz: None,
                frame_samples: None,
                channel_layout: ChannelLayout::Any,
                format: SampleFormat::F32Interleaved,
            }),
            clock: ClockDomain::Capture,
            latency_budget_ms: None,
            jitter_budget_ms: None,
            backpressure: BackpressurePolicy::DropNewest,
            delivery: DeliverySemantics::Ordered,
            loss: LossPolicy::ConcealForAudio,
            copy_policy: CopyPolicy::ShareReadOnly,
            observability: EdgeObservabilityLevel::Counters,
            max_payload_bytes: None,
        }
    }

    /// Generic bounded asynchronous edge. Connected ports supply the payload
    /// representation and the envelope preserves its producer clock.
    pub fn bounded_async() -> Self {
        Self {
            media: MediaCaps::Any,
            clock: ClockDomain::Inherited,
            latency_budget_ms: None,
            jitter_budget_ms: None,
            backpressure: BackpressurePolicy::BoundedQueue,
            delivery: DeliverySemantics::Ordered,
            loss: LossPolicy::MustDeliverOrFail,
            copy_policy: CopyPolicy::ShareReadOnly,
            observability: EdgeObservabilityLevel::Counters,
            max_payload_bytes: Some(DEFAULT_ASYNC_MAX_PAYLOAD_BYTES),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use proptest::prelude::*;

    fn stereo_caps() -> AudioCaps {
        AudioCaps {
            sample_rate_hz: Some(48_000),
            frame_samples: Some(960),
            channel_layout: ChannelLayout::Stereo,
            format: SampleFormat::F32Interleaved,
        }
    }

    #[test]
    fn given_mono_and_stereo_when_channel_count_then_returns_one_and_two() {
        assert_eq!(ChannelLayout::Mono.channel_count(), Some(1));
        assert_eq!(ChannelLayout::Stereo.channel_count(), Some(2));
        assert_eq!(ChannelLayout::Any.channel_count(), None);
    }

    #[test]
    fn given_any_layout_when_compat_checked_both_directions_then_matches() {
        assert!(ChannelLayout::Any.is_compatible_with(ChannelLayout::Mono));
        assert!(ChannelLayout::Stereo.is_compatible_with(ChannelLayout::Any));
        assert!(!ChannelLayout::Mono.is_compatible_with(ChannelLayout::Stereo));
    }

    #[test]
    fn given_wildcard_rate_when_audio_compat_checked_then_matches_concrete() {
        let wildcard = AudioCaps {
            sample_rate_hz: None,
            frame_samples: None,
            channel_layout: ChannelLayout::Any,
            format: SampleFormat::F32Interleaved,
        };
        assert!(wildcard.is_compatible_with(&stereo_caps()));
        assert!(stereo_caps().is_compatible_with(&wildcard));
    }

    #[test]
    fn given_mismatched_rate_when_audio_compat_checked_then_incompatible() {
        let other = AudioCaps {
            sample_rate_hz: Some(44_100),
            ..stereo_caps()
        };
        assert!(!stereo_caps().is_compatible_with(&other));
    }

    #[test]
    fn given_audio_pair_when_media_compat_checked_then_compatible() {
        let a = MediaCaps::Audio(stereo_caps());
        let b = MediaCaps::Audio(stereo_caps());
        assert!(a.is_compatible_with(&b));
    }

    #[test]
    fn given_audio_and_text_when_media_compat_checked_then_incompatible() {
        let audio = MediaCaps::Audio(stereo_caps());
        assert!(!audio.is_compatible_with(&MediaCaps::Text));
    }

    #[test]
    fn given_any_media_when_compat_checked_both_directions_then_matches() {
        let audio = MediaCaps::Audio(stereo_caps());
        assert!(MediaCaps::Any.is_compatible_with(&audio));
        assert!(audio.is_compatible_with(&MediaCaps::Any));
    }

    #[test]
    fn given_any_and_audio_when_negotiated_then_yields_audio() {
        let audio = MediaCaps::Audio(stereo_caps());
        assert_eq!(MediaCaps::Any.negotiate(&audio), Some(audio));
    }

    #[test]
    fn given_incompatible_media_when_negotiated_then_none() {
        let audio = MediaCaps::Audio(stereo_caps());
        assert_eq!(audio.negotiate(&MediaCaps::Text), None);
    }

    #[test]
    fn given_wildcard_audio_when_negotiated_then_narrows_to_concrete() {
        let wildcard = MediaCaps::Audio(AudioCaps {
            sample_rate_hz: None,
            frame_samples: None,
            channel_layout: ChannelLayout::Any,
            format: SampleFormat::F32Interleaved,
        });
        let concrete = MediaCaps::Audio(stereo_caps());
        assert_eq!(wildcard.negotiate(&concrete), Some(concrete));
    }

    #[test]
    fn given_realtime_audio_when_built_then_physical_caps_remain_negotiable() {
        let edge = EdgeContract::realtime_audio();
        assert_eq!(edge.latency_budget_ms, None);
        assert_eq!(edge.jitter_budget_ms, None);
        assert_eq!(edge.backpressure, BackpressurePolicy::DropNewest);
        assert_eq!(edge.loss, LossPolicy::ConcealForAudio);
        assert!(matches!(
            edge.media,
            MediaCaps::Audio(AudioCaps {
                sample_rate_hz: None,
                frame_samples: None,
                ..
            })
        ));
    }

    #[test]
    fn given_bounded_async_when_built_then_contains_no_payload_or_clock_origin_assumption() {
        let edge = EdgeContract::bounded_async();
        assert_eq!(edge.loss, LossPolicy::MustDeliverOrFail);
        assert_eq!(edge.backpressure, BackpressurePolicy::BoundedQueue);
        assert_eq!(edge.media, MediaCaps::Any);
        assert_eq!(edge.delivery, DeliverySemantics::Ordered);
        assert_eq!(edge.clock, ClockDomain::Inherited);
        assert_eq!(edge.latency_budget_ms, None);
        assert_eq!(edge.jitter_budget_ms, None);
        assert_eq!(
            edge.max_payload_bytes,
            Some(DEFAULT_ASYNC_MAX_PAYLOAD_BYTES)
        );
    }

    #[test]
    fn given_observability_levels_when_ranked_then_ordered_ascending() {
        assert!(EdgeObservabilityLevel::Off.rank() < EdgeObservabilityLevel::Counters.rank());
        assert!(EdgeObservabilityLevel::Counters.rank() < EdgeObservabilityLevel::Full.rank());
    }

    #[test]
    fn given_supported_non_audio_signals_when_checked_then_media_is_symmetric() {
        assert!(MediaCaps::EncodedAudio(Codec::Opus)
            .supports_signal(&SignalSpec::encoded_audio(Codec::Opus)));
        assert!(MediaCaps::Text.supports_signal(&SignalSpec::text(crate::graph::TextFormat::Json)));
        assert!(MediaCaps::Event
            .supports_signal(&SignalSpec::event(crate::graph::EventFormat::Protobuf)));
        assert!(MediaCaps::Metrics.supports_signal(&SignalSpec::metrics()));
        assert!(MediaCaps::Control.supports_signal(&SignalSpec::control()));
        assert!(MediaCaps::Binary(BinaryFormat::Cbor)
            .supports_signal(&SignalSpec::binary(BinaryFormat::Cbor)));
        assert!(MediaCaps::Binary(BinaryFormat::Protobuf).supports_signal(
            &SignalSpec::custom("com.acme.signal.v1").with_schema("proto:acme.Signal")
        ));
    }

    #[test]
    fn given_custom_signal_without_schema_when_checked_then_binary_media_rejects_it() {
        assert!(!MediaCaps::Binary(BinaryFormat::Raw)
            .supports_signal(&SignalSpec::custom("com.acme.signal.v1")));
    }

    fn any_layout() -> impl Strategy<Value = ChannelLayout> {
        prop_oneof![
            Just(ChannelLayout::Mono),
            Just(ChannelLayout::Stereo),
            Just(ChannelLayout::Any),
        ]
    }

    fn any_audio_caps() -> impl Strategy<Value = AudioCaps> {
        (
            prop::option::of(any::<u32>()),
            prop::option::of(any::<usize>()),
            any_layout(),
        )
            .prop_map(
                |(sample_rate_hz, frame_samples, channel_layout)| AudioCaps {
                    sample_rate_hz,
                    frame_samples,
                    channel_layout,
                    format: SampleFormat::F32Interleaved,
                },
            )
    }

    proptest! {
        #[test]
        fn given_any_audio_caps_when_compat_checked_then_reflexive_and_symmetric(
            a in any_audio_caps(),
            b in any_audio_caps(),
        ) {
            prop_assert!(a.is_compatible_with(&a));
            prop_assert_eq!(a.is_compatible_with(&b), b.is_compatible_with(&a));
        }
    }
}