vesper-player-plugin 0.5.1

Safe Rust author SDK for Vesper native plugins.
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
use std::collections::VecDeque;

use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::{DecoderFrameFormat, DecoderPcmFrame, DecoderPcmFrameMetadata};

const MAX_AUDIO_PROCESSOR_QUEUE_FRAMES: usize = 256;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AudioProcessorSubmitStatus {
    Accepted,
    Backpressure,
}

/// Pitch behavior requested from an audio processing chain.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AudioPitchMode {
    /// Preserve the original pitch while changing playback speed.
    PreservePitch,
    /// Let pitch follow the playback rate.
    FollowRate,
}

impl AudioPitchMode {
    pub const fn wire_name(self) -> &'static str {
        match self {
            Self::PreservePitch => "preservePitch",
            Self::FollowRate => "followRate",
        }
    }
}

/// Playback policy applied to an audio processor chain.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct AudioPlaybackPolicy {
    pub playback_rate: f32,
    pub pitch_mode: AudioPitchMode,
}

impl AudioPlaybackPolicy {
    pub const fn normal() -> Self {
        Self {
            playback_rate: 1.0,
            pitch_mode: AudioPitchMode::FollowRate,
        }
    }

    pub fn validate(self) -> Result<(), AudioProcessorError> {
        if !self.playback_rate.is_finite() || self.playback_rate <= 0.0 {
            return Err(AudioProcessorError::InvalidPlaybackRate {
                rate: self.playback_rate,
            });
        }
        Ok(())
    }
}

#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct AudioProcessorCapabilities {
    pub accepted_formats: Vec<DecoderFrameFormat>,
    pub output_format: Option<DecoderFrameFormat>,
    pub supports_flush: bool,
    pub max_in_flight_frames: Option<u32>,
    pub playback_rate_min: Option<f32>,
    pub playback_rate_max: Option<f32>,
    pub pitch_modes: Vec<AudioPitchMode>,
}

impl AudioProcessorCapabilities {
    pub fn supports_input_format(&self, format: &DecoderFrameFormat) -> bool {
        self.accepted_formats.is_empty() || self.accepted_formats.iter().any(|item| item == format)
    }

    pub fn supports_playback_policy(&self, policy: AudioPlaybackPolicy) -> bool {
        let rate_supported = self
            .playback_rate_min
            .is_none_or(|minimum| policy.playback_rate >= minimum)
            && self
                .playback_rate_max
                .is_none_or(|maximum| policy.playback_rate <= maximum);
        let pitch_supported =
            self.pitch_modes.is_empty() || self.pitch_modes.contains(&policy.pitch_mode);
        rate_supported && pitch_supported
    }
}

/// Configuration used to open one native audio processor session.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AudioProcessorSessionConfig {
    pub processor_index: usize,
    pub input_metadata: DecoderPcmFrameMetadata,
    pub playback_policy: AudioPlaybackPolicy,
    #[serde(default)]
    pub max_in_flight_frames: Option<u32>,
}

/// Metadata returned after opening one audio processor session.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct AudioProcessorSessionInfo {
    pub processor_name: Option<String>,
    pub selected_backend: Option<String>,
    pub output_format: Option<DecoderFrameFormat>,
    pub max_in_flight_frames: Option<u32>,
}

/// Empty success payload used by configure, flush, and close operations.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct AudioProcessorOperationStatus {
    pub completed: bool,
}

#[derive(Debug, Error, Clone, PartialEq, Serialize, Deserialize)]
pub enum AudioProcessorError {
    #[error(
        "audio processor queue capacity must be between 1 and {MAX_AUDIO_PROCESSOR_QUEUE_FRAMES}"
    )]
    InvalidCapacity,
    #[error("audio processor chain is closed")]
    Closed,
    #[error("invalid PCM frame: {0}")]
    InvalidPcm(String),
    #[error("invalid playback rate: {rate}")]
    InvalidPlaybackRate { rate: f32 },
    #[error("audio processor does not support the requested playback policy")]
    UnsupportedPlaybackPolicy,
    #[error("audio processor payload codec error: {0}")]
    PayloadCodec(String),
    #[error("audio processor ABI violation: {0}")]
    AbiViolation(String),
    #[error("audio processor backpressure: {0}")]
    Backpressure(String),
    #[error("audio processor timeout: {0}")]
    Timeout(String),
    #[error("audio processor failure: {0}")]
    Processor(String),
}

