styx 0.1.0

Sync-first, zero-copy capture→decode→process→encode media stack.
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
use crate::{BackendKind, ProbedBackend, ProbedDevice};
use styx_capture::prelude::*;

use super::handle::start_backend;

/// Errors starting a capture session.
///
/// # Example
/// ```rust,ignore
/// use styx::prelude::*;
///
/// let device = probe_all().into_iter().next().expect("device");
/// let err = CaptureRequest::new(&device)
///     .backend(BackendKind::Virtual)
///     .start()
///     .err()
///     .expect("error");
/// eprintln!("capture failed: {} ({})", err, err.code());
/// ```
#[derive(Debug, thiserror::Error)]
pub enum CaptureError {
    #[error("device has no backends")]
    NoBackend,
    #[error("backend {0:?} not available on this device")]
    BackendUnavailable(BackendKind),
    #[error("backend {0:?} not implemented in this build")]
    BackendMissing(BackendKind),
    #[error("no modes advertised by backend")]
    NoModes,
    #[error("mode {0:?} not advertised by backend")]
    InvalidMode(ModeId),
    #[error("capture config rejected: {0}")]
    InvalidConfig(String),
    #[error("backend {0:?} capture not implemented yet")]
    NotImplemented(BackendKind),
    #[error("control plane not available for backend")]
    ControlUnsupported,
    #[error("control apply failed: {0}")]
    ControlApply(String),
    #[error("backend error: {0}")]
    Backend(String),
}

impl CaptureError {
    /// Stable string code for error classification.
    pub fn code(&self) -> &'static str {
        match self {
            CaptureError::NoBackend => "no_backend",
            CaptureError::BackendUnavailable(_) => "backend_unavailable",
            CaptureError::BackendMissing(_) => "backend_missing",
            CaptureError::NoModes => "no_modes",
            CaptureError::InvalidMode(_) => "invalid_mode",
            CaptureError::InvalidConfig(_) => "invalid_config",
            CaptureError::NotImplemented(_) => "not_implemented",
            CaptureError::ControlUnsupported => "control_unsupported",
            CaptureError::ControlApply(_) => "control_apply_failed",
            CaptureError::Backend(_) => "backend_error",
        }
    }

    /// Whether the error may succeed when retried.
    pub fn retryable(&self) -> bool {
        matches!(self, CaptureError::BackendUnavailable(_) | CaptureError::Backend(_))
    }
}

/// Builder for starting capture with backend/mode/controls validated ahead of time.
///
/// # Example
/// ```rust,ignore
/// use styx::prelude::*;
///
/// let device = probe_all().into_iter().next().expect("device");
/// let handle = CaptureRequest::new(&device)
///     .backend_preferred(Some(BackendKind::V4l2))
///     .start()?;
/// let _ = handle.recv();
/// # Ok::<(), styx::capture_api::CaptureError>(())
/// ```
pub struct CaptureRequest<'a> {
    device: &'a ProbedDevice,
    backend: Option<BackendKind>,
    mode: Option<ModeId>,
    interval: Option<Interval>,
    controls: Vec<(ControlId, ControlValue)>,
    enable_tdn_output: bool,
}

impl<'a> CaptureRequest<'a> {
    /// Create a new request targeting a probed device.
    pub fn new(device: &'a ProbedDevice) -> Self {
        Self {
            device,
            backend: None,
            mode: None,
            interval: None,
            controls: Vec::new(),
            enable_tdn_output: false,
        }
    }

    /// Pin to a backend kind.
    ///
    /// If the backend is missing/unavailable, `start` returns an error.
    pub fn backend(mut self, kind: BackendKind) -> Self {
        self.backend = Some(kind);
        self
    }

    /// Apply defaults for an optional preferred backend.
    ///
    /// Pass `None` to select the first available backend.
    pub fn backend_preferred(mut self, kind: Option<BackendKind>) -> Self {
        self.backend = kind;
        self
    }

    /// Pin to a specific mode id.
    ///
    /// Use the `ModeId` from a probed backend descriptor.
    pub fn mode(mut self, mode: ModeId) -> Self {
        self.mode = Some(mode);
        self
    }

    /// Pin to a specific interval (must exist in the chosen mode).
    ///
    /// If a backend does not advertise intervals, validation is relaxed.
    pub fn interval(mut self, interval: Interval) -> Self {
        self.interval = Some(interval);
        self
    }

