gst-plugin-zenoh 0.3.2

High-performance GStreamer plugin for distributed media streaming using Zenoh protocol
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, LazyLock, Mutex};
use std::time::Duration;

use gst::{glib, prelude::*, subclass::prelude::*};
use zenoh::Wait;

use crate::error::{ErrorHandling, ZenohError};
use crate::metadata::MetadataParser;

// Define debug category for logging
static CAT: LazyLock<gst::DebugCategory> = LazyLock::new(|| {
    gst::DebugCategory::new(
        "zenohdemux",
        gst::DebugColorFlags::empty(),
        Some("Zenoh Demux"),
    )
});

/// How to derive pad names from key expressions
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, glib::Enum)]
#[enum_type(name = "GstZenohDemuxPadNaming")]
#[repr(u32)]
pub enum PadNaming {
    /// Use full key expression: "camera/front" -> "camera_front"
    #[default]
    #[enum_value(name = "Full Path", nick = "full-path")]
    FullPath = 0,
    /// Use last segment only: "camera/front" -> "front"
    #[enum_value(name = "Last Segment", nick = "last-segment")]
    LastSegment = 1,
    /// Use hash of key expression: "camera/front" -> "pad_a1b2c3"
    #[enum_value(name = "Hash", nick = "hash")]
    Hash = 2,
}

/// Statistics tracking for ZenohDemux
#[derive(Debug, Clone, Default)]
struct Statistics {
    bytes_received: u64,
    messages_received: u64,
    pads_created: u64,
    errors: u64,
}

struct Started {
    // Keep session alive for the duration of the element
    _session: zenoh::Session,
    // Keep subscriber alive (actual receiving is done in thread with its own subscriber)
    _subscriber:
        zenoh::pubsub::Subscriber<zenoh::handlers::FifoChannelHandler<zenoh::sample::Sample>>,
    /// Flag to signal that the element is stopping
    stopping: Arc<AtomicBool>,
    /// Statistics tracking
    stats: Arc<Mutex<Statistics>>,
    /// Map of key expression -> source pad
    pads: Arc<Mutex<HashMap<String, gst::Pad>>>,
    /// Receiver thread handle
    thread_handle: Option<std::thread::JoinHandle<()>>,
}

#[derive(Default)]
enum State {
    #[default]
    Stopped,
    Started(Started),
}

/// Configuration settings for the ZenohDemux element.
#[derive(Debug)]
struct Settings {
    /// Zenoh key expression for subscribing (supports wildcards)
    key_expr: String,
    /// Optional path to Zenoh configuration file
    config_file: Option<String>,
    /// How to name pads from key expressions
    pad_naming: PadNaming,
    /// Receive timeout in milliseconds
    receive_timeout_ms: u64,
    /// Session group name for sharing sessions via property (gst-launch compatible)
    session_group: Option<String>,
}

impl Default for Settings {
    fn default() -> Self {
        Self {
            key_expr: String::new(),
            config_file: None,
            pad_naming: PadNaming::FullPath,
            receive_timeout_ms: 100,
            session_group: None,
        }
    }
}

/// Convert a key expression to a valid GStreamer pad name
fn key_expr_to_pad_name(key_expr: &str, naming: PadNaming) -> String {
    match naming {
        PadNaming::FullPath => {
            // Replace invalid characters with underscores
            key_expr
                .replace('/', "_")
                .replace('*', "wildcard")
                .replace(' ', "_")
        }
        PadNaming::LastSegment => {
            // Use only the last segment of the key expression
            key_expr
                .split('/')
                .next_back()
                .unwrap_or("unknown")
                .replace('*', "wildcard")
                .replace(' ', "_")
        }
        PadNaming::Hash => {
            // Use a hash of the key expression
            use std::collections::hash_map::DefaultHasher;
            use std::hash::{Hash, Hasher};
            let mut hasher = DefaultHasher::new();
            key_expr.hash(&mut hasher);
            format!("pad_{:x}", hasher.finish() & 0xFFFFFF)
        }
    }
}

/// GStreamer ZenohDemux element implementation.
pub struct ZenohDemux {
    settings: Mutex<Settings>,
    state: Mutex<State>,
}

impl Default for ZenohDemux {
    fn default() -> Self {
        Self {
            settings: Mutex::new(Settings::default()),
            state: Mutex::new(State::default()),
        }
    }
}