impl AudioProcessorError {
    pub fn payload_codec(message: impl Into<String>) -> Self {
        Self::PayloadCodec(message.into())
    }

    pub fn abi_violation(message: impl Into<String>) -> Self {
        Self::AbiViolation(message.into())
    }
}

/// Safe factory exported by native Rust audio processor plugins.
pub trait AudioProcessorPluginFactory: Send + Sync {
    fn name(&self) -> &str;

    fn capabilities(&self) -> AudioProcessorCapabilities;

    fn open_session(
        &self,
        config: &AudioProcessorSessionConfig,
    ) -> Result<Box<dyn AudioProcessorSession>, AudioProcessorError>;
}

pub trait AudioProcessorSession: Send {
    fn name(&self) -> &str;

    fn capabilities(&self) -> AudioProcessorCapabilities;

    fn session_info(&self) -> AudioProcessorSessionInfo {
        let capabilities = self.capabilities();
        AudioProcessorSessionInfo {
            processor_name: Some(self.name().to_owned()),
            selected_backend: None,
            output_format: capabilities.output_format,
            max_in_flight_frames: capabilities.max_in_flight_frames,
        }
    }

    fn configure(&mut self, _policy: AudioPlaybackPolicy) -> Result<(), AudioProcessorError> {
        Ok(())
    }

    /// Processes one PCM frame while preserving the host-owned PTS and discontinuity marker.
    fn process(&mut self, frame: DecoderPcmFrame) -> Result<DecoderPcmFrame, AudioProcessorError>;

    fn flush(&mut self) -> Result<(), AudioProcessorError>;

    fn close(&mut self) -> Result<(), AudioProcessorError>;
}

pub struct AudioProcessorChain {
    capacity: usize,
    pending: VecDeque<DecoderPcmFrame>,
    processors: Vec<Box<dyn AudioProcessorSession>>,
    playback_policy: AudioPlaybackPolicy,
    closed: bool,
}

impl AudioProcessorChain {
    pub fn new(capacity: usize) -> Result<Self, AudioProcessorError> {
        if capacity == 0 || capacity > MAX_AUDIO_PROCESSOR_QUEUE_FRAMES {
            return Err(AudioProcessorError::InvalidCapacity);
        }
        Ok(Self {
            capacity,
            pending: VecDeque::with_capacity(capacity),
            processors: Vec::new(),
            playback_policy: AudioPlaybackPolicy::normal(),
            closed: false,
        })
    }

    pub fn with_processors(
        capacity: usize,
        processors: Vec<Box<dyn AudioProcessorSession>>,
    ) -> Result<Self, AudioProcessorError> {
        let mut chain = Self::new(capacity)?;
        chain.processors = processors;
        Ok(chain)
    }

    pub fn playback_policy(&self) -> AudioPlaybackPolicy {
        self.playback_policy
    }

    pub fn set_playback_policy(
        &mut self,
        policy: AudioPlaybackPolicy,
    ) -> Result<(), AudioProcessorError> {
        if self.closed {
            return Err(AudioProcessorError::Closed);
        }
        policy.validate()?;
        if self
            .processors
            .iter()
            .any(|processor| !processor.capabilities().supports_playback_policy(policy))
        {
            return Err(AudioProcessorError::UnsupportedPlaybackPolicy);
        }
        for processor in &mut self.processors {
            processor.configure(policy)?;
        }
        self.playback_policy = policy;
        Ok(())
    }

    pub fn submit(
        &mut self,
        frame: DecoderPcmFrame,
    ) -> Result<AudioProcessorSubmitStatus, AudioProcessorError> {
        if self.closed {
            return Err(AudioProcessorError::Closed);
        }
        frame
            .validate()
            .map_err(|error| AudioProcessorError::InvalidPcm(error.to_string()))?;
        if self.pending.len() >= self.capacity {
            return Ok(AudioProcessorSubmitStatus::Backpressure);
        }
        let mut processed = frame;
        for processor in &mut self.processors {
            let input_pts_us = processed.metadata.pts_us;
            let input_discontinuity = processed.metadata.discontinuity;
            let output = processor.process(processed)?;
            output.validate().map_err(|error| {
                AudioProcessorError::abi_violation(format!(
                    "processor returned invalid PCM: {error}"
                ))
            })?;
            if output.metadata.pts_us != input_pts_us {
                return Err(AudioProcessorError::abi_violation(
                    "processor changed the host-owned PCM presentation timestamp",
                ));
            }
            if output.metadata.discontinuity != input_discontinuity {
                return Err(AudioProcessorError::abi_violation(
                    "processor changed the host-owned PCM discontinuity marker",
                ));
            }
            processed = output;
        }
        self.pending.push_back(processed);
        Ok(AudioProcessorSubmitStatus::Accepted)
    }

