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
//! # ZenohSink Element
//!
//! The ZenohSink element sends GStreamer buffers over the Zenoh network protocol.
//! It acts as a bridge between GStreamer pipelines and Zenoh networks, enabling
//! distributed media streaming and data sharing across different applications
//! and systems.
//!
//! ## Features
//!
//! * **Quality of Service (QoS) Control**: Configurable reliability and congestion control
//! * **Low Latency Mode**: Express mode for time-critical applications
//! * **Priority Management**: Message prioritization for bandwidth management
//! * **Session Sharing**: Support for shared Zenoh sessions across elements
//! * **Flexible Configuration**: Support for Zenoh config files and runtime parameters
//!
//! ## Properties
//!
//! * `key-expr` - Zenoh key expression for publishing data (required)
//!   - Example: "demo/video/stream" or "sensors/temperature/{device_id}"
//! * `config` - Path to Zenoh configuration file (optional)
//!   - Allows custom Zenoh network configuration (endpoints, discovery, etc.)
//! * `priority` - Publisher priority level (1-7, default: 5)
//!   - 1=RealTime (highest), 2=InteractiveHigh, 3=InteractiveLow, 4=DataHigh, 5=Data(default), 6=DataLow, 7=Background(lowest)
//! * `congestion-control` - Congestion control policy (default: "block")
//!   - `"block"`: Wait for network congestion to clear (ensures delivery)
//!   - `"drop"`: Drop messages during congestion (maintains real-time performance)
//! * `reliability` - Reliability mode (default: "best-effort")
//!   - `"best-effort"`: Fire-and-forget delivery (lower latency)
//!   - `"reliable"`: Acknowledged delivery with retransmission (higher reliability)
//! * `express` - Enable express mode for lower latency (default: false)
//!   - Bypasses some internal queues for reduced end-to-end latency
//!   - May increase CPU usage but improves responsiveness
//!
//! ## Example Pipelines
//!
//! ### Basic Video Streaming
//! ```bash
//! # Simple video streaming
//! gst-launch-1.0 videotestsrc ! zenohsink key-expr=demo/video/stream
//! ```
//!
//! ### High-Quality Reliable Streaming
//! ```bash
//! # Reliable delivery with high priority and express mode for low latency
//! gst-launch-1.0 videotestsrc ! zenohsink key-expr=demo/video/reliable \
//!   reliability=reliable congestion-control=block express=true priority=2
//! ```
//!
//! ### Real-Time Streaming with Quality Trade-offs
//! ```bash
//! # Best-effort delivery optimized for real-time performance
//! gst-launch-1.0 videotestsrc ! zenohsink key-expr=demo/video/realtime \
//!   reliability=best-effort congestion-control=drop express=true
//! ```
//!
//! ### Audio Streaming with Custom Configuration
//! ```bash
//! # Audio with custom Zenoh configuration
//! gst-launch-1.0 audiotestsrc ! audioconvert ! zenohsink \
//!   key-expr=demo/audio/stream config=/path/to/zenoh.json5 priority=4
//! ```
//!
//! ### Encoded Video with H.264
//! ```bash
//! # H.264 encoded video streaming
//! gst-launch-1.0 videotestsrc ! x264enc ! rtph264pay ! zenohsink \
//!   key-expr=demo/video/h264 reliability=reliable
//! ```

use gst::glib;
use gst::prelude::*;
use gst::subclass::prelude::ObjectSubclassIsExt;

pub mod imp;

glib::wrapper! {
    /// A GStreamer sink element that publishes data via Zenoh.
    ///
    /// This element receives buffers from upstream elements and publishes
    /// them to a Zenoh network using the configured key expression.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use gst::prelude::*;
    /// use gstzenoh::zenohsink::ZenohSink;
    ///
    /// gst::init().unwrap();
    /// gstzenoh::plugin_register_static().unwrap();
    ///
    /// // Create with the builder pattern
    /// let sink = ZenohSink::builder("demo/video")
    ///     .reliability("reliable")
    ///     .priority(2)
    ///     .express(true)
    ///     .build();
    ///
    /// // Or create and configure separately
    /// let sink = ZenohSink::new("demo/video");
    /// sink.set_reliability("reliable");
    /// sink.set_express(true);
    ///
    /// // Access statistics
    /// println!("Bytes sent: {}", sink.bytes_sent());
    /// ```
    pub struct ZenohSink(ObjectSubclass<imp::ZenohSink>) @extends gst_base::BaseSink, gst::Element, gst::Object, @implements gst::URIHandler;
}