impl GstObjectImpl for ZenohDemux {}

impl ElementImpl for ZenohDemux {
    fn metadata() -> Option<&'static gst::subclass::ElementMetadata> {
        static ELEMENT_METADATA: LazyLock<gst::subclass::ElementMetadata> = LazyLock::new(|| {
            gst::subclass::ElementMetadata::new(
                "Zenoh Stream Demultiplexer",
                "Demuxer/Network/Protocol",
                "Demultiplexes Zenoh streams by key expression, creating dynamic pads for each unique key",
                "Marc Pardo <p13marc@gmail.com>",
            )
        });
        Some(&*ELEMENT_METADATA)
    }

    fn pad_templates() -> &'static [gst::PadTemplate] {
        static PAD_TEMPLATES: LazyLock<Vec<gst::PadTemplate>> = LazyLock::new(|| {
            // Dynamic source pads - created on demand
            let src_template = gst::PadTemplate::new(
                "src_%s",
                gst::PadDirection::Src,
                gst::PadPresence::Sometimes,
                &gst::Caps::new_any(),
            )
            .unwrap();

            vec![src_template]
        });

        PAD_TEMPLATES.as_ref()
    }

    fn change_state(
        &self,
        transition: gst::StateChange,
    ) -> Result<gst::StateChangeSuccess, gst::StateChangeError> {
        gst::debug!(CAT, imp = self, "State change: {:?}", transition);

        match transition {
            gst::StateChange::NullToReady => {
                // Validate settings before starting
                let settings = self.settings.lock().unwrap();
                if settings.key_expr.is_empty() {
                    gst::element_imp_error!(
                        self,
                        gst::ResourceError::Settings,
                        ["Key expression is required"]
                    );
                    return Err(gst::StateChangeError);
                }
            }
            gst::StateChange::ReadyToPaused => {
                if let Err(e) = self.start() {
                    gst::element_imp_error!(
                        self,
                        gst::ResourceError::OpenRead,
                        ["Failed to start: {}", e]
                    );
                    return Err(gst::StateChangeError);
                }
            }
            gst::StateChange::PausedToReady => {
                self.stop();
            }
            _ => {}
        }

        self.parent_change_state(transition)
    }
}

impl ObjectImpl for ZenohDemux {
    fn properties() -> &'static [glib::ParamSpec] {
        static PROPERTIES: LazyLock<Vec<glib::ParamSpec>> = LazyLock::new(|| {
            vec![
                glib::ParamSpecString::builder("key-expr")
                    .nick("Zenoh Key Expression")
                    .blurb("Zenoh key expression for subscribing. Use wildcards (* or **) to match multiple streams.")
                    .build(),
                glib::ParamSpecString::builder("config")
                    .nick("Zenoh Configuration")
                    .blurb("Path to Zenoh configuration file (JSON5 format)")
                    .build(),
                glib::ParamSpecEnum::builder_with_default("pad-naming", PadNaming::FullPath)
                    .nick("Pad Naming Strategy")
                    .blurb("How to derive pad names from key expressions")
                    .build(),
                glib::ParamSpecUInt64::builder("receive-timeout-ms")
                    .nick("Receive Timeout")
                    .blurb("Timeout in milliseconds for polling Zenoh subscriber")
                    .default_value(100)
                    .minimum(10)
                    .maximum(5000)
                    .build(),
                // Session sharing property
                glib::ParamSpecString::builder("session-group")
                    .nick("Session Group")
                    .blurb("Name of the session group for sharing Zenoh sessions across elements. Elements with the same group name share a single session.")
                    .build(),
                // Statistics (read-only)
                glib::ParamSpecUInt64::builder("bytes-received")
                    .nick("Bytes Received")
                    .blurb("Total bytes received since element started")
                    .read_only()
                    .build(),
                glib::ParamSpecUInt64::builder("messages-received")
                    .nick("Messages Received")
                    .blurb("Total messages received since element started")
                    .read_only()
                    .build(),
                glib::ParamSpecUInt64::builder("pads-created")
                    .nick("Pads Created")
                    .blurb("Number of dynamic pads created")
                    .read_only()
                    .build(),
            ]
        });