    /// Queue a control assignment to apply before streaming.
    pub fn control(mut self, id: ControlId, value: ControlValue) -> Self {
        self.controls.push((id, value));
        self
    }

    /// Request a dedicated TDN output stream (libcamera PiSP).
    ///
    /// Requires the `libcamera` backend and hardware support.
    pub fn enable_tdn_output(mut self, enable: bool) -> Self {
        self.enable_tdn_output = enable;
        self
    }

    /// Start capture after validating backend/mode/interval/controls.
    ///
    /// Returns a running `CaptureHandle` that can receive frames.
    pub fn start(self) -> Result<super::handle::CaptureHandle, CaptureError> {
        let backend = pick_backend(self.device, self.backend)?;
        let mode = pick_mode(backend, self.mode)?;
        validate_config(backend, mode, self.interval, &self.controls)?;
        let interval = self.interval.or_else(|| default_interval(mode));
        start_backend(backend, mode.clone(), interval, self.controls, self.enable_tdn_output)
    }
}

/// Start capture on the preferred backend (or first available), returning a handle.
///
/// # Example
/// ```rust,ignore
/// use styx::prelude::*;
///
/// let device = probe_all().into_iter().next().expect("device");
/// let handle = start_capture(&device, None)?;
/// let _ = handle.recv();
/// # Ok::<(), styx::capture_api::CaptureError>(())
/// ```
pub fn start_capture(
    device: &ProbedDevice,
    preferred: Option<BackendKind>,
) -> Result<super::handle::CaptureHandle, CaptureError> {
    CaptureRequest::new(device)
        .backend_preferred(preferred)
        .start()
}

fn pick_backend(
    device: &ProbedDevice,
    preferred: Option<BackendKind>,
) -> Result<&ProbedBackend, CaptureError> {
    if device.backends.is_empty() {
        return Err(CaptureError::NoBackend);
    }
    if let Some(kind) = preferred {
        device
            .backends
            .iter()
            .find(|b| b.kind == kind)
            .ok_or(CaptureError::BackendUnavailable(kind))
    } else {
        Ok(&device.backends[0])
    }
}

fn pick_mode(backend: &ProbedBackend, mode: Option<ModeId>) -> Result<&Mode, CaptureError> {
    if backend.descriptor.modes.is_empty() {
        return Err(CaptureError::NoModes);
    }
    if let Some(id) = mode {
        let requested = &id.format;
        let is_bayer = requested.code == FourCc::new(*b"RGGB")
            || requested.code == FourCc::new(*b"BGGR")
            || requested.code == FourCc::new(*b"GBRG")
            || requested.code == FourCc::new(*b"GRBG");

        // Prefer an exact ModeId match, then exact MediaFormat matches.
        if let Some(found) = backend.descriptor.modes.iter().find(|m| m.id == id) {
            return Ok(found);
        }
        if let Some(found) = backend
            .descriptor
            .modes
            .iter()
            .find(|m| m.id.format == *requested || m.format == *requested)
        {
            return Ok(found);
        }

        // Fall back to matching by code+resolution, relaxing color-space when either side is
        // Unknown (or for raw Bayer formats where color-space is not a meaningful selector).
        backend
            .descriptor
            .modes
            .iter()
            .find(|m| {
                let advertised_id = &m.id.format;
                let advertised_format = &m.format;

                let matches_id = advertised_id.code == requested.code
                    && advertised_id.resolution == requested.resolution;
                let matches_format = advertised_format.code == requested.code
                    && advertised_format.resolution == requested.resolution;
                if !matches_id && !matches_format {
                    return false;
                }

                let advertised_color = if matches_id {
                    advertised_id.color
                } else {
                    advertised_format.color
                };
                advertised_color == requested.color
                    || advertised_color == ColorSpace::Unknown
                    || requested.color == ColorSpace::Unknown
                    || is_bayer
            })
            .ok_or(CaptureError::InvalidMode(id))
    } else {
        Ok(&backend.descriptor.modes[0])
    }
}

#[cfg(test)]
#[allow(clippy::items_after_test_module)]
mod tests {
    use super::*;
    use crate::BackendHandle;

