plughost-core 0.2.6

Shared types and offline render rules for plughost
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
//! Messages between an application and a plughost helper process, and their framing: a
//! little-endian `u32` length followed by the postcard encoding. postcard carries no type
//! information, so the application checks the helper's [`PROTOCOL_VERSION`] before sending requests.
//!
//! This module and its shared-memory transport are internal to `plughost` and `plughost-helper`
//! and exempt from semver.

use std::borrow::Cow;
use std::path::PathBuf;

use serde::{Deserialize, Serialize};
use serde_with::serde_as;

use crate::errors::InputError;
use crate::parameter::ParameterInfo;
use crate::plugin::{PluginInfo, PluginRef};
use crate::render::Tail;
use crate::state::{PluginState, PresetInfo, StatePurpose};
use crate::{CapabilityReport, DiagnosticBatch, Failure, HostIdentity};

pub const PROTOCOL_VERSION: u32 = 47;
/// Upper bound on one message. A helper whose memory a plugin corrupted could send anything.
pub const MAX_MESSAGE_BYTES: usize = 256 << 20;
/// The deepest submenu a menu notice carries: the helper leaves deeper ones out, and the
/// application does not decode them.
pub const MAX_MENU_DEPTH: usize = 16;

/// The helper's first message after connecting. Its encoding is the same in every protocol
/// version, so an application can report the version of a helper built with another one.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Hello {
    pub protocol: u32,
    /// The token the application passed on the command line, proving the connection comes from
    /// the helper it started.
    pub token: u64,
}

