sim-lib-stream-host 0.1.0-rc.1

Host-device stream backend substrate for STREAM 6.
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
//! Host stream placement vocabulary and LAN peer placement policy.

use sim_kernel::{CapabilityName, Error, Expr, Result, Symbol};
use sim_lib_stream_core::{
    BridgeLatency, ClockDomain, DomainBridgeDescriptor, LatencyClass, PlacedFragment,
    StreamCapability, StreamEnvelope, TransportProfile,
};

const DEFAULT_BEATS_PER_BAR: u32 = 4;

/// Stable runtime key for an audio evaluation site.
///
/// The key is plain data so device catalogs can store and compare audio sites
/// without carrying platform handles.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct AudioSiteKey(pub Symbol);

impl AudioSiteKey {
    /// Builds an audio site key from a stable symbolic name.
    pub fn new(name: &str) -> Self {
        Self(Symbol::new(name))
    }
}

/// Export-record-style descriptor for an audio device.
///
/// The card carries only stable metadata and never owns platform handles.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AudioDeviceCard {
    /// Stable site key used for registration and lookup.
    pub key: AudioSiteKey,
    /// Human-facing device name.
    pub display_name: String,
    /// Number of output channels supported by this site.
    pub channels_out: u16,
    /// Number of input channels supported by this site.
    pub channels_in: u16,
    /// Advertised sample rates in hertz.
    pub sample_rates: Vec<u32>,
    /// Whether opening this site requires real host hardware.
    pub hardware_required: bool,
}

impl AudioDeviceCard {
    /// Builds a deterministic modeled stereo device card for validation.
    pub fn modeled(key: AudioSiteKey, name: impl Into<String>) -> Self {
        Self {
            key,
            display_name: name.into(),
            channels_out: 2,
            channels_in: 2,
            sample_rates: vec![44_100, 48_000],
            hardware_required: false,
        }
    }
}

/// Caller-side request to place an audio graph on a registered audio site.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AudioPlacementRequest {
    /// Target site key selected by placement.
    pub site_key: AudioSiteKey,
    /// Host stream request forwarded to the selected backend.
    pub stream_request: crate::HostStreamConfigRequest,
}

/// Placement tier for a host-visible stream device.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Placement {
    /// Deterministic in-process fixture that needs no host hardware.
    Modeled,
    /// Real host hardware behind the named transport.
    Hardware {
        /// Transport responsible for opening the hardware device.
        transport: Symbol,
    },
}

/// Stream-device media family surfaced by the shared device catalog.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DeviceKind {
    /// Audio PCM device.
    Audio,
    /// MIDI event device.
    Midi,
}

/// Direction supported by a cataloged stream device.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DeviceDirection {
    /// Device produces stream input.
    Input,
    /// Device consumes stream output.
    Output,
    /// Device supports input and output.
    Duplex,
}

/// Catalog row for a host-visible stream device.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DeviceRecord {
    /// Stable catalog identifier.
    pub id: Symbol,
    /// Human-facing device label.
    pub display_name: String,
    /// Device media family.
    pub kind: DeviceKind,
    /// Device stream direction.
    pub direction: DeviceDirection,
    /// Placement tier for the device.
    pub placement: Placement,
}

impl DeviceRecord {
    /// Builds a modeled MIDI input record.
    pub fn modeled_midi_input(id: &str, name: impl Into<String>) -> Self {
        Self::modeled(id, name, DeviceKind::Midi, DeviceDirection::Input)
    }

    /// Builds a modeled MIDI output record.
    pub fn modeled_midi_output(id: &str, name: impl Into<String>) -> Self {
        Self::modeled(id, name, DeviceKind::Midi, DeviceDirection::Output)
    }

    /// Builds a modeled audio output record.
    pub fn modeled_audio_output(id: &str, name: impl Into<String>) -> Self {
        Self::modeled(id, name, DeviceKind::Audio, DeviceDirection::Output)
    }

    /// Builds a modeled audio record from an audio device card.
    pub fn modeled_audio_from_card(card: &AudioDeviceCard) -> Self {
        let direction = match (card.channels_in > 0, card.channels_out > 0) {
            (true, true) => DeviceDirection::Duplex,
            (true, false) => DeviceDirection::Input,
            (false, true) | (false, false) => DeviceDirection::Output,
        };
        Self {
            id: card.key.0.clone(),
            display_name: card.display_name.clone(),
            kind: DeviceKind::Audio,
            direction,
            placement: Placement::Modeled,
        }
    }

    fn modeled(
        id: &str,
        name: impl Into<String>,
        kind: DeviceKind,
        direction: DeviceDirection,
    ) -> Self {
        Self {
            id: Symbol::new(id),
            display_name: name.into(),
            kind,
            direction,
            placement: Placement::Modeled,
        }
    }
}