unsafe impl Send for ZenohSink {}
unsafe impl Sync for ZenohSink {}

impl Default for ZenohSink {
    fn default() -> Self {
        gst::Object::builder().build().unwrap()
    }
}

impl ZenohSink {
    /// Creates a new ZenohSink with the specified key expression.
    ///
    /// # Arguments
    ///
    /// * `key_expr` - The Zenoh key expression for publishing data
    ///
    /// # Example
    ///
    /// ```no_run
    /// use gstzenoh::zenohsink::ZenohSink;
    ///
    /// gst::init().unwrap();
    /// gstzenoh::plugin_register_static().unwrap();
    ///
    /// let sink = ZenohSink::new("demo/video/stream");
    /// ```
    pub fn new(key_expr: &str) -> Self {
        gst::Object::builder()
            .property("key-expr", key_expr)
            .build()
            .unwrap()
    }

    /// Returns a builder for creating a ZenohSink with custom configuration.
    ///
    /// # Arguments
    ///
    /// * `key_expr` - The Zenoh key expression for publishing data
    ///
    /// # Example
    ///
    /// ```no_run
    /// use gstzenoh::zenohsink::ZenohSink;
    ///
    /// gst::init().unwrap();
    /// gstzenoh::plugin_register_static().unwrap();
    ///
    /// let sink = ZenohSink::builder("demo/video")
    ///     .reliability("reliable")
    ///     .priority(2)
    ///     .express(true)
    ///     .congestion_control("block")
    ///     .build();
    /// ```
    pub fn builder(key_expr: &str) -> ZenohSinkBuilder {
        ZenohSinkBuilder::new(key_expr)
    }

    // -------------------------------------------------------------------------
    // Property Setters
    // -------------------------------------------------------------------------

    /// Sets the Zenoh key expression for publishing data.
    ///
    /// Must be set before the element is started.
    pub fn set_key_expr(&self, key_expr: &str) {
        self.set_property("key-expr", key_expr);
    }

    /// Sets the path to a Zenoh configuration file.
    ///
    /// The file should be in JSON5 format.
    pub fn set_config(&self, config_path: &str) {
        self.set_property("config", config_path);
    }

    /// Sets the publisher priority level.
    ///
    /// Valid values: 1-7
    /// - 1: RealTime (highest priority)
    /// - 2: InteractiveHigh
    /// - 3: InteractiveLow
    /// - 4: DataHigh
    /// - 5: Data (default)
    /// - 6: DataLow
    /// - 7: Background (lowest priority)
    pub fn set_priority(&self, priority: u32) {
        self.set_property("priority", priority);
    }

    /// Sets the congestion control policy.
    ///
    /// - `"block"`: Wait for network congestion to clear (default)
    /// - `"drop"`: Drop messages during congestion
    pub fn set_congestion_control(&self, mode: &str) {
        self.set_property("congestion-control", mode);
    }

    /// Sets the reliability mode.
    ///
    /// - `"best-effort"`: Fire-and-forget delivery (default)
    /// - `"reliable"`: Acknowledged delivery with retransmission
    pub fn set_reliability(&self, mode: &str) {
        self.set_property("reliability", mode);
    }

    /// Enables or disables express mode.
    ///
    /// Express mode bypasses internal queues for lower latency,
    /// but may increase CPU usage.
    pub fn set_express(&self, express: bool) {
        self.set_property("express", express);
    }

    /// Enables or disables sending GStreamer caps as metadata.
    pub fn set_send_caps(&self, send_caps: bool) {
        self.set_property("send-caps", send_caps);
    }

    /// Sets the interval in seconds for periodic caps transmission.
    ///
    /// Set to 0 to only send caps on the first buffer and on changes.
    pub fn set_caps_interval(&self, interval: u32) {
        self.set_property("caps-interval", interval);
    }

    /// Enables or disables sending buffer timing metadata (PTS, DTS, duration, flags).
    pub fn set_send_buffer_meta(&self, send: bool) {
        self.set_property("send-buffer-meta", send);
    }