/// Outgoing state and preset payloads can borrow caller storage until serialization. Decoding
/// always owns these payloads, so queued helper requests remain independent of receive buffers.
#[serde_as]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Request<'a> {
    Changes,
    AudioBuses {
        slot: usize,
    },
    EventPorts {
        slot: usize,
    },
    AudioConfigurations {
        slot: usize,
    },
    Bypass {
        slot: usize,
    },
    SetBypass {
        slot: usize,
        enabled: bool,
    },
    /// Whether the slot's host bypass is on. Answered with `HostBypass`.
    HostBypass {
        slot: usize,
    },
    /// Turns the slot's host bypass on or off: the helper replaces the slot's main output with its
    /// main input delayed by the slot's latency while the plugin goes on processing.
    SetHostBypass {
        slot: usize,
        enabled: bool,
    },
    PrepareAudio {
        #[serde_as(as = "wire::RoutedChainConfigDef")]
        config: crate::RoutedChainConfig,
        memory: shared::Descriptor,
        handle: u64,
    },
    ParameterEvents {
        slot: usize,
    },
    FactoryPresets {
        slot: usize,
    },
    SelectFactoryPreset {
        slot: usize,
        #[serde_as(as = "wire::FactoryPresetIdDef")]
        preset: crate::FactoryPresetId,
    },
    LoadDiscoveredPreset {
        slot: usize,
        #[serde_as(as = "wire::PresetLocationDef")]
        location: crate::PresetLocation,
        load_key: Option<String>,
    },
    PresetProviders {
        bundle: PathBuf,
        #[serde_as(as = "wire::HostIdentityDef")]
        host: HostIdentity,
    },
    DiscoverPresets {
        bundle: PathBuf,
        #[serde_as(as = "wire::HostIdentityDef")]
        host: HostIdentity,
        #[serde_as(as = "wire::PresetDiscoveryTargetDef")]
        target: crate::PresetDiscoveryTarget,
    },
    /// Drains bounded plugin and host diagnostic queues. Logs never push unsolicited traffic.
    Diagnostics,
    /// Scan mode: load one bundle and report its classes. An interactive scan brings the helper
    /// to the front first, so the user can answer a dialog the plugin shows while loading.
    Scan {
        bundle: PathBuf,
        #[serde_as(as = "wire::HostIdentityDef")]
        host: HostIdentity,
        interactive: bool,
    },
    /// Scan mode: report the Audio Units in the system registry, without loading them.
    ListAudioUnits,
    /// Validate each distinct render automation target before the first block advances DSP.
    ValidateAutomation {
        targets: Vec<(usize, u64)>,
    },
    /// Host mode: load the chain. `activity` is the transfer token of the application's
    /// [`shared::Activity`], through which the helper announces the slot each of its threads calls.
    Load {
        #[serde_as(as = "wire::HostIdentityDef")]
        host: HostIdentity,
        #[serde_as(as = "Vec<wire::PluginRefDef>")]
        plugins: Vec<PluginRef>,
        activity: u64,
    },
    /// Host mode, right after `Load`: restores a project state into the slot's newly loaded
    /// plugin. Each state goes in its own request, so the states of a chain together may exceed
    /// one message.
    LoadState {
        slot: usize,
        #[serde(
            serialize_with = "state::serialize",
            deserialize_with = "state::deserialize_cow"
        )]
        state: Cow<'a, PluginState>,
    },
    /// One block through the chain, with events on the chain's external event inputs.
    Process {
        #[serde_as(as = "wire::BlockContextDef")]
        context: crate::BlockContext,
        submission: shared::Submission,
    },
    Parameters {
        slot: usize,
    },
    Capabilities {
        slot: usize,
    },
    SetParameter {
        slot: usize,
        id: u64,
        value: f64,
    },
    ParameterDetails {
        slot: usize,
        id: u64,
    },
    ParameterChoices {
        slot: usize,
        id: u64,
        start: u64,
        count: u32,
    },
    ParameterText {
        slot: usize,
        id: u64,
        value: f64,
    },
    ParameterFromText {
        slot: usize,
        id: u64,
        text: String,
    },
    ParameterToPlain {
        slot: usize,
        id: u64,
        value: f64,
    },
    ParameterToNormalized {
        slot: usize,
        id: u64,
        plain: f64,
    },
    SetParameterText {
        slot: usize,
        id: u64,
        text: String,
    },
    SaveState {
        slot: usize,
        #[serde_as(as = "wire::StatePurposeDef")]
        purpose: StatePurpose,
    },
    RestoreState {
        slot: usize,
        #[serde(
            serialize_with = "state::serialize",
            deserialize_with = "state::deserialize_cow"
        )]
        state: Cow<'a, PluginState>,
        #[serde_as(as = "wire::StatePurposeDef")]
        purpose: StatePurpose,
    },
    InspectPreset {
        slot: usize,
        bytes: Cow<'a, [u8]>,
    },
    ExportPreset {
        slot: usize,
    },
    ImportPreset {
        slot: usize,
        bytes: Cow<'a, [u8]>,
    },
    /// Replaces the slot's instance with a fresh one restored from the active one's project
    /// state, staged like `RestoreState`.
    ReprepareSlot {
        slot: usize,
    },
    /// Loads `plugin` into a new slot at `slot`, at most the slot count, restoring `state` and
    /// taking `bypass` before it goes in, with its host bypass `host_bypass`. The other slots' instances and editors stay, and a
    /// prepared chain is left unprepared. Answered with `Loaded` and every slot's plugin.
    InsertSlot {
        slot: usize,
        #[serde_as(as = "wire::PluginRefDef")]
        plugin: PluginRef,
        #[serde(
            serialize_with = "state::serialize_option",
            deserialize_with = "state::deserialize_option_cow"
        )]
        state: Option<Cow<'a, PluginState>>,
        bypass: Option<bool>,
        host_bypass: bool,
    },
    /// As `InsertSlot`, in place of the slot's plugin, whose editor closes.
    ReplaceSlot {
        slot: usize,
        #[serde_as(as = "wire::PluginRefDef")]
        plugin: PluginRef,
        #[serde(
            serialize_with = "state::serialize_option",
            deserialize_with = "state::deserialize_option_cow"
        )]
        state: Option<Cow<'a, PluginState>>,
        bypass: Option<bool>,
        host_bypass: bool,
    },
    /// Removes the slot and its plugin, whose editor closes; a chain keeps one slot at least.
    /// Answered as `InsertSlot`.
    RemoveSlot {
        slot: usize,
    },
    /// Moves the slot's plugin, with its state and open editor, to `to`; the others keep their
    /// order. Answered as `InsertSlot`.
    MoveSlot {
        from: usize,
        to: usize,
    },
    Reset,
    Timing,
    /// Opens the plugin's editor in a window of the helper, which processes while it is open.
    OpenEditor {
        slot: usize,
    },
    CloseEditor {
        slot: usize,
    },
    /// Whether the editor is still open; the user may have closed its window.
    EditorOpen {
        slot: usize,
    },
    /// Sets where a remote-mode helper shows its remote editors, moving those open there.
    SetRemoteRegion {
        #[serde_as(as = "wire::RegionDef")]
        region: crate::remote::Region,
    },
    /// Opens the slot's editor remotely and shows it, hiding the remote editor shown before.
    /// Answered with `RemoteEditorOpened`; an editor already open is shown again.
    OpenRemoteEditor {
        slot: usize,
    },
    /// Shows the slot's open remote editor, hiding the one shown before.
    ShowRemoteEditor {
        slot: usize,
    },
    /// Starts a remote session on the pipes the transfers name, the read end of the commands'
    /// and then the write end of the notices', ending the one before. The helper first sends the
    /// current state on it, and closes a menu that stays open `menu_timeout_ms` without a choice.
    RemoteSession {
        commands: u64,
        notices: u64,
        menu_timeout_ms: u64,
    },
    /// Installs the realtime ring the transfers `tokens` name, for the prepared chain, replacing
    /// one installed before. Preparing the chain again removes it.
    RealtimeInstall {
        ring: shared::realtime::RingDescriptor,
        tokens: Vec<u64>,
    },
    /// Starts the installed ring's worker at the generation the application stored in the ring.
    RealtimeStart,
    /// Stops the realtime worker between quanta.
    RealtimeStop,
    /// Resynchronizes the running worker: it adopts the generation the application stored in
    /// the ring between quanta, and every slot is reset as by `Reset`. Answered with the timing.
    RealtimeResync,
    Shutdown,
}