    #[test]
    fn pick_mode_ignores_color_when_unknown() {
        let fmt_advertised = MediaFormat::new(
            FourCc::new(*b"RGGB"),
            Resolution::new(1280, 800).unwrap(),
            ColorSpace::Unknown,
        );
        let fmt_requested = MediaFormat::new(
            FourCc::new(*b"RGGB"),
            Resolution::new(1280, 800).unwrap(),
            ColorSpace::Bt709,
        );
        let advertised_mode = Mode {
            id: ModeId {
                format: fmt_advertised,
                interval: None,
            },
            format: fmt_advertised,
            intervals: smallvec::smallvec![],
            interval_stepwise: None,
        };
        let backend = ProbedBackend {
            kind: BackendKind::Virtual,
            handle: BackendHandle::Virtual,
            descriptor: CaptureDescriptor {
                modes: vec![advertised_mode.clone()],
                controls: vec![],
            },
            properties: vec![],
        };

        let requested_id = ModeId {
            format: fmt_requested,
            interval: None,
        };
        let picked = pick_mode(&backend, Some(requested_id)).expect("pick");
        assert_eq!(picked.id.format.code, FourCc::new(*b"RGGB"));
        assert_eq!(picked.id.format.resolution.width.get(), 1280);
        assert_eq!(picked.id.format.resolution.height.get(), 800);
    }

    #[test]
    fn pick_mode_accepts_mode_format_when_id_format_differs() {
        let fmt_id = MediaFormat::new(
            FourCc::new(*b"RGGB"),
            Resolution::new(1280, 800).unwrap(),
            ColorSpace::Unknown,
        );
        let fmt_mode = MediaFormat::new(
            FourCc::new(*b"RGGB"),
            Resolution::new(1280, 800).unwrap(),
            ColorSpace::Srgb,
        );
        let advertised_mode = Mode {
            id: ModeId {
                format: fmt_id,
                interval: None,
            },
            format: fmt_mode,
            intervals: smallvec::smallvec![],
            interval_stepwise: None,
        };
        let backend = ProbedBackend {
            kind: BackendKind::Virtual,
            handle: BackendHandle::Virtual,
            descriptor: CaptureDescriptor {
                modes: vec![advertised_mode.clone()],
                controls: vec![],
            },
            properties: vec![],
        };

        let requested = ModeId {
            format: fmt_mode,
            interval: None,
        };
        let picked = pick_mode(&backend, Some(requested)).expect("pick");
        assert_eq!(picked.format.color, ColorSpace::Srgb);
    }

    #[test]
    fn pick_mode_relaxes_color_for_bayer() {
        let fmt_advertised = MediaFormat::new(
            FourCc::new(*b"RGGB"),
            Resolution::new(1280, 800).unwrap(),
            ColorSpace::Bt709,
        );
        let fmt_requested = MediaFormat::new(
            FourCc::new(*b"RGGB"),
            Resolution::new(1280, 800).unwrap(),
            ColorSpace::Srgb,
        );
        let advertised_mode = Mode {
            id: ModeId {
                format: fmt_advertised,
                interval: None,
            },
            format: fmt_advertised,
            intervals: smallvec::smallvec![],
            interval_stepwise: None,
        };
        let backend = ProbedBackend {
            kind: BackendKind::Virtual,
            handle: BackendHandle::Virtual,
            descriptor: CaptureDescriptor {
                modes: vec![advertised_mode.clone()],
                controls: vec![],
            },
            properties: vec![],
        };

        let requested_id = ModeId {
            format: fmt_requested,
            interval: None,
        };
        let picked = pick_mode(&backend, Some(requested_id)).expect("pick");
        assert_eq!(picked.id.format.code, FourCc::new(*b"RGGB"));
    }
}

fn default_interval(mode: &Mode) -> Option<Interval> {
    mode.intervals
        .first()
        .copied()
        .or_else(|| mode.interval_stepwise.map(|s| s.min))
}

fn validate_config(
    backend: &ProbedBackend,
    mode: &Mode,
    interval: Option<Interval>,
    controls: &[(ControlId, ControlValue)],
) -> Result<(), CaptureError> {
    // Some backends (notably libcamera) do not provide enumerated interval lists even though they
    // can honor a requested frame duration via controls. When a mode advertises no intervals and
    // no stepwise descriptor, treat interval pinning as "best effort" and validate everything
    // else against the descriptor.
    let interval_for_validation =
        if interval.is_some() && mode.intervals.is_empty() && mode.interval_stepwise.is_none() {
            None
        } else {
            interval
        };
    let cfg = CaptureConfig {
        mode: mode.id.clone(),
        interval: interval_for_validation,
        controls: controls.to_vec(),
    };
    cfg.validate(&backend.descriptor)
        .map_err(CaptureError::InvalidConfig)
}