/// Stable site name for a non-real-time node hosted by a LAN peer.
pub fn lan_peer_site_symbol() -> Symbol {
    Symbol::qualified("stream/site", "lan-peer")
}

/// Stable mode name for jitter-buffered LAN placement.
pub fn lan_jitter_buffered_mode_symbol() -> Symbol {
    Symbol::qualified("stream/lan-mode", "jitter-buffered")
}

/// Stable mode name for musically aligned bar-delay collaboration.
pub fn lan_bar_delay_mode_symbol() -> Symbol {
    Symbol::qualified("stream/lan-mode", "collab-bardelay")
}

/// Diagnostic emitted when a pinned sample-domain node is refused across LAN.
pub fn lan_pinned_sample_refusal_diagnostic() -> Symbol {
    Symbol::qualified("stream/lan-diagnostic", "pinned-sample-remote-refused")
}

/// Diagnostic emitted when experimental pinned sample-domain LAN placement is used.
pub fn lan_pinned_sample_experimental_diagnostic() -> Symbol {
    Symbol::qualified("stream/lan-diagnostic", "pinned-sample-remote-experimental")
}

/// Capability required to try pinned sample-domain placement across LAN.
pub fn lan_experimental_remote_sample_capability() -> CapabilityName {
    CapabilityName::new("stream.lan.experimental-remote-sample")
}

/// Placement mode for a stream fragment hosted on a LAN peer.
///
/// Selects how packets crossing the LAN are buffered and time-aligned: a plain
/// jitter buffer for buffered preview, or a musically aligned bar delay for
/// collaborative play.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LanPlacementMode {
    /// Jitter-buffered preview placement.
    JitterBuffered {
        /// Packets retained in the jitter buffer.
        jitter_packets: u32,
        /// Latency-compensation delay applied, in frames.
        latency_comp_frames: u64,
    },
    /// Musically aligned collaborative placement delayed by whole bars.
    BarDelay {
        /// Number of bars of alignment delay.
        bars: u32,
        /// Beats per bar used to size the bar delay.
        beats_per_bar: u32,
        /// Tempo in beats per minute used to size the bar delay.
        tempo_bpm: u32,
        /// Packets retained in the jitter buffer.
        jitter_packets: u32,
        /// Latency-compensation delay applied, in frames.
        latency_comp_frames: u64,
    },
}

impl LanPlacementMode {
    /// Builds a jitter-buffered mode retaining `jitter_packets` packets.
    ///
    /// Returns an evaluation error when `jitter_packets` is zero.
    ///
    /// # Examples
    ///
    /// ```
    /// use sim_lib_stream_host::LanPlacementMode;
    ///
    /// let mode = LanPlacementMode::jitter_buffered(4, 128).unwrap();
    /// assert_eq!(mode.jitter_packets(), 4);
    /// assert_eq!(mode.latency_comp_frames(), 128);
    /// assert!(mode.bar_delay_millis().is_none());
    /// ```
    pub fn jitter_buffered(jitter_packets: u32, latency_comp_frames: u64) -> Result<Self> {
        if jitter_packets == 0 {
            return Err(Error::Eval(
                "LAN jitter buffer must retain at least one packet".to_owned(),
            ));
        }
        Ok(Self::JitterBuffered {
            jitter_packets,
            latency_comp_frames,
        })
    }

    /// Builds a bar-delay mode delaying `bars` bars at `tempo_bpm`.
    ///
    /// Uses a default of four beats per bar. Returns an evaluation error when
    /// `bars`, `tempo_bpm`, or `jitter_packets` is zero.
    pub fn bar_delay(
        bars: u32,
        tempo_bpm: u32,
        jitter_packets: u32,
        latency_comp_frames: u64,
    ) -> Result<Self> {
        if bars == 0 {
            return Err(Error::Eval(
                "LAN bar-delay mode must delay at least one bar".to_owned(),
            ));
        }
        if tempo_bpm == 0 {
            return Err(Error::Eval(
                "LAN bar-delay mode tempo must be greater than zero".to_owned(),
            ));
        }
        if jitter_packets == 0 {
            return Err(Error::Eval(
                "LAN jitter buffer must retain at least one packet".to_owned(),
            ));
        }
        Ok(Self::BarDelay {
            bars,
            beats_per_bar: DEFAULT_BEATS_PER_BAR,
            tempo_bpm,
            jitter_packets,
            latency_comp_frames,
        })
    }

    /// Returns the stable mode symbol.
    pub fn symbol(self) -> Symbol {
        match self {
            Self::JitterBuffered { .. } => lan_jitter_buffered_mode_symbol(),
            Self::BarDelay { .. } => lan_bar_delay_mode_symbol(),
        }
    }

