oximedia-graph 0.1.8

Filter pipeline for OxiMedia
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
//! Node types for the filter graph.
//!
//! Nodes are the processing units in a filter graph. Each node has input and output
//! ports and implements the [`Node`] trait to process frames.

use std::collections::HashMap;
use std::fmt;

use crate::error::{GraphError, GraphResult};
use crate::frame::FilterFrame;
use crate::port::{InputPort, OutputPort, PortId, PortType};

/// Unique identifier for a node in the graph.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
pub struct NodeId(pub u64);

impl fmt::Display for NodeId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Node({})", self.0)
    }
}

/// Type of node in the filter graph.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum NodeType {
    /// Source node that produces frames (e.g., decoder).
    Source,
    /// Filter node that transforms frames.
    Filter,
    /// Sink node that consumes frames (e.g., encoder, display).
    Sink,
}

impl fmt::Display for NodeType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Source => write!(f, "Source"),
            Self::Filter => write!(f, "Filter"),
            Self::Sink => write!(f, "Sink"),
        }
    }
}

/// State of a node during graph execution.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum NodeState {
    /// Node is idle and ready to process.
    #[default]
    Idle,
    /// Node is currently processing.
    Processing,
    /// Node has finished processing (end of stream).
    Done,
    /// Node encountered an error.
    Error,
}

impl fmt::Display for NodeState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Idle => write!(f, "Idle"),
            Self::Processing => write!(f, "Processing"),
            Self::Done => write!(f, "Done"),
            Self::Error => write!(f, "Error"),
        }
    }
}

impl NodeState {
    /// Check if the node can transition to the given state.
    #[must_use]
    #[allow(clippy::match_same_arms)]
    pub fn can_transition_to(&self, new_state: Self) -> bool {
        match (self, new_state) {
            // From Idle - can transition to any state
            (Self::Idle, Self::Processing | Self::Done | Self::Error) => true,
            // From Processing - can transition to any state
            (Self::Processing, Self::Idle | Self::Done | Self::Error) => true,
            // From Done or Error - can only reset to Idle
            (Self::Done | Self::Error, Self::Idle) => true,
            // Same state is always ok
            (a, b) if *a == b => true,
            _ => false,
        }
    }
}

/// Configuration for a node.
#[derive(Clone, Debug, Default)]
pub struct NodeConfig {
    /// Human-readable name for the node.
    pub name: String,
    /// Type of the node.
    pub node_type: Option<NodeType>,
    /// Custom configuration options.
    pub options: HashMap<String, ConfigValue>,
}

impl NodeConfig {
    /// Create a new node configuration with the given name.
    #[must_use]
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            node_type: None,
            options: HashMap::new(),
        }
    }

    /// Set the node type.
    #[must_use]
    pub fn with_type(mut self, node_type: NodeType) -> Self {
        self.node_type = Some(node_type);
        self
    }

    /// Add a configuration option.
    #[must_use]
    pub fn with_option(mut self, key: impl Into<String>, value: ConfigValue) -> Self {
        self.options.insert(key.into(), value);
        self
    }

    /// Get a configuration option.
    #[must_use]
    pub fn get_option(&self, key: &str) -> Option<&ConfigValue> {
        self.options.get(key)
    }

    /// Get an integer option.
    #[must_use]
    pub fn get_int(&self, key: &str) -> Option<i64> {
        self.options.get(key).and_then(ConfigValue::as_int)
    }

    /// Get a float option.
    #[must_use]
    pub fn get_float(&self, key: &str) -> Option<f64> {
        self.options.get(key).and_then(ConfigValue::as_float)
    }

    /// Get a string option.
    #[must_use]
    pub fn get_string(&self, key: &str) -> Option<&str> {
        self.options.get(key).and_then(ConfigValue::as_string)
    }

    /// Get a boolean option.
    #[must_use]
    pub fn get_bool(&self, key: &str) -> Option<bool> {
        self.options.get(key).and_then(ConfigValue::as_bool)
    }
}

/// Configuration value type.
#[derive(Clone, Debug, PartialEq)]
pub enum ConfigValue {
    /// Integer value.
    Int(i64),
    /// Floating point value.
    Float(f64),
    /// String value.
    String(String),
    /// Boolean value.
    Bool(bool),
}