    /// Sets a shared Zenoh session for this element.
    ///
    /// This allows multiple elements to share a single Zenoh session,
    /// reducing network overhead and resource usage. The session must
    /// be set before the element transitions to the PLAYING state.
    ///
    /// Note: `zenoh::Session` is internally Arc-based and Clone, so you
    /// can simply clone the session when sharing it across multiple elements.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use gstzenoh::ZenohSink;
    /// use zenoh::Wait;
    ///
    /// let session = zenoh::open(zenoh::Config::default()).wait()?;
    ///
    /// let sink1 = ZenohSink::new("demo/video");
    /// sink1.set_session(session.clone());
    ///
    /// let sink2 = ZenohSink::new("demo/audio");
    /// sink2.set_session(session);
    /// ```
    pub fn set_session(&self, session: zenoh::Session) {
        self.imp().set_external_session(session);
    }

    /// Sets the session group name for sharing sessions across elements.
    ///
    /// Elements with the same session-group name will share a single
    /// Zenoh session. This is useful for gst-launch pipelines where
    /// you can't share session objects directly.
    ///
    /// Must be set before the element transitions to the PLAYING state.
    pub fn set_session_group(&self, group: &str) {
        self.set_property("session-group", group);
    }

    // -------------------------------------------------------------------------
    // Property Getters
    // -------------------------------------------------------------------------

    /// Returns the current Zenoh key expression.
    pub fn key_expr(&self) -> String {
        self.property("key-expr")
    }

    /// Returns the path to the Zenoh configuration file, if set.
    pub fn config(&self) -> Option<String> {
        self.property("config")
    }

    /// Returns the current priority level (1-7).
    pub fn priority(&self) -> u32 {
        self.property("priority")
    }

    /// Returns the current congestion control mode.
    pub fn congestion_control(&self) -> String {
        self.property("congestion-control")
    }

    /// Returns the current reliability mode.
    pub fn reliability(&self) -> String {
        self.property("reliability")
    }

    /// Returns whether express mode is enabled.
    pub fn express(&self) -> bool {
        self.property("express")
    }

    /// Returns whether caps are being sent as metadata.
    pub fn send_caps(&self) -> bool {
        self.property("send-caps")
    }

    /// Returns the caps transmission interval in seconds.
    pub fn caps_interval(&self) -> u32 {
        self.property("caps-interval")
    }

    /// Returns whether buffer timing metadata is being sent.
    pub fn send_buffer_meta(&self) -> bool {
        self.property("send-buffer-meta")
    }

    /// Returns the session group name, if set.
    pub fn session_group(&self) -> Option<String> {
        self.property("session-group")
    }

    // -------------------------------------------------------------------------
    // Statistics (read-only)
    // -------------------------------------------------------------------------

    /// Returns the total number of bytes sent since the element started.
    pub fn bytes_sent(&self) -> u64 {
        self.property("bytes-sent")
    }

    /// Returns the total number of messages sent since the element started.
    pub fn messages_sent(&self) -> u64 {
        self.property("messages-sent")
    }

    /// Returns the total number of errors encountered.
    pub fn errors(&self) -> u64 {
        self.property("errors")
    }

    /// Returns the total number of messages dropped due to congestion.
    pub fn dropped(&self) -> u64 {
        self.property("dropped")
    }
}

impl TryFrom<gst::Element> for ZenohSink {
    type Error = gst::Element;

    /// Attempts to convert a generic GStreamer element to a ZenohSink.
    ///
    /// Returns the original element as an error if it's not a ZenohSink.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use gst::prelude::*;
    /// use gstzenoh::zenohsink::ZenohSink;
    ///
    /// gst::init().unwrap();
    /// gstzenoh::plugin_register_static().unwrap();
    ///
    /// let element = gst::ElementFactory::make("zenohsink")
    ///     .property("key-expr", "demo/video")
    ///     .build()
    ///     .unwrap();
    ///
    /// let sink = ZenohSink::try_from(element).expect("Should be a ZenohSink");
    /// assert_eq!(sink.key_expr(), "demo/video");
    /// ```
    fn try_from(element: gst::Element) -> Result<Self, Self::Error> {
        element.downcast()
    }
}

/// Builder for creating a [`ZenohSink`] with custom configuration.
///
/// # Example
///
/// ```no_run
/// use gstzenoh::zenohsink::ZenohSink;
///
/// gst::init().unwrap();
/// gstzenoh::plugin_register_static().unwrap();
///
/// let sink = ZenohSink::builder("demo/video")
///     .reliability("reliable")
///     .priority(2)
///     .express(true)
///     .send_caps(true)
///     .build();
/// ```
pub struct ZenohSinkBuilder {
    key_expr: String,
    config: Option<String>,
    priority: Option<u32>,
    congestion_control: Option<String>,
    reliability: Option<String>,
    express: Option<bool>,
    send_caps: Option<bool>,
    caps_interval: Option<u32>,
    send_buffer_meta: Option<bool>,
    session: Option<zenoh::Session>,
    session_group: Option<String>,
}