    pub fn receive(&mut self) -> Result<Option<DecoderPcmFrame>, AudioProcessorError> {
        if self.closed {
            return Err(AudioProcessorError::Closed);
        }
        Ok(self.pending.pop_front())
    }

    pub fn flush(&mut self) -> Result<(), AudioProcessorError> {
        if self.closed {
            return Err(AudioProcessorError::Closed);
        }
        self.pending.clear();
        for processor in &mut self.processors {
            processor.flush()?;
        }
        Ok(())
    }

    pub fn close(&mut self) -> Result<(), AudioProcessorError> {
        if !self.closed {
            self.pending.clear();
            let mut first_error = None;
            for processor in self.processors.iter_mut().rev() {
                if let Err(error) = processor.close()
                    && first_error.is_none()
                {
                    first_error = Some(error);
                }
            }
            self.closed = true;
            if let Some(error) = first_error {
                return Err(error);
            }
        }
        Ok(())
    }

    pub fn queue_depth(&self) -> usize {
        self.pending.len()
    }
}

#[cfg(test)]
mod tests {
    use super::{
        AudioPitchMode, AudioPlaybackPolicy, AudioProcessorCapabilities, AudioProcessorChain,
        AudioProcessorError, AudioProcessorSession, AudioProcessorSubmitStatus,
    };
    use crate::{
        DecoderFrameFormat, DecoderPcmFrame, DecoderPcmFrameMetadata, DecoderPcmSampleLayout,
    };

    fn frame() -> DecoderPcmFrame {
        let metadata = DecoderPcmFrameMetadata::audio(
            "aac",
            DecoderFrameFormat::F32,
            48_000,
            2,
            DecoderPcmSampleLayout::Interleaved,
            2,
        );
        DecoderPcmFrame {
            metadata,
            data: vec![0; 16],
        }
    }

    #[test]
    fn audio_chain_is_bounded_and_flushes_pending_output() {
        let mut chain = AudioProcessorChain::new(1).expect("bounded chain");
        assert_eq!(
            chain.submit(frame()).unwrap(),
            AudioProcessorSubmitStatus::Accepted
        );
        assert_eq!(
            chain.submit(frame()).unwrap(),
            AudioProcessorSubmitStatus::Backpressure
        );
        chain.flush().expect("flush chain");
        assert!(chain.receive().unwrap().is_none());
    }

    struct AddOneProcessor;

    impl AudioProcessorSession for AddOneProcessor {
        fn name(&self) -> &str {
            "add-one"
        }

        fn capabilities(&self) -> AudioProcessorCapabilities {
            AudioProcessorCapabilities {
                accepted_formats: vec![DecoderFrameFormat::F32],
                output_format: Some(DecoderFrameFormat::F32),
                supports_flush: true,
                max_in_flight_frames: Some(1),
                playback_rate_min: Some(0.5),
                playback_rate_max: Some(2.0),
                pitch_modes: vec![AudioPitchMode::PreservePitch, AudioPitchMode::FollowRate],
            }
        }

        fn process(
            &mut self,
            mut frame: DecoderPcmFrame,
        ) -> Result<DecoderPcmFrame, AudioProcessorError> {
            frame.data[0] = frame.data[0].saturating_add(1);
            Ok(frame)
        }

        fn flush(&mut self) -> Result<(), AudioProcessorError> {
            Ok(())
        }

        fn close(&mut self) -> Result<(), AudioProcessorError> {
            Ok(())
        }
    }

    #[test]
    fn audio_chain_applies_processors_in_linear_order() {
        let mut chain = AudioProcessorChain::with_processors(
            2,
            vec![Box::new(AddOneProcessor), Box::new(AddOneProcessor)],
        )
        .expect("processor chain");
        let mut input = frame();
        input.data[0] = 0;
        assert_eq!(
            chain.submit(input).unwrap(),
            AudioProcessorSubmitStatus::Accepted
        );
        assert_eq!(chain.receive().unwrap().expect("output").data[0], 2);
        chain.close().expect("close chain");
    }