impl ConfigValue {
    /// Get value as integer if possible.
    #[must_use]
    pub fn as_int(&self) -> Option<i64> {
        match self {
            Self::Int(v) => Some(*v),
            _ => None,
        }
    }

    /// Get value as float if possible.
    #[must_use]
    pub fn as_float(&self) -> Option<f64> {
        match self {
            Self::Float(v) => Some(*v),
            Self::Int(v) => Some(*v as f64),
            _ => None,
        }
    }

    /// Get value as string if possible.
    #[must_use]
    pub fn as_string(&self) -> Option<&str> {
        match self {
            Self::String(v) => Some(v),
            _ => None,
        }
    }

    /// Get value as boolean if possible.
    #[must_use]
    pub fn as_bool(&self) -> Option<bool> {
        match self {
            Self::Bool(v) => Some(*v),
            _ => None,
        }
    }
}

impl From<i64> for ConfigValue {
    fn from(v: i64) -> Self {
        Self::Int(v)
    }
}

impl From<f64> for ConfigValue {
    fn from(v: f64) -> Self {
        Self::Float(v)
    }
}

impl From<String> for ConfigValue {
    fn from(v: String) -> Self {
        Self::String(v)
    }
}

impl From<&str> for ConfigValue {
    fn from(v: &str) -> Self {
        Self::String(v.to_string())
    }
}

impl From<bool> for ConfigValue {
    fn from(v: bool) -> Self {
        Self::Bool(v)
    }
}

/// Trait for filter graph nodes.
///
/// Nodes implement this trait to participate in the filter graph.
/// The graph will call [`Node::process`] to push frames through the pipeline.
pub trait Node: Send + Sync {
    /// Get the node's unique identifier.
    fn id(&self) -> NodeId;

    /// Get the node's name.
    fn name(&self) -> &str;

    /// Get the node type.
    fn node_type(&self) -> NodeType;

    /// Get the current state of the node.
    fn state(&self) -> NodeState;

    /// Set the node state.
    fn set_state(&mut self, state: NodeState) -> GraphResult<()>;

    /// Get the node's input ports.
    fn inputs(&self) -> &[InputPort];

    /// Get the node's output ports.
    fn outputs(&self) -> &[OutputPort];

    /// Initialize the node before processing starts.
    fn initialize(&mut self) -> GraphResult<()> {
        Ok(())
    }

    /// Process available input and produce output.
    ///
    /// Returns `Ok(Some(frame))` if a frame was produced, `Ok(None)` if no
    /// frame is ready (need more input), or `Err` on error.
    fn process(&mut self, input: Option<FilterFrame>) -> GraphResult<Option<FilterFrame>>;

    /// Flush any buffered data.
    fn flush(&mut self) -> GraphResult<Vec<FilterFrame>> {
        Ok(Vec::new())
    }

    /// Reset the node to initial state.
    fn reset(&mut self) -> GraphResult<()> {
        self.set_state(NodeState::Idle)
    }

    /// Get input port by ID.
    fn input_port(&self, id: PortId) -> Option<&InputPort> {
        self.inputs().iter().find(|p| p.id == id)
    }

    /// Get output port by ID.
    fn output_port(&self, id: PortId) -> Option<&OutputPort> {
        self.outputs().iter().find(|p| p.id == id)
    }

    /// Check if node accepts the given port type as input.
    fn accepts_input(&self, port_type: PortType) -> bool {
        self.inputs().iter().any(|p| p.port_type == port_type)
    }

    /// Check if node produces the given port type as output.
    fn produces_output(&self, port_type: PortType) -> bool {
        self.outputs().iter().any(|p| p.port_type == port_type)
    }
}

/// Runtime state for a node in the graph.
#[allow(dead_code)]
pub struct NodeRuntime {
    /// The node implementation.
    node: Box<dyn Node>,
    /// Input buffers indexed by port ID.
    input_buffers: HashMap<PortId, Vec<FilterFrame>>,
    /// Output buffers indexed by port ID.
    output_buffers: HashMap<PortId, Vec<FilterFrame>>,
    /// Frames processed count.
    frames_processed: u64,
}

impl NodeRuntime {
    /// Create a new node runtime.
    pub fn new(node: Box<dyn Node>) -> Self {
        let input_buffers = node.inputs().iter().map(|p| (p.id, Vec::new())).collect();
        let output_buffers = node.outputs().iter().map(|p| (p.id, Vec::new())).collect();

        Self {
            node,
            input_buffers,
            output_buffers,
            frames_processed: 0,
        }
    }