        PROPERTIES.as_ref()
    }

    fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
        let mut settings = self.settings.lock().unwrap();

        match pspec.name() {
            "key-expr" => {
                settings.key_expr = value.get::<String>().expect("type checked upstream");
            }
            "config" => {
                settings.config_file = value
                    .get::<Option<String>>()
                    .expect("type checked upstream");
            }
            "pad-naming" => {
                settings.pad_naming = value.get::<PadNaming>().expect("type checked upstream");
            }
            "receive-timeout-ms" => {
                settings.receive_timeout_ms = value.get::<u64>().expect("type checked upstream");
            }
            "session-group" => {
                settings.session_group = value
                    .get::<Option<String>>()
                    .expect("type checked upstream");
            }
            name => {
                gst::warning!(CAT, imp = self, "Unknown property: {}", name);
            }
        }
    }

    fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
        match pspec.name() {
            "key-expr" => self.settings.lock().unwrap().key_expr.to_value(),
            "config" => self.settings.lock().unwrap().config_file.to_value(),
            "pad-naming" => self.settings.lock().unwrap().pad_naming.to_value(),
            "receive-timeout-ms" => self.settings.lock().unwrap().receive_timeout_ms.to_value(),
            "session-group" => self.settings.lock().unwrap().session_group.to_value(),
            "bytes-received" => {
                let state = self.state.lock().unwrap();
                if let State::Started(ref started) = *state {
                    started.stats.lock().unwrap().bytes_received.to_value()
                } else {
                    0u64.to_value()
                }
            }
            "messages-received" => {
                let state = self.state.lock().unwrap();
                if let State::Started(ref started) = *state {
                    started.stats.lock().unwrap().messages_received.to_value()
                } else {
                    0u64.to_value()
                }
            }
            "pads-created" => {
                let state = self.state.lock().unwrap();
                if let State::Started(ref started) = *state {
                    started.stats.lock().unwrap().pads_created.to_value()
                } else {
                    0u64.to_value()
                }
            }
            name => {
                gst::warning!(CAT, imp = self, "Unknown property: {}", name);
                "".to_value()
            }
        }
    }
}

#[glib::object_subclass]
impl ObjectSubclass for ZenohDemux {
    const NAME: &'static str = "GstZenohDemux";
    type Type = super::ZenohDemux;
    type ParentType = gst::Element;
}

impl ZenohDemux {
    fn start(&self) -> Result<(), gst::ErrorMessage> {
        let mut state = self.state.lock().unwrap();
        if matches!(*state, State::Started(_)) {
            return Ok(());
        }

        let settings = self.settings.lock().unwrap();
        let key_expr = settings.key_expr.clone();
        let config_file = settings.config_file.clone();
        let pad_naming = settings.pad_naming;
        let receive_timeout_ms = settings.receive_timeout_ms;
        let session_group = settings.session_group.clone();
        drop(settings);

        // Determine session source: session-group (property) > new session
        let session = if let Some(ref group) = session_group {
            // Use session group (gst-launch compatible)
            gst::debug!(CAT, imp = self, "Using session group '{}'", group);
            crate::session::get_or_create_session(group, config_file.as_deref())
                .map_err(|e| ZenohError::Init(e).to_error_message())?
        } else {
            // Create a new session
            gst::debug!(CAT, imp = self, "Creating new Zenoh session");
            let config = match config_file {
                Some(path) if !path.is_empty() => {
                    gst::debug!(CAT, imp = self, "Loading Zenoh config from {}", path);
                    zenoh::Config::from_file(&path)
                        .map_err(|e| ZenohError::Init(e).to_error_message())?
                }
                _ => zenoh::Config::default(),
            };
            zenoh::open(config)
                .wait()
                .map_err(|e| ZenohError::Init(e).to_error_message())?
        };

        gst::debug!(
            CAT,
            imp = self,
            "Creating subscriber with key_expr='{}'",
            key_expr
        );

        // Create subscriber for the receiver thread
        let subscriber_for_thread = session
            .declare_subscriber(&key_expr)
            .wait()
            .map_err(|e| ZenohError::Init(e).to_error_message())?;

        // Create another subscriber to keep in state (for potential future use)
        let subscriber_for_state = session
            .declare_subscriber(&key_expr)
            .wait()
            .map_err(|e| ZenohError::Init(e).to_error_message())?;

        let stopping = Arc::new(AtomicBool::new(false));
        let stats = Arc::new(Mutex::new(Statistics::default()));
        let pads: Arc<Mutex<HashMap<String, gst::Pad>>> = Arc::new(Mutex::new(HashMap::new()));

        // Clone for the receiver thread
        let stopping_clone = stopping.clone();
        let stats_clone = stats.clone();
        let pads_clone = pads.clone();
        let element = self.obj().clone();

        // Spawn receiver thread
        let thread_handle = std::thread::spawn(move || {
            Self::receiver_loop(
                element,
                subscriber_for_thread,
                stopping_clone,
                stats_clone,
                pads_clone,
                pad_naming,
                receive_timeout_ms,
            );
        });

        *state = State::Started(Started {
            _session: session,
            _subscriber: subscriber_for_state,
            stopping,
            stats,
            pads,
            thread_handle: Some(thread_handle),
        });

        gst::debug!(CAT, imp = self, "ZenohDemux started successfully");
        Ok(())
    }

