gst-plugins-rs/src/lib.rs

264 lines
7.2 KiB
Rust
Raw Normal View History

2018-06-26 12:07:43 +00:00
#![allow(non_camel_case_types, non_upper_case_globals, non_snake_case)]
2018-04-09 05:53:04 +00:00
extern crate glib;
extern crate gobject_subclass;
2018-04-09 05:53:04 +00:00
#[macro_use]
extern crate gst_plugin;
#[macro_use]
extern crate gstreamer as gst;
use gst::prelude::*;
2018-04-09 05:53:04 +00:00
extern crate gstreamer_audio as gst_audio;
extern crate gstreamer_base as gst_base;
extern crate gstreamer_video as gst_video;
#[macro_use]
extern crate lazy_static;
2018-04-09 05:53:04 +00:00
2018-06-12 12:40:17 +00:00
mod ndiaudiosrc;
2018-09-18 09:52:09 +00:00
pub mod ndisys;
2018-09-18 09:53:12 +00:00
mod ndivideosrc;
use gst_plugin::base_src::*;
2018-09-18 09:53:12 +00:00
use ndisys::*;
use std::ffi::{CStr, CString};
use std::{thread, time};
use std::collections::HashMap;
use std::sync::Mutex;
2018-04-09 05:53:04 +00:00
use gst::GstObjectExt;
2018-04-09 05:53:04 +00:00
fn plugin_init(plugin: &gst::Plugin) -> bool {
2018-06-12 12:40:17 +00:00
ndivideosrc::register(plugin);
ndiaudiosrc::register(plugin);
2018-04-09 05:53:04 +00:00
true
}
2018-09-18 09:53:12 +00:00
struct ndi_receiver_info {
stream_name: String,
ip: String,
video: bool,
audio: bool,
ndi_instance: NdiInstance,
initial_timestamp: u64,
id: i8,
}
2018-09-18 09:53:12 +00:00
struct Ndi {
2018-08-24 16:00:02 +00:00
start_pts: gst::ClockTime,
}
2018-09-18 09:53:12 +00:00
static mut ndi_struct: Ndi = Ndi {
2018-08-24 16:00:02 +00:00
start_pts: gst::ClockTime(Some(0)),
};
lazy_static! {
static ref hashmap_receivers: Mutex<HashMap<i8, ndi_receiver_info>> = {
2018-08-20 10:14:54 +00:00
let m = HashMap::new();
Mutex::new(m)
};
}
static mut id_receiver: i8 = 0;
2018-09-18 11:12:04 +00:00
fn connect_ndi(cat: gst::DebugCategory, element: &BaseSrc, ip: &str, stream_name: &str) -> i8 {
2018-09-12 07:44:46 +00:00
gst_debug!(cat, obj: element, "Starting NDI connection...");
2018-09-12 07:44:46 +00:00
let mut receivers = hashmap_receivers.lock().unwrap();
let mut audio = false;
let mut video = false;
2018-09-25 14:04:00 +00:00
if element
.get_factory()
.map(|f| f.get_name() == "ndiaudiosrc")
.unwrap_or(false)
{
2018-09-12 07:44:46 +00:00
audio = true;
2018-09-18 09:53:12 +00:00
} else {
2018-09-12 07:44:46 +00:00
video = true;
}
2018-09-18 09:53:12 +00:00
for val in receivers.values_mut() {
if val.ip == ip || val.stream_name == stream_name {
if (val.audio && val.video) || (val.audio && audio) || (val.video && video) {
2018-09-12 07:44:46 +00:00
continue;
2018-09-18 09:53:12 +00:00
} else {
2018-09-12 07:44:46 +00:00
if video {
val.video = video;
2018-09-18 09:53:12 +00:00
} else {
2018-09-12 07:44:46 +00:00
val.audio = audio;
}
2018-09-12 07:44:46 +00:00
return val.id;
}
}
2018-09-12 07:44:46 +00:00
}
unsafe {
if !NDIlib_initialize() {
2018-09-18 09:53:12 +00:00
gst_element_error!(
element,
gst::CoreError::Negotiation,
["Cannot run NDI: NDIlib_initialize error"]
);
return 0;
}
2018-08-20 10:14:54 +00:00
let NDI_find_create_desc: NDIlib_find_create_t = Default::default();
let pNDI_find = NDIlib_find_create_v2(&NDI_find_create_desc);
if pNDI_find.is_null() {
2018-09-18 09:53:12 +00:00
gst_element_error!(
element,
gst::CoreError::Negotiation,
["Cannot run NDI: NDIlib_find_create_v2 error"]
);
2018-08-20 10:14:54 +00:00
return 0;
}
2018-08-20 10:14:54 +00:00
let mut total_sources: u32 = 0;
let p_sources;
2018-06-27 09:56:11 +00:00
2018-08-20 10:14:54 +00:00
// TODO Sleep 1s to wait for all sources
thread::sleep(time::Duration::from_millis(2000));
p_sources = NDIlib_find_get_current_sources(pNDI_find, &mut total_sources as *mut u32);
2018-08-20 10:14:54 +00:00
// We need at least one source
if p_sources.is_null() {
2018-09-18 09:53:12 +00:00
gst_element_error!(
element,
gst::CoreError::Negotiation,
["Error getting NDIlib_find_get_current_sources"]
);
2018-08-20 10:14:54 +00:00
return 0;
}
2018-08-20 10:14:54 +00:00
let mut no_source: isize = -1;
2018-09-18 09:53:12 +00:00
for i in 0..total_sources as isize {
if CStr::from_ptr((*p_sources.offset(i)).p_ndi_name)
.to_string_lossy()
.into_owned()
== stream_name
|| CStr::from_ptr((*p_sources.offset(i)).p_ip_address)
.to_string_lossy()
.into_owned()
== ip
{
2018-08-20 10:14:54 +00:00
no_source = i;
break;
}
}
2018-09-18 09:53:12 +00:00
if no_source == -1 {
2018-08-24 16:00:02 +00:00
gst_element_error!(element, gst::ResourceError::OpenRead, ["Stream not found"]);
2018-08-20 10:14:54 +00:00
return 0;
}
2018-06-27 09:56:11 +00:00
2018-09-18 09:53:12 +00:00
gst_debug!(
cat,
obj: element,
"Total sources in network {}: Connecting to NDI source with name '{}' and address '{}'",
total_sources,
CStr::from_ptr((*p_sources.offset(no_source)).p_ndi_name)
.to_string_lossy()
.into_owned(),
CStr::from_ptr((*p_sources.offset(no_source)).p_ip_address)
.to_string_lossy()
.into_owned()
);
2018-06-27 09:56:11 +00:00
2018-09-18 11:12:04 +00:00
let source = *p_sources.offset(no_source);
2018-09-18 09:53:12 +00:00
let source_ip = CStr::from_ptr(source.p_ip_address)
.to_string_lossy()
.into_owned();
let source_name = CStr::from_ptr(source.p_ndi_name)
.to_string_lossy()
.into_owned();
2018-06-27 09:56:11 +00:00
let p_ndi_name = CString::new("Galicaster NDI Receiver").unwrap();
let NDI_recv_create_desc = NDIlib_recv_create_v3_t {
source_to_connect_to: source,
p_ndi_name: p_ndi_name.as_ptr(),
..Default::default()
};
let pNDI_recv = NDIlib_recv_create_v3(&NDI_recv_create_desc);
if pNDI_recv.is_null() {
2018-09-18 09:53:12 +00:00
gst_element_error!(
element,
gst::CoreError::Negotiation,
["Cannot run NDI: NDIlib_recv_create_v3 error"]
);
return 0;
}
2018-06-27 09:56:11 +00:00
NDIlib_find_destroy(pNDI_find);
let tally_state: NDIlib_tally_t = Default::default();
NDIlib_recv_set_tally(pNDI_recv, &tally_state);
let data = CString::new("<ndi_hwaccel enabled=\"true\"/>").unwrap();
let enable_hw_accel = NDIlib_metadata_frame_t {
length: data.to_bytes().len() as i32,
timecode: 0,
p_data: data.as_ptr(),
};
NDIlib_recv_send_metadata(pNDI_recv, &enable_hw_accel);
id_receiver += 1;
2018-09-18 09:53:12 +00:00
receivers.insert(
id_receiver,
ndi_receiver_info {
stream_name: source_name.clone(),
ip: source_ip.clone(),
2018-09-18 11:12:04 +00:00
video,
audio,
2018-09-18 09:53:12 +00:00
ndi_instance: NdiInstance { recv: pNDI_recv },
initial_timestamp: 0,
2018-09-18 09:53:12 +00:00
id: id_receiver,
},
);
2018-06-27 09:56:11 +00:00
gst_debug!(cat, obj: element, "Started NDI connection");
2018-09-18 11:12:04 +00:00
id_receiver
}
2018-06-27 09:56:11 +00:00
}
2018-09-18 09:53:12 +00:00
fn stop_ndi(cat: gst::DebugCategory, element: &BaseSrc, id: i8) -> bool {
2018-06-27 09:56:11 +00:00
gst_debug!(cat, obj: element, "Closing NDI connection...");
2018-09-12 07:44:46 +00:00
let mut receivers = hashmap_receivers.lock().unwrap();
{
let val = receivers.get_mut(&id).unwrap();
2018-09-18 09:53:12 +00:00
if val.video && val.audio {
if element.get_name().contains("audiosrc") {
2018-09-12 07:44:46 +00:00
val.audio = false;
2018-09-18 09:53:12 +00:00
} else {
2018-09-12 07:44:46 +00:00
val.video = false;
}
return true;
}
2018-09-12 07:44:46 +00:00
let recv = &val.ndi_instance;
let pNDI_recv = recv.recv;
2018-09-18 09:53:12 +00:00
unsafe {
NDIlib_recv_destroy(pNDI_recv);
// ndi_struct.recv = None;
NDIlib_destroy();
}
}
2018-09-12 07:44:46 +00:00
receivers.remove(&id);
gst_debug!(cat, obj: element, "Closed NDI connection");
2018-09-18 11:12:04 +00:00
true
2018-06-27 09:56:11 +00:00
}
2018-06-27 09:56:11 +00:00
plugin_define!(
b"ndi\0",
b"NewTek NDI Plugin\0",
plugin_init,
2018-09-18 11:25:24 +00:00
b"1.0.0\0",
b"LGPL\0",
2018-06-27 09:56:11 +00:00
b"ndi\0",
b"ndi\0",
2018-09-18 11:25:24 +00:00
b"https://github.com/teltek/gst-plugin-ndi\0",
2018-06-27 09:56:11 +00:00
b"2018-04-09\0"
);