impl ZenohSinkBuilder {
    /// Creates a new builder with the required key expression.
    pub fn new(key_expr: &str) -> Self {
        Self {
            key_expr: key_expr.to_string(),
            config: None,
            priority: None,
            congestion_control: None,
            reliability: None,
            express: None,
            send_caps: None,
            caps_interval: None,
            send_buffer_meta: None,
            session: None,
            session_group: None,
        }
    }

    /// Sets the path to a Zenoh configuration file.
    pub fn config(mut self, path: &str) -> Self {
        self.config = Some(path.to_string());
        self
    }

    /// Sets the publisher priority level (1-7).
    pub fn priority(mut self, priority: u32) -> Self {
        self.priority = Some(priority);
        self
    }

    /// Sets the congestion control policy ("block" or "drop").
    pub fn congestion_control(mut self, mode: &str) -> Self {
        self.congestion_control = Some(mode.to_string());
        self
    }

    /// Sets the reliability mode ("best-effort" or "reliable").
    pub fn reliability(mut self, mode: &str) -> Self {
        self.reliability = Some(mode.to_string());
        self
    }

    /// Enables or disables express mode.
    pub fn express(mut self, express: bool) -> Self {
        self.express = Some(express);
        self
    }

    /// Enables or disables sending caps as metadata.
    pub fn send_caps(mut self, send: bool) -> Self {
        self.send_caps = Some(send);
        self
    }

    /// Sets the caps transmission interval in seconds.
    pub fn caps_interval(mut self, interval: u32) -> Self {
        self.caps_interval = Some(interval);
        self
    }

    /// Enables or disables sending buffer timing metadata.
    pub fn send_buffer_meta(mut self, send: bool) -> Self {
        self.send_buffer_meta = Some(send);
        self
    }

    /// Sets a shared Zenoh session for this element.
    ///
    /// This allows multiple elements to share a single Zenoh session,
    /// reducing network overhead and resource usage.
    ///
    /// Note: `zenoh::Session` is internally Arc-based and Clone, so you
    /// can simply clone the session when sharing it across elements.
    pub fn session(mut self, session: zenoh::Session) -> Self {
        self.session = Some(session);
        self
    }

    /// Sets the session group name for sharing sessions across elements.
    ///
    /// Elements with the same session-group name will share a single
    /// Zenoh session. This is useful for gst-launch pipelines.
    pub fn session_group(mut self, group: &str) -> Self {
        self.session_group = Some(group.to_string());
        self
    }

    /// Builds the ZenohSink with the configured properties.
    pub fn build(self) -> ZenohSink {
        let mut builder = gst::Object::builder::<ZenohSink>().property("key-expr", &self.key_expr);

        if let Some(config) = self.config {
            builder = builder.property("config", config);
        }
        if let Some(priority) = self.priority {
            builder = builder.property("priority", priority);
        }
        if let Some(cc) = self.congestion_control {
            builder = builder.property("congestion-control", cc);
        }
        if let Some(rel) = self.reliability {
            builder = builder.property("reliability", rel);
        }
        if let Some(exp) = self.express {
            builder = builder.property("express", exp);
        }
        if let Some(sc) = self.send_caps {
            builder = builder.property("send-caps", sc);
        }
        if let Some(ci) = self.caps_interval {
            builder = builder.property("caps-interval", ci);
        }
        if let Some(sbm) = self.send_buffer_meta {
            builder = builder.property("send-buffer-meta", sbm);
        }
        if let Some(ref sg) = self.session_group {
            builder = builder.property("session-group", sg);
        }

        let sink: ZenohSink = builder.build().unwrap();

        // Set the session directly (can't be done via properties)
        if let Some(session) = self.session {
            sink.set_session(session);
        }

        sink
    }
}

pub fn register(plugin: &gst::Plugin) -> Result<(), glib::BoolError> {
    gst::Element::register(
        Some(plugin),
        "zenohsink",
        gst::Rank::NONE + 100, // Higher than MARGINAL to be discoverable
        ZenohSink::static_type(),
    )
}