1use glib::translate::*;
4
5#[cfg(feature = "v1_30")]
6use std::{ffi::CStr, num::NonZeroU64, ptr};
7
8#[cfg(feature = "v1_30")]
9use crate::StructureRef;
10use crate::{Plugin, Tracer};
11
12impl Tracer {
13 #[doc(alias = "gst_tracer_register")]
14 pub fn register(
15 plugin: Option<&Plugin>,
16 name: &str,
17 type_: glib::types::Type,
18 ) -> Result<(), glib::error::BoolError> {
19 skip_assert_initialized!();
20 unsafe {
21 glib::result_from_gboolean!(
22 crate::ffi::gst_tracer_register(
23 plugin.to_glib_none().0,
24 name.to_glib_none().0,
25 type_.into_glib()
26 ),
27 "Failed to register tracer factory"
28 )
29 }
30 }
31}
32
33#[cfg(feature = "v1_30")]
41#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
42#[doc(alias = "GstTraceSpanId")]
43pub struct TraceSpanId(NonZeroU64);
44
45#[cfg(feature = "v1_30")]
46impl TraceSpanId {
47 #[inline]
48 pub fn get(self) -> u64 {
49 self.0.get()
50 }
51
52 #[inline]
56 pub(crate) fn from_glib(span_id: crate::ffi::GstTraceSpanId) -> Option<Self> {
57 skip_assert_initialized!();
58 NonZeroU64::new(span_id).map(Self)
59 }
60}
61
62#[cfg(feature = "v1_30")]
63#[repr(transparent)]
64#[doc(alias = "GstTraceFormat")]
65#[derive(Clone, Copy)]
66pub struct TraceFormat(ptr::NonNull<crate::ffi::GstTraceFormat>);
67
68#[cfg(feature = "v1_30")]
69unsafe impl Send for TraceFormat {}
70#[cfg(feature = "v1_30")]
71unsafe impl Sync for TraceFormat {}
72
73#[cfg(feature = "v1_30")]
74impl std::fmt::Debug for TraceFormat {
75 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
76 f.debug_struct("TraceFormat")
77 .field("name", &self.name())
78 .field("description", &self.description())
79 .field(
80 "fields",
81 &self.fields().map(|field| field.name()).collect::<Vec<_>>(),
82 )
83 .finish()
84 }
85}
86
87#[cfg(feature = "v1_30")]
88impl TraceFormat {
89 #[inline]
90 pub(crate) unsafe fn from_glib_borrow(ptr: *mut crate::ffi::GstTraceFormat) -> Self {
91 skip_assert_initialized!();
92 debug_assert!(!ptr.is_null());
93 Self(unsafe { ptr::NonNull::new_unchecked(ptr) })
94 }
95
96 #[inline]
97 pub(crate) fn as_mut_ptr(&self) -> *mut crate::ffi::GstTraceFormat {
98 self.0.as_ptr()
99 }
100
101 #[inline]
105 pub fn as_ptr_id(&self) -> usize {
106 self.0.as_ptr() as usize
107 }
108
109 #[doc(alias = "gst_trace_format_get_name")]
110 pub fn name(&self) -> &str {
111 unsafe {
112 let ptr = crate::ffi::gst_trace_format_get_name(self.as_mut_ptr());
113 if ptr.is_null() {
114 return "";
115 }
116 CStr::from_ptr(ptr).to_str().unwrap_or("")
117 }
118 }
119
120 #[doc(alias = "gst_trace_format_get_description")]
121 pub fn description(&self) -> Option<&str> {
122 unsafe {
123 let ptr = crate::ffi::gst_trace_format_get_description(self.as_mut_ptr());
124 if ptr.is_null() {
125 None
126 } else {
127 CStr::from_ptr(ptr).to_str().ok()
128 }
129 }
130 }
131
132 #[doc(alias = "gst_trace_format_get_n_fields")]
133 pub fn n_fields(&self) -> usize {
134 unsafe { crate::ffi::gst_trace_format_get_n_fields(self.as_mut_ptr()) as usize }
135 }
136
137 #[doc(alias = "gst_trace_format_get_field_name")]
141 pub(crate) fn field_name(&self, index: usize) -> Option<&str> {
142 unsafe {
143 let ptr = crate::ffi::gst_trace_format_get_field_name(self.as_mut_ptr(), index as u32);
144 if ptr.is_null() {
145 None
146 } else {
147 CStr::from_ptr(ptr).to_str().ok()
148 }
149 }
150 }
151
152 #[doc(alias = "gst_trace_format_get_field_type")]
157 pub(crate) fn field_type(&self, index: usize) -> crate::TracerFieldType {
158 unsafe {
159 from_glib(crate::ffi::gst_trace_format_get_field_type(
160 self.as_mut_ptr(),
161 index as u32,
162 ))
163 }
164 }
165
166 #[doc(alias = "gst_trace_format_get_field_description")]
170 pub(crate) fn field_description(&self, index: usize) -> Option<&str> {
171 unsafe {
172 let ptr =
173 crate::ffi::gst_trace_format_get_field_description(self.as_mut_ptr(), index as u32);
174 if ptr.is_null() {
175 None
176 } else {
177 CStr::from_ptr(ptr).to_str().ok()
178 }
179 }
180 }
181
182 #[doc(alias = "gst_trace_format_get_field_structure")]
183 pub(crate) fn field_structure(&self, index: usize) -> Option<&StructureRef> {
184 unsafe {
185 let ptr =
186 crate::ffi::gst_trace_format_get_field_structure(self.as_mut_ptr(), index as u32);
187 if ptr.is_null() {
188 None
189 } else {
190 Some(StructureRef::from_glib_borrow(ptr))
191 }
192 }
193 }
194
195 pub fn fields(&self) -> impl ExactSizeIterator<Item = TraceFormatField<'_>> + '_ {
198 (0..self.n_fields()).map(move |index| TraceFormatField {
199 format: self,
200 index,
201 })
202 }
203}
204
205#[cfg(feature = "v1_30")]
209#[derive(Debug, Clone, Copy)]
210pub struct TraceFormatField<'a> {
211 format: &'a TraceFormat,
212 index: usize,
213}
214
215#[cfg(feature = "v1_30")]
216impl<'a> TraceFormatField<'a> {
217 pub fn name(&self) -> &'a str {
218 self.format.field_name(self.index).unwrap()
219 }
220
221 pub fn type_(&self) -> crate::TracerFieldType {
222 self.format.field_type(self.index)
223 }
224
225 pub fn description(&self) -> Option<&'a str> {
226 self.format.field_description(self.index)
227 }
228
229 pub fn structure(&self) -> Option<&'a StructureRef> {
230 self.format.field_structure(self.index)
231 }
232}
233
234#[cfg(feature = "v1_30")]
242unsafe fn read_trace_value(
243 value: &crate::ffi::GstTraceValue,
244 field_type: crate::TracerFieldType,
245) -> Option<TraceValue<'_>> {
246 use crate::TracerFieldType::*;
247 unsafe {
248 match field_type {
249 Boolean => Some(TraceValue::Boolean(from_glib(value.v_boolean))),
250 Int => Some(TraceValue::Int(value.v_int)),
251 Uint => Some(TraceValue::Uint(value.v_uint)),
252 Int64 => Some(TraceValue::Int64(value.v_int64)),
253 Uint64 | ClockTime => Some(TraceValue::Uint64(value.v_uint64)),
254 Double => Some(TraceValue::Double(value.v_double)),
255 String => Some(TraceValue::String(
256 value
257 .v_string
258 .as_ref()
259 .map(|_| glib::GStr::from_ptr(value.v_string)),
260 )),
261 Structure => Some(TraceValue::Structure(
262 value
263 .v_structure
264 .as_ref()
265 .map(|_| StructureRef::from_glib_borrow(value.v_structure)),
266 )),
267 Object => Some(TraceValue::Object(
268 value
269 .v_object
270 .as_ref()
271 .map(|_| from_glib_none(value.v_object)),
272 )),
273 _ => None,
274 }
275 }
276}
277
278#[cfg(feature = "v1_30")]
282#[derive(Debug)]
283pub enum TraceValue<'a> {
284 Boolean(bool),
285 Int(i32),
286 Uint(u32),
287 Int64(i64),
288 Uint64(u64),
289 Double(f64),
290 String(Option<&'a glib::GStr>),
291 Structure(Option<&'a StructureRef>),
292 Object(Option<glib::Object>),
293}
294
295#[cfg(feature = "v1_30")]
302pub struct TraceValues<'a> {
303 format: TraceFormat,
304 values: &'a [crate::ffi::GstTraceValue],
305}
306
307#[cfg(feature = "v1_30")]
308impl<'a> TraceValues<'a> {
309 #[inline]
315 pub(crate) unsafe fn from_glib(
316 format: TraceFormat,
317 values: *const crate::ffi::GstTraceValue,
318 len: usize,
319 ) -> Self {
320 skip_assert_initialized!();
321 let values = if len == 0 {
322 &[]
323 } else {
324 debug_assert!(!values.is_null());
325 unsafe { std::slice::from_raw_parts(values, len) }
326 };
327 Self { format, values }
328 }
329
330 #[inline]
333 pub fn format(&self) -> TraceFormat {
334 self.format
335 }
336
337 #[inline]
338 pub fn len(&self) -> usize {
339 self.values.len()
340 }
341
342 #[inline]
343 pub fn is_empty(&self) -> bool {
344 self.values.is_empty()
345 }
346
347 pub fn iter(&self) -> impl ExactSizeIterator<Item = Option<TraceValue<'a>>> + '_ {
351 let format = self.format;
352 self.values.iter().enumerate().map(move |(index, value)| {
353 unsafe { read_trace_value(value, format.field_type(index)) }
356 })
357 }
358}