2019-02-01 12:36:52 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
use gio;
|
|
|
|
use glib;
|
|
|
|
use glib::translate::*;
|
|
|
|
use gst;
|
|
|
|
use gst::prelude::*;
|
2019-03-19 07:58:20 +00:00
|
|
|
use gst_net_sys;
|
2019-02-01 12:36:52 +00:00
|
|
|
|
2020-10-24 17:06:59 +00:00
|
|
|
#[repr(transparent)]
|
2019-03-19 07:58:20 +00:00
|
|
|
pub struct NetAddressMeta(gst_net_sys::GstNetAddressMeta);
|
2019-02-01 12:36:52 +00:00
|
|
|
|
2019-12-18 15:04:42 +00:00
|
|
|
unsafe impl Send for NetAddressMeta {}
|
|
|
|
unsafe impl Sync for NetAddressMeta {}
|
|
|
|
|
2019-02-01 12:36:52 +00:00
|
|
|
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!();
|
2019-02-01 12:36:52 +00:00
|
|
|
unsafe {
|
2019-03-19 07:58:20 +00:00
|
|
|
let meta = gst_net_sys::gst_buffer_add_net_address_meta(
|
2019-02-01 12:36:52 +00:00
|
|
|
buffer.as_mut_ptr(),
|
|
|
|
addr.as_ref().to_glib_none().0,
|
|
|
|
);
|
|
|
|
Self::from_mut_ptr(buffer, meta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_addr(&self) -> gio::SocketAddress {
|
|
|
|
unsafe { from_glib_none(self.0.addr) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_addr<T: IsA<gio::SocketAddress>>(&mut self, addr: &T) {
|
2019-02-28 08:32:13 +00:00
|
|
|
#![allow(clippy::cast_ptr_alignment)]
|
2019-02-01 12:36:52 +00:00
|
|
|
unsafe {
|
2019-03-19 07:58:20 +00:00
|
|
|
gobject_sys::g_object_unref(self.0.addr as *mut _);
|
2019-02-01 12:36:52 +00:00
|
|
|
self.0.addr = addr.as_ref().to_glib_full();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl MetaAPI for NetAddressMeta {
|
2019-03-19 07:58:20 +00:00
|
|
|
type GstType = gst_net_sys::GstNetAddressMeta;
|
2019-02-01 12:36:52 +00:00
|
|
|
|
|
|
|
fn get_meta_api() -> glib::Type {
|
2019-03-19 07:58:20 +00:00
|
|
|
unsafe { from_glib(gst_net_sys::gst_net_address_meta_api_get_type()) }
|
2019-02-01 12:36:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for NetAddressMeta {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.debug_struct("NetAddressMeta")
|
|
|
|
.field("addr", &self.get_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;
|
2020-10-12 06:48:03 +00:00
|
|
|
let inet_addr = gio::InetAddress::from_string("127.0.0.1").unwrap();
|
2019-02-01 12:36:52 +00:00
|
|
|
|
|
|
|
let expected_addr = &gio::InetSocketAddress::new(&inet_addr, port);
|
|
|
|
|
|
|
|
let expected_inet_addr = expected_addr.get_address().unwrap();
|
|
|
|
|
|
|
|
{
|
|
|
|
let meta = NetAddressMeta::add(
|
|
|
|
buffer.get_mut().unwrap(),
|
|
|
|
&gio::InetSocketAddress::new(&inet_addr, port),
|
|
|
|
);
|
|
|
|
|
|
|
|
let actual_addr = meta
|
|
|
|
.get_addr()
|
|
|
|
.downcast::<gio::InetSocketAddress>()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(actual_addr.get_port(), expected_addr.get_port());
|
|
|
|
|
|
|
|
let actual_inet_addr = actual_addr.get_address().unwrap();
|
|
|
|
|
|
|
|
assert!(actual_inet_addr.equal(&expected_inet_addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let meta = buffer.get_meta::<NetAddressMeta>().unwrap();
|
|
|
|
let actual_addr = meta
|
|
|
|
.get_addr()
|
|
|
|
.downcast::<gio::InetSocketAddress>()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(actual_addr.get_port(), expected_addr.get_port());
|
|
|
|
|
|
|
|
let actual_inet_addr = actual_addr.get_address().unwrap();
|
|
|
|
|
|
|
|
assert!(actual_inet_addr.equal(&expected_inet_addr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|