gstreamer-rs/gstreamer-net/src/net_address_meta.rs

103 lines
2.8 KiB
Rust
Raw Normal View History

2020-12-15 10:53:31 +00:00
// Take a look at the license at the top of the repository in the LICENSE file.
use std::fmt;
use glib::translate::*;
use gst::prelude::*;
#[repr(transparent)]
pub struct NetAddressMeta(ffi::GstNetAddressMeta);
unsafe impl Send for NetAddressMeta {}
unsafe impl Sync for NetAddressMeta {}
impl NetAddressMeta {
pub fn add<'a, A: IsA<gio::SocketAddress>>(
buffer: &'a mut gst::BufferRef,
addr: &A,
) -> gst::MetaRefMut<'a, Self, gst::meta::Standalone> {
2020-03-22 14:18:47 +00:00
skip_assert_initialized!();
unsafe {
let meta = ffi::gst_buffer_add_net_address_meta(
buffer.as_mut_ptr(),
addr.as_ref().to_glib_none().0,
);
Self::from_mut_ptr(buffer, meta)
}
}
2021-04-11 19:39:50 +00:00
pub fn addr(&self) -> gio::SocketAddress {
unsafe { from_glib_none(self.0.addr) }
}
pub fn set_addr<T: IsA<gio::SocketAddress>>(&mut self, addr: &T) {
#![allow(clippy::cast_ptr_alignment)]
unsafe {
glib::gobject_ffi::g_object_unref(self.0.addr as *mut _);
self.0.addr = addr.as_ref().to_glib_full();
}
}
}
unsafe impl MetaAPI for NetAddressMeta {
type GstType = ffi::GstNetAddressMeta;
2021-04-20 10:23:24 +00:00
fn meta_api() -> glib::Type {
unsafe { from_glib(ffi::gst_net_address_meta_api_get_type()) }
}
}
impl fmt::Debug for NetAddressMeta {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("NetAddressMeta")
2021-04-11 19:39:50 +00:00
.field("addr", &self.addr())
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
use gio::InetAddressExt;
use gio::InetSocketAddressExt;
#[test]
fn test_add_get_meta() {
gst::init().unwrap();
let mut buffer = gst::Buffer::new();
let port = 5000;
let inet_addr = gio::InetAddress::from_string("127.0.0.1").unwrap();
let expected_addr = &gio::InetSocketAddress::new(&inet_addr, port);
2021-04-11 19:39:50 +00:00
let expected_inet_addr = expected_addr.address();
{
let meta = NetAddressMeta::add(
buffer.get_mut().unwrap(),
&gio::InetSocketAddress::new(&inet_addr, port),
);
2021-04-11 19:39:50 +00:00
let actual_addr = meta.addr().downcast::<gio::InetSocketAddress>().unwrap();
2021-04-11 19:39:50 +00:00
assert_eq!(actual_addr.port(), expected_addr.port());
2021-04-11 19:39:50 +00:00
let actual_inet_addr = actual_addr.address();
assert!(actual_inet_addr.equal(&expected_inet_addr));
}
{
2021-04-20 10:24:17 +00:00
let meta = buffer.meta::<NetAddressMeta>().unwrap();
2021-04-11 19:39:50 +00:00
let actual_addr = meta.addr().downcast::<gio::InetSocketAddress>().unwrap();
2021-04-11 19:39:50 +00:00
assert_eq!(actual_addr.port(), expected_addr.port());
2021-04-11 19:39:50 +00:00
let actual_inet_addr = actual_addr.address();
assert!(actual_inet_addr.equal(&expected_inet_addr));
}
}
}