    #[test]
    fn audio_chain_rejects_invalid_or_unsupported_playback_policy() {
        let mut chain = AudioProcessorChain::with_processors(2, vec![Box::new(AddOneProcessor)])
            .expect("processor chain");
        assert!(matches!(
            chain.set_playback_policy(AudioPlaybackPolicy {
                playback_rate: 0.0,
                pitch_mode: AudioPitchMode::FollowRate,
            }),
            Err(AudioProcessorError::InvalidPlaybackRate { .. })
        ));
        assert_eq!(chain.playback_policy(), AudioPlaybackPolicy::normal());

        let mut chain = AudioProcessorChain::new(2).expect("processor chain");
        let mut processor = AddOneProcessor;
        processor
            .configure(AudioPlaybackPolicy::normal())
            .expect("default policy");
        chain
            .set_playback_policy(AudioPlaybackPolicy {
                playback_rate: 1.5,
                pitch_mode: AudioPitchMode::PreservePitch,
            })
            .expect("empty chain accepts policy");
    }

    struct FollowRateOnlyProcessor;

    impl AudioProcessorSession for FollowRateOnlyProcessor {
        fn name(&self) -> &str {
            "follow-rate-only"
        }

        fn capabilities(&self) -> AudioProcessorCapabilities {
            AudioProcessorCapabilities {
                accepted_formats: vec![DecoderFrameFormat::F32],
                output_format: Some(DecoderFrameFormat::F32),
                supports_flush: true,
                max_in_flight_frames: Some(1),
                playback_rate_min: Some(0.5),
                playback_rate_max: Some(2.0),
                pitch_modes: vec![AudioPitchMode::FollowRate],
            }
        }

        fn process(
            &mut self,
            frame: DecoderPcmFrame,
        ) -> Result<DecoderPcmFrame, AudioProcessorError> {
            Ok(frame)
        }

        fn flush(&mut self) -> Result<(), AudioProcessorError> {
            Ok(())
        }

        fn close(&mut self) -> Result<(), AudioProcessorError> {
            Ok(())
        }
    }

    #[test]
    fn audio_chain_rejects_rate_and_pitch_modes_outside_processor_capabilities() {
        let mut chain =
            AudioProcessorChain::with_processors(2, vec![Box::new(FollowRateOnlyProcessor)])
                .expect("processor chain");
        assert_eq!(
            chain.set_playback_policy(AudioPlaybackPolicy {
                playback_rate: 2.5,
                pitch_mode: AudioPitchMode::FollowRate,
            }),
            Err(AudioProcessorError::UnsupportedPlaybackPolicy)
        );
        assert_eq!(
            chain.set_playback_policy(AudioPlaybackPolicy {
                playback_rate: 1.5,
                pitch_mode: AudioPitchMode::PreservePitch,
            }),
            Err(AudioProcessorError::UnsupportedPlaybackPolicy)
        );
    }

    struct TimestampMutatingProcessor {
        pts_us: Option<i64>,
    }

    impl AudioProcessorSession for TimestampMutatingProcessor {
        fn name(&self) -> &str {
            "timestamp-mutator"
        }

        fn capabilities(&self) -> AudioProcessorCapabilities {
            AudioProcessorCapabilities::default()
        }

        fn process(
            &mut self,
            mut frame: DecoderPcmFrame,
        ) -> Result<DecoderPcmFrame, AudioProcessorError> {
            frame.metadata.pts_us = self.pts_us;
            Ok(frame)
        }

        fn flush(&mut self) -> Result<(), AudioProcessorError> {
            Ok(())
        }

        fn close(&mut self) -> Result<(), AudioProcessorError> {
            Ok(())
        }
    }

    #[test]
    fn audio_chain_rejects_negative_or_mutated_host_owned_timestamps() {
        for mutated_pts in [Some(-1), Some(1_001)] {
            let mut input = frame();
            input.metadata.pts_us = Some(1_000);
            let mut chain = AudioProcessorChain::with_processors(
                1,
                vec![Box::new(TimestampMutatingProcessor {
                    pts_us: mutated_pts,
                })],
            )
            .expect("processor chain");

            assert!(matches!(
                chain.submit(input),
                Err(AudioProcessorError::AbiViolation(message))
                    if message.contains("presentation timestamp")
            ));
            assert_eq!(chain.queue_depth(), 0);
        }
    }
}