    /// Returns the latency class this mode places the fragment into.
    pub fn latency_class(self) -> LatencyClass {
        match self {
            Self::JitterBuffered { .. } => LatencyClass::BufferedPreview,
            Self::BarDelay { .. } => LatencyClass::CollabBarDelay,
        }
    }

    /// Returns the number of packets retained in the jitter buffer.
    pub fn jitter_packets(self) -> u32 {
        match self {
            Self::JitterBuffered { jitter_packets, .. } | Self::BarDelay { jitter_packets, .. } => {
                jitter_packets
            }
        }
    }

    /// Returns the latency-compensation delay in frames.
    pub fn latency_comp_frames(self) -> u64 {
        match self {
            Self::JitterBuffered {
                latency_comp_frames,
                ..
            }
            | Self::BarDelay {
                latency_comp_frames,
                ..
            } => latency_comp_frames,
        }
    }

    /// Returns the bar-delay length in milliseconds, or `None` for
    /// jitter-buffered placement.
    pub fn bar_delay_millis(self) -> Option<u64> {
        match self {
            Self::JitterBuffered { .. } => None,
            Self::BarDelay {
                bars,
                beats_per_bar,
                tempo_bpm,
                ..
            } => Some(
                u64::from(bars)
                    .saturating_mul(u64::from(beats_per_bar))
                    .saturating_mul(60_000)
                    / u64::from(tempo_bpm),
            ),
        }
    }

    /// Returns the transport profile advertised for this mode.
    pub fn transport_profile(self) -> Result<TransportProfile> {
        match self {
            Self::JitterBuffered { .. } => Ok(TransportProfile::lan_buffered_audio_preview()),
            Self::BarDelay { .. } => TransportProfile::new(
                Symbol::qualified("stream/profile", "lan-collab-bardelay"),
                LatencyClass::CollabBarDelay,
                vec![
                    StreamCapability::Remote,
                    StreamCapability::Bounded,
                    StreamCapability::Preview,
                    StreamCapability::Lossy,
                ],
            ),
        }
    }

    fn bridges(self) -> Vec<DomainBridgeDescriptor> {
        vec![
            DomainBridgeDescriptor::jitter_buffer(self.jitter_packets()),
            DomainBridgeDescriptor::latency_comp_delay(self.latency_comp_frames()),
        ]
    }
}

/// Request to place a stream fragment on a LAN peer under a chosen mode.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LanPlacementRequest {
    fragment: PlacedFragment,
    mode: LanPlacementMode,
    realtime_pinned: bool,
    capabilities: Vec<CapabilityName>,
}

impl LanPlacementRequest {
    /// Builds a request to place `fragment` using `mode`, unpinned and with no
    /// extra capabilities.
    pub fn new(fragment: PlacedFragment, mode: LanPlacementMode) -> Self {
        Self {
            fragment,
            mode,
            realtime_pinned: false,
            capabilities: Vec::new(),
        }
    }

    /// Marks whether the fragment is pinned to realtime (sample-locked) play.
    pub fn with_realtime_pin(mut self, realtime_pinned: bool) -> Self {
        self.realtime_pinned = realtime_pinned;
        self
    }

    /// Grants an additional capability to the request.
    pub fn with_capability(mut self, capability: CapabilityName) -> Self {
        self.capabilities.push(capability);
        self
    }

    /// Plans the placement, returning a report or an evaluation error.
    ///
    /// Refuses a realtime-pinned sample-domain fragment across the LAN unless
    /// the experimental remote-sample capability is granted, in which case it
    /// proceeds and records an experimental diagnostic.
    pub fn plan(&self) -> Result<LanPlacementReport> {
        let experimental = self
            .capabilities
            .contains(&lan_experimental_remote_sample_capability());
        if self.realtime_pinned && self.fragment_has_sample_domain() && !experimental {
            let diagnostic = lan_pinned_sample_refusal_diagnostic();
            return Err(Error::Eval(format!(
                "{}: pinned sample-domain nodes cannot be sample-locked across LAN",
                diagnostic.as_qualified_str()
            )));
        }

        let mut diagnostics = Vec::new();
        if self.realtime_pinned && self.fragment_has_sample_domain() {
            diagnostics.push(lan_pinned_sample_experimental_diagnostic());
        }

        let profile = self.mode.transport_profile()?;
        let output_envelopes =
            remote_output_envelopes(&self.fragment.output_envelopes(), &profile, &diagnostics)?;
        Ok(LanPlacementReport {
            fragment_id: self.fragment.id().clone(),
            site: lan_peer_site_symbol(),
            mode: self.mode,
            bridges: self.mode.bridges(),
            output_envelopes,
            diagnostics,
        })
    }

