gstreamer_analytics/
batchmeta.rs

1// Copyright (C) 2025 Sebastian Dröge <sebastian@centricular.com>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public License, v2.0.
4// If a copy of the MPL was not distributed with this file, You can obtain one at
5// <https://mozilla.org/MPL/2.0/>.
6//
7// SPDX-License-Identifier: MPL-2.0
8
9use crate::ffi;
10use glib::translate::*;
11use gst::prelude::*;
12
13use std::{fmt, slice, sync::LazyLock};
14
15#[doc(alias = "GST_CAPS_FEATURE_META_GST_ANALYTICS_BATCH_META")]
16pub const CAPS_FEATURE_META_ANALYTICS_BATCH_META: &glib::GStr = unsafe {
17    glib::GStr::from_utf8_with_nul_unchecked(ffi::GST_CAPS_FEATURE_META_GST_ANALYTICS_BATCH_META)
18};
19pub static CAPS_FEATURES_META_ANALYTICS_BATCH_META: LazyLock<gst::CapsFeatures> =
20    LazyLock::new(|| gst::CapsFeatures::new([CAPS_FEATURE_META_ANALYTICS_BATCH_META]));
21
22#[repr(transparent)]
23#[doc(alias = "GstAnalyticsBatchMeta")]
24pub struct AnalyticsBatchMeta(ffi::GstAnalyticsBatchMeta);
25
26unsafe impl Send for AnalyticsBatchMeta {}
27unsafe impl Sync for AnalyticsBatchMeta {}
28
29impl AnalyticsBatchMeta {
30    #[doc(alias = "gst_buffer_add_analytics_batch_meta")]
31    pub fn add<'a>(
32        buffer: &'a mut gst::BufferRef,
33    ) -> gst::MetaRefMut<'a, Self, gst::meta::Standalone> {
34        skip_assert_initialized!();
35
36        unsafe {
37            let meta = ffi::gst_buffer_add_analytics_batch_meta(buffer.as_mut_ptr());
38
39            Self::from_mut_ptr(buffer, meta)
40        }
41    }
42
43    pub fn streams(&self) -> &[AnalyticsBatchStream] {
44        unsafe {
45            if self.0.streams.is_null() {
46                &[]
47            } else {
48                slice::from_raw_parts(self.0.streams as *const _, self.0.n_streams)
49            }
50        }
51    }
52
53    pub fn streams_mut(&mut self) -> &mut [AnalyticsBatchStream] {
54        unsafe {
55            if self.0.streams.is_null() {
56                &mut []
57            } else {
58                slice::from_raw_parts_mut(self.0.streams as *mut _, self.0.n_streams)
59            }
60        }
61    }
62}
63
64unsafe impl gst::MetaAPI for AnalyticsBatchMeta {
65    type GstType = ffi::GstAnalyticsBatchMeta;
66
67    #[doc(alias = "gst_analytics_batch_meta_api_get_type")]
68    #[inline]
69    fn meta_api() -> glib::Type {
70        unsafe { from_glib(ffi::gst_analytics_batch_meta_api_get_type()) }
71    }
72}
73
74impl fmt::Debug for AnalyticsBatchMeta {
75    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
76        f.debug_struct("AnalyticsBatchMeta")
77            .field("streams", &self.streams())
78            .finish()
79    }
80}
81
82#[repr(transparent)]
83#[doc(alias = "GstAnalyticsBatchStream")]
84pub struct AnalyticsBatchStream(ffi::GstAnalyticsBatchStream);
85
86impl AnalyticsBatchStream {
87    pub fn index(&self) -> u32 {
88        self.0.index
89    }
90
91    pub fn sticky_events(&self) -> &[gst::Event] {
92        unsafe {
93            if self.0.sticky_events.is_null() {
94                &[]
95            } else {
96                slice::from_raw_parts(self.0.sticky_events as *const _, self.0.n_sticky_events)
97            }
98        }
99    }
100
101    pub fn sticky_events_mut(&mut self) -> &mut [gst::Event] {
102        unsafe {
103            if self.0.sticky_events.is_null() {
104                &mut []
105            } else {
106                slice::from_raw_parts_mut(self.0.sticky_events as *mut _, self.0.n_sticky_events)
107            }
108        }
109    }
110
111    pub fn objects(&self) -> &[gst::MiniObject] {
112        unsafe {
113            if self.0.objects.is_null() {
114                &[]
115            } else {
116                slice::from_raw_parts(self.0.objects as *const _, self.0.n_objects)
117            }
118        }
119    }
120
121    pub fn objects_mut(&mut self) -> &mut [gst::MiniObject] {
122        unsafe {
123            if self.0.objects.is_null() {
124                &mut []
125            } else {
126                slice::from_raw_parts_mut(self.0.objects as *mut _, self.0.n_objects)
127            }
128        }
129    }
130
131    #[doc(alias = "gst_analytics_batch_stream_get_caps")]
132    pub fn caps(&self) -> Option<gst::Caps> {
133        unsafe {
134            from_glib_none(ffi::gst_analytics_batch_stream_get_caps(mut_override(
135                &self.0,
136            )))
137        }
138    }
139
140    #[doc(alias = "gst_analytics_batch_stream_get_segment")]
141    pub fn segment(&self) -> Option<gst::Segment> {
142        unsafe {
143            from_glib_none(ffi::gst_analytics_batch_stream_get_segment(mut_override(
144                &self.0,
145            )))
146        }
147    }
148
149    #[doc(alias = "gst_analytics_batch_stream_get_stream_id")]
150    pub fn stream_id(&self) -> Option<&glib::GStr> {
151        unsafe {
152            let res = ffi::gst_analytics_batch_stream_get_stream_id(mut_override(&self.0));
153
154            if res.is_null() {
155                None
156            } else {
157                Some(glib::GStr::from_ptr(res))
158            }
159        }
160    }
161}
162
163impl fmt::Debug for AnalyticsBatchStream {
164    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
165        f.debug_struct("AnalyticsBatchStream")
166            .field("index", &self.index())
167            .field("objects", &self.objects())
168            .finish()
169    }
170}