impl Request<'_> {
    /// The single plugin target, when this control request has one.
    pub fn slot(&self) -> Option<usize> {
        match self {
            Self::AudioBuses { slot }
            | Self::EventPorts { slot }
            | Self::AudioConfigurations { slot }
            | Self::Bypass { slot }
            | Self::SetBypass { slot, .. }
            | Self::HostBypass { slot }
            | Self::SetHostBypass { slot, .. }
            | Self::ParameterEvents { slot }
            | Self::FactoryPresets { slot }
            | Self::SelectFactoryPreset { slot, .. }
            | Self::LoadDiscoveredPreset { slot, .. }
            | Self::Parameters { slot }
            | Self::Capabilities { slot }
            | Self::SetParameter { slot, .. }
            | Self::ParameterDetails { slot, .. }
            | Self::ParameterChoices { slot, .. }
            | Self::ParameterText { slot, .. }
            | Self::ParameterFromText { slot, .. }
            | Self::ParameterToPlain { slot, .. }
            | Self::ParameterToNormalized { slot, .. }
            | Self::SetParameterText { slot, .. }
            | Self::InspectPreset { slot, .. }
            | Self::ExportPreset { slot }
            | Self::ImportPreset { slot, .. }
            | Self::ReprepareSlot { slot }
            | Self::ReplaceSlot { slot, .. }
            | Self::RemoveSlot { slot }
            | Self::SaveState { slot, .. }
            | Self::RestoreState { slot, .. }
            | Self::LoadState { slot, .. }
            | Self::OpenEditor { slot }
            | Self::CloseEditor { slot }
            | Self::EditorOpen { slot }
            | Self::OpenRemoteEditor { slot }
            | Self::ShowRemoteEditor { slot } => Some(*slot),
            _ => None,
        }
    }
}

/// A message the application sends on a remote session's channel.
#[serde_as]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct CommandMessage(#[serde_as(as = "wire::CommandDef")] pub crate::remote::Command);

/// A message a remote-mode helper sends on a remote session's channel.
#[serde_as]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct NoticeMessage(#[serde_as(as = "wire::NoticeDef")] pub crate::remote::Notice);