    /// Get the underlying node.
    #[must_use]
    pub fn node(&self) -> &dyn Node {
        self.node.as_ref()
    }

    /// Get mutable reference to the underlying node.
    pub fn node_mut(&mut self) -> &mut dyn Node {
        self.node.as_mut()
    }

    /// Push a frame to an input port.
    pub fn push_input(&mut self, port: PortId, frame: FilterFrame) -> GraphResult<()> {
        self.input_buffers
            .get_mut(&port)
            .ok_or(GraphError::PortNotFound {
                node: self.node.id(),
                port,
            })?
            .push(frame);
        Ok(())
    }

    /// Pop a frame from an output port.
    pub fn pop_output(&mut self, port: PortId) -> GraphResult<Option<FilterFrame>> {
        let buffer = self
            .output_buffers
            .get_mut(&port)
            .ok_or(GraphError::PortNotFound {
                node: self.node.id(),
                port,
            })?;

        Ok(if buffer.is_empty() {
            None
        } else {
            Some(buffer.remove(0))
        })
    }

    /// Process the node.
    pub fn process(&mut self) -> GraphResult<()> {
        // Get input frame from first input port if available
        let input = self.input_buffers.values_mut().find_map(|buf| {
            if buf.is_empty() {
                None
            } else {
                Some(buf.remove(0))
            }
        });

        // Process
        if let Some(output) = self.node.process(input)? {
            // Push to first output port
            if let Some(buf) = self.output_buffers.values_mut().next() {
                buf.push(output);
            }
            self.frames_processed += 1;
        }

        Ok(())
    }

    /// Get the number of frames processed.
    #[must_use]
    pub fn frames_processed(&self) -> u64 {
        self.frames_processed
    }
}

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

    #[test]
    fn test_node_id_display() {
        let id = NodeId(42);
        assert_eq!(format!("{id}"), "Node(42)");
    }

    #[test]
    fn test_node_type_display() {
        assert_eq!(format!("{}", NodeType::Source), "Source");
        assert_eq!(format!("{}", NodeType::Filter), "Filter");
        assert_eq!(format!("{}", NodeType::Sink), "Sink");
    }

    #[test]
    fn test_node_state_transitions() {
        let state = NodeState::Idle;
        assert!(state.can_transition_to(NodeState::Processing));
        assert!(state.can_transition_to(NodeState::Done));
        assert!(state.can_transition_to(NodeState::Error));

        let state = NodeState::Processing;
        assert!(state.can_transition_to(NodeState::Idle));
        assert!(state.can_transition_to(NodeState::Done));

        let state = NodeState::Done;
        assert!(state.can_transition_to(NodeState::Idle));
        assert!(!state.can_transition_to(NodeState::Processing));
    }

    #[test]
    fn test_node_config() {
        let config = NodeConfig::new("test")
            .with_type(NodeType::Filter)
            .with_option("quality", ConfigValue::Int(80))
            .with_option("name", ConfigValue::String("test".into()));

        assert_eq!(config.name, "test");
        assert_eq!(config.node_type, Some(NodeType::Filter));
        assert_eq!(config.get_int("quality"), Some(80));
        assert_eq!(config.get_string("name"), Some("test"));
    }

    #[test]
    fn test_config_value_conversions() {
        let int_val = ConfigValue::Int(42);
        assert_eq!(int_val.as_int(), Some(42));
        assert_eq!(int_val.as_float(), Some(42.0));
        assert_eq!(int_val.as_string(), None);

        let float_val = ConfigValue::Float(3.14);
        assert_eq!(float_val.as_float(), Some(3.14));
        assert_eq!(float_val.as_int(), None);

        let str_val = ConfigValue::String("hello".into());
        assert_eq!(str_val.as_string(), Some("hello"));

        let bool_val = ConfigValue::Bool(true);
        assert_eq!(bool_val.as_bool(), Some(true));
    }

    #[test]
    fn test_config_value_from() {
        let _: ConfigValue = 42i64.into();
        let _: ConfigValue = 3.14f64.into();
        let _: ConfigValue = "test".into();
        let _: ConfigValue = String::from("test").into();
        let _: ConfigValue = true.into();
    }
}