Skip to main content

gstreamer_tag/
sample_ext.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use crate::{TagImageType, ffi};
5use glib::translate::{IntoGlib as _, ToGlibPtr as _, from_glib_full};
6
7pub trait ImageSampleExt: Sized {
8    // rustdoc-stripper-ignore-next
9    /// # Example
10    /// ```
11    /// # use gstreamer_tag as gst_tag;
12    /// # fn test() -> Result<(), glib::BoolError> {
13    /// # let data = Vec::new();
14    /// use gst_tag::prelude::*;
15    /// // let data: Vec<u8> = ...;
16    /// let sample = gst::Sample::from_image_data(&data, gst_tag::TagImageType::FrontCover)?;
17    /// # Ok(())
18    /// # }
19    /// ```
20    #[doc(alias = "gst_tag_image_data_to_image_sample")]
21    fn from_image_data(data: &[u8], image_type: TagImageType) -> Result<Self, glib::BoolError>;
22}
23
24impl ImageSampleExt for gst::Sample {
25    fn from_image_data(data: &[u8], image_type: TagImageType) -> Result<Self, glib::BoolError> {
26        assert_initialized_main_thread!();
27        let data_len = u32::try_from(data.len())
28            .map_err(|_| glib::bool_error!("image is larger than 4GiB"))?;
29        let sample: Option<_> = unsafe {
30            from_glib_full(ffi::gst_tag_image_data_to_image_sample(
31                data.to_glib_none().0,
32                data_len,
33                image_type.into_glib(),
34            ))
35        };
36        sample.ok_or_else(|| glib::bool_error!("invalid or unsupported image data"))
37    }
38}