#[serde_as]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Response {
    Changes(#[serde_as(as = "wire::ChangeBatchDef")] crate::ChangeBatch),
    AudioBuses(#[serde_as(as = "Vec<wire::AudioBusInfoDef>")] Vec<crate::AudioBusInfo>),
    EventPorts(#[serde_as(as = "Vec<wire::EventPortInfoDef>")] Vec<crate::EventPortInfo>),
    AudioConfigurations(
        #[serde_as(as = "Vec<wire::AudioConfigurationDef>")] Vec<crate::AudioConfiguration>,
    ),
    Bypass(#[serde_as(as = "wire::BypassStateDef")] crate::BypassState),
    AudioPrepared {
        generation: u64,
        #[serde_as(as = "Vec<Vec<wire::AudioBusInfoDef>>")]
        buses: Vec<Vec<crate::AudioBusInfo>>,
        latency: u32,
        #[serde_as(as = "wire::TailDef")]
        tail: Tail,
    },
    ParameterEvents(#[serde_as(as = "wire::ParameterEventBatchDef")] crate::ParameterEventBatch),
    FactoryPresets(#[serde_as(as = "Vec<wire::FactoryPresetDef>")] Vec<crate::FactoryPreset>),
    PresetProviders(
        #[serde_as(as = "Vec<wire::PresetProviderInfoDef>")] Vec<crate::PresetProviderInfo>,
    ),
    DiscoveredPresets(#[serde_as(as = "wire::PresetDiscoveryDef")] crate::PresetDiscovery),
    Diagnostics(#[serde_as(as = "wire::DiagnosticBatchDef")] DiagnosticBatch),
    /// Scan progress: the module loaded.
    ModuleLoaded,
    /// Scan progress: one audio module class.
    Class(#[serde_as(as = "wire::PluginInfoDef")] PluginInfo),
    Loaded(#[serde_as(as = "Vec<wire::PluginInfoDef>")] Vec<PluginInfo>),
    Done,
    /// Host progress: a request that loads or prepares the slots one at a time finished one,
    /// which renews the application's deadline for the rest.
    Progress,
    /// Confirms a published shared output, with the chain timing after the block.
    Processed {
        submission: shared::Submission,
        /// Output events written to the shared output event slots.
        events: usize,
        latency: u32,
        #[serde_as(as = "wire::TailDef")]
        tail: Tail,
    },
    /// Each parameter with its current normalized value.
    Parameters(
        #[serde_as(as = "Vec<(wire::ParameterInfoDef, _)>")] Vec<(ParameterInfo, Option<f64>)>,
    ),
    Capabilities(#[serde_as(as = "wire::CapabilityReportDef")] CapabilityReport),
    ParameterDetails(#[serde_as(as = "wire::ParameterDetailsDef")] crate::ParameterDetails),
    ParameterChoices(#[serde_as(as = "wire::ParameterChoicePageDef")] crate::ParameterChoicePage),
    ParameterText(String),
    ParameterValue(f64),
    State(#[serde(with = "state")] PluginState),
    PresetInfo(#[serde_as(as = "wire::PresetInfoDef")] PresetInfo),
    Preset(Vec<u8>),
    StateRestored {
        #[serde_as(as = "Option<(_, wire::TailDef)>")]
        timing: Option<(u32, Tail)>,
    },
    /// Latency and tail of the whole chain, and whether a plugin announced a latency that is
    /// not applied yet.
    Timing {
        latency: u32,
        #[serde_as(as = "wire::TailDef")]
        tail: Tail,
        latency_changed: bool,
    },
    EditorOpen(bool),
    /// Whether the slot's host bypass is on.
    HostBypass(bool),
    RemoteEditorOpened(#[serde_as(as = "wire::OpenedDef")] crate::remote::Opened),
    /// Invalid caller input; the helper remains usable and no mutation was attempted.
    Rejected {
        slot: Option<usize>,
        #[serde_as(as = "wire::InputErrorDef")]
        error: InputError,
    },
    Failed {
        slot: Option<usize>,
        #[serde_as(as = "wire::FailureDef")]
        failure: Failure,
    },
}

/// Local planar scratch with the same precision as the prepared shared slot.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum AudioSamples {
    F32(Vec<Vec<f32>>),
    F64(Vec<Vec<f64>>),
}

mod framing;
mod state;
mod wire;
pub use framing::{FrameDecoder, MessageReader, MessageWriter, encode_message, write_message};

pub mod shared;

#[cfg(any(test, fuzzing))]
pub mod fuzz;