    fn stop(&self) {
        let mut state = self.state.lock().unwrap();
        if let State::Started(ref mut started) = *state {
            gst::debug!(CAT, imp = self, "Stopping ZenohDemux");

            // Signal the receiver thread to stop
            started.stopping.store(true, Ordering::SeqCst);

            // Wait for the thread to finish
            if let Some(handle) = started.thread_handle.take() {
                drop(state); // Release lock before joining
                let _ = handle.join();
                state = self.state.lock().unwrap();
            }

            // Remove all dynamic pads
            if let State::Started(ref started) = *state {
                let pads = started.pads.lock().unwrap();
                for (_, pad) in pads.iter() {
                    let _ = self.obj().remove_pad(pad);
                }
            }
        }

        *state = State::Stopped;
        gst::debug!(CAT, imp = self, "ZenohDemux stopped");
    }

    fn receiver_loop(
        element: super::ZenohDemux,
        subscriber: zenoh::pubsub::Subscriber<
            zenoh::handlers::FifoChannelHandler<zenoh::sample::Sample>,
        >,
        stopping: Arc<AtomicBool>,
        stats: Arc<Mutex<Statistics>>,
        pads: Arc<Mutex<HashMap<String, gst::Pad>>>,
        pad_naming: PadNaming,
        receive_timeout_ms: u64,
    ) {
        gst::debug!(CAT, "Receiver loop started");

        while !stopping.load(Ordering::SeqCst) {
            // Use recv_timeout to remain responsive to stopping signal
            match subscriber.recv_timeout(Duration::from_millis(receive_timeout_ms)) {
                Ok(Some(sample)) => {
                    // Get the key expression this sample arrived on
                    let sample_key_expr = sample.key_expr().as_str().to_string();
                    let pad_name = key_expr_to_pad_name(&sample_key_expr, pad_naming);

                    // Get or create the pad for this key expression
                    let pad = {
                        let mut pads_guard = pads.lock().unwrap();
                        if let Some(pad) = pads_guard.get(&pad_name) {
                            pad.clone()
                        } else {
                            // Create a new pad
                            gst::debug!(
                                CAT,
                                "Creating new pad '{}' for key expression '{}'",
                                pad_name,
                                sample_key_expr
                            );

                            let templ = element.pad_template("src_%s").unwrap();
                            let pad = gst::Pad::builder_from_template(&templ)
                                .name(pad_name.as_str())
                                .build();

                            // Activate the pad
                            pad.set_active(true).unwrap();

                            // Add pad to element
                            element.add_pad(&pad).unwrap();

                            // Send stream-start event (required before any data)
                            let stream_id = format!("zenohdemux/{}/{}", pad_name, sample_key_expr);
                            pad.push_event(gst::event::StreamStart::new(&stream_id));

                            // Send segment event (required before any data)
                            let segment = gst::FormattedSegment::<gst::ClockTime>::new();
                            pad.push_event(gst::event::Segment::new(&segment));

                            // Update statistics
                            stats.lock().unwrap().pads_created += 1;

                            pads_guard.insert(pad_name, pad.clone());
                            pad
                        }
                    };

                    // Process the sample and create a buffer
                    let payload = sample.payload();
                    let data = payload.to_bytes();

                    // Check for metadata (caps, compression, etc.)
                    #[cfg(any(
                        feature = "compression-zstd",
                        feature = "compression-lz4",
                        feature = "compression-gzip"
                    ))]
                    let (final_data, metadata) = if let Some(attachment) = sample.attachment() {
                        match MetadataParser::parse(attachment) {
                            Ok(meta) => {
                                // Check for compression
                                if let Some(comp_str) =
                                    meta.user_metadata().get(crate::metadata::keys::COMPRESSION)
                                {
                                    if let Some(comp_type) =
                                        crate::compression::CompressionType::from_metadata_value(
                                            comp_str,
                                        )
                                    {
                                        match crate::compression::decompress(&data, comp_type) {
                                            Ok(decompressed) => (decompressed, Some(meta)),
                                            Err(e) => {
                                                gst::warning!(CAT, "Decompression failed: {}", e);
                                                stats.lock().unwrap().errors += 1;
                                                continue;
                                            }
                                        }
                                    } else {
                                        (data.to_vec(), Some(meta))
                                    }
                                } else {
                                    (data.to_vec(), Some(meta))
                                }
                            }
                            Err(e) => {
                                gst::warning!(CAT, "Failed to parse metadata: {}", e);
                                (data.to_vec(), None)
                            }
                        }
                    } else {
                        (data.to_vec(), None)
                    };

                    #[cfg(not(any(
                        feature = "compression-zstd",
                        feature = "compression-lz4",
                        feature = "compression-gzip"
                    )))]
                    let (final_data, metadata) = if let Some(attachment) = sample.attachment() {
                        match MetadataParser::parse(attachment) {
                            Ok(meta) => (data.to_vec(), Some(meta)),
                            Err(e) => {
                                gst::warning!(CAT, "Failed to parse metadata: {}", e);
                                (data.to_vec(), None)
                            }
                        }
                    } else {
                        (data.to_vec(), None)
                    };

                    // Create buffer
                    let mut buffer = match gst::Buffer::with_size(final_data.len()) {
                        Ok(buf) => buf,
                        Err(_) => {
                            gst::warning!(CAT, "Failed to allocate buffer");
                            stats.lock().unwrap().errors += 1;
                            continue;
                        }
                    };

                    {
                        let buffer_ref = match buffer.get_mut() {
                            Some(b) => b,
                            None => {
                                gst::warning!(CAT, "Failed to get mutable buffer");
                                stats.lock().unwrap().errors += 1;
                                continue;
                            }
                        };

                        if buffer_ref.copy_from_slice(0, &final_data).is_err() {
                            gst::warning!(CAT, "Failed to copy data to buffer");
                            stats.lock().unwrap().errors += 1;
                            continue;
                        }

                        // Apply metadata to buffer
                        if let Some(ref meta) = metadata {
                            meta.apply_to_buffer(buffer_ref);

                            // Set caps if present
                            if let Some(caps) = meta.caps() {
                                // We can't set caps on the buffer directly, but we can
                                // set them on the pad if needed
                                if !pad.has_current_caps() {
                                    pad.push_event(gst::event::Caps::new(caps));
                                }
                            }
                        }
                    }

                    // Update statistics
                    {
                        let mut stats_guard = stats.lock().unwrap();
                        stats_guard.bytes_received += final_data.len() as u64;
                        stats_guard.messages_received += 1;
                    }

                    // Push buffer to the pad
                    match pad.push(buffer) {
                        Ok(_) => {}
                        Err(gst::FlowError::Flushing) => {
                            gst::debug!(CAT, "Pad {} is flushing", pad.name());
                        }
                        Err(e) => {
                            gst::warning!(
                                CAT,
                                "Failed to push buffer to pad {}: {:?}",
                                pad.name(),
                                e
                            );
                        }
                    }
                }
                Ok(None) => {
                    // Timeout - continue loop
                    continue;
                }
                Err(e) => {
                    let err_msg = format!("{:?}", e);
                    if err_msg.contains("Timeout") {
                        continue;
                    } else {
                        gst::warning!(CAT, "Subscriber error: {}", e);
                        stats.lock().unwrap().errors += 1;
                        break;
                    }
                }
            }
        }

        gst::debug!(CAT, "Receiver loop finished");
    }
}