    fn fragment_has_sample_domain(&self) -> bool {
        self.fragment
            .input_edges()
            .iter()
            .chain(self.fragment.output_edges())
            .any(|edge| edge.rate_contract().clock_domain() == ClockDomain::Sample)
    }
}

/// Outcome of planning a LAN fragment placement.
///
/// Records the placement site, mode, the domain bridges inserted, the rewritten
/// output envelopes carrying the remote transport profile, and any diagnostics.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LanPlacementReport {
    fragment_id: Symbol,
    site: Symbol,
    mode: LanPlacementMode,
    bridges: Vec<DomainBridgeDescriptor>,
    output_envelopes: Vec<StreamEnvelope>,
    diagnostics: Vec<Symbol>,
}

impl LanPlacementReport {
    /// Returns the placed fragment identifier.
    pub fn fragment_id(&self) -> &Symbol {
        &self.fragment_id
    }

    /// Returns the placement site symbol.
    pub fn site(&self) -> &Symbol {
        &self.site
    }

    /// Returns the placement mode.
    pub fn mode(&self) -> LanPlacementMode {
        self.mode
    }

    /// Returns the latency class of the placement.
    pub fn latency_class(&self) -> LatencyClass {
        self.mode.latency_class()
    }

    /// Returns the domain bridges inserted by the placement.
    pub fn bridges(&self) -> &[DomainBridgeDescriptor] {
        &self.bridges
    }

    /// Returns the rewritten output envelopes.
    pub fn output_envelopes(&self) -> &[StreamEnvelope] {
        &self.output_envelopes
    }

    /// Returns the diagnostics recorded during planning.
    pub fn diagnostics(&self) -> &[Symbol] {
        &self.diagnostics
    }

    /// Returns the total latency added by the inserted bridges.
    pub fn added_bridge_latency(&self) -> BridgeLatency {
        self.bridges
            .iter()
            .fold(BridgeLatency::zero(), |latency, bridge| {
                latency.plus(bridge.latency())
            })
    }

    /// Returns the bar-delay length in milliseconds when the mode uses one.
    pub fn bar_delay_millis(&self) -> Option<u64> {
        self.mode.bar_delay_millis()
    }

    /// Builds a browse/inspection expression summarizing the placement.
    pub fn to_expr(&self) -> Expr {
        let latency = self.added_bridge_latency();
        Expr::Map(vec![
            (
                Expr::Symbol(Symbol::new("fragment")),
                Expr::Symbol(self.fragment_id.clone()),
            ),
            (
                Expr::Symbol(Symbol::new("site")),
                Expr::Symbol(self.site.clone()),
            ),
            (
                Expr::Symbol(Symbol::new("mode")),
                Expr::Symbol(self.mode.symbol()),
            ),
            (
                Expr::Symbol(Symbol::new("latency-class")),
                Expr::Symbol(self.latency_class().symbol()),
            ),
            (
                Expr::Symbol(Symbol::new("bar-delay-ms")),
                Expr::String(self.bar_delay_millis().unwrap_or(0).to_string()),
            ),
            (
                Expr::Symbol(Symbol::new("bridge-latency-frames")),
                Expr::String(latency.frame_count().to_string()),
            ),
            (
                Expr::Symbol(Symbol::new("bridge-latency-packets")),
                Expr::String(latency.packet_count().to_string()),
            ),
            (
                Expr::Symbol(Symbol::new("bridges")),
                Expr::List(
                    self.bridges
                        .iter()
                        .map(|bridge| Expr::Symbol(bridge.kind().symbol()))
                        .collect(),
                ),
            ),
            (
                Expr::Symbol(Symbol::new("diagnostics")),
                Expr::List(self.diagnostics.iter().cloned().map(Expr::Symbol).collect()),
            ),
            (
                Expr::Symbol(Symbol::new("output-profiles")),
                Expr::List(
                    self.output_envelopes
                        .iter()
                        .map(|envelope| Expr::Symbol(envelope.profile().name().clone()))
                        .collect(),
                ),
            ),
        ])
    }
}

fn remote_output_envelopes(
    envelopes: &[StreamEnvelope],
    profile: &TransportProfile,
    diagnostics: &[Symbol],
) -> Result<Vec<StreamEnvelope>> {
    envelopes
        .iter()
        .map(|envelope| {
            let mut envelope_diagnostics = envelope.diagnostics().to_vec();
            envelope_diagnostics.extend(diagnostics.iter().cloned());
            StreamEnvelope::new_with_clock_domains(
                envelope.stream_id().clone(),
                envelope.packet_id().clone(),
                envelope.media(),
                envelope.direction(),
                envelope.sequence(),
                envelope.ticks().to_vec(),
                envelope.clock_domain(),
                envelope.clock_domains().to_vec(),
                profile.clone(),
                envelope_diagnostics,
                envelope.packet().clone(),
            )
        })
        .collect()
}