mirror of
https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs.git
synced 2025-01-23 01:18:11 +00:00
Refactor logic to use multiple streams in the same pipeline
Now it's possible to connect to N streams in the same pipeline. Each new connection will create a new NDI receiver except if it's a empty slot in the receiver. Each receiver has two slots one to connect to audio and other to connect to video to the same stream.
This commit is contained in:
parent
ba45931830
commit
07a8b8a274
5 changed files with 113 additions and 75 deletions
|
@ -1,7 +1,7 @@
|
|||
[package]
|
||||
name = "gst-plugin-ndi"
|
||||
version = "0.1.0"
|
||||
authors = ["Ruben Gonzalez <rubenrua@teltek.es>"]
|
||||
authors = ["Ruben Gonzalez <rubenrua@teltek.es>", "Daniel Vilar <daniel.peiteado@teltek.es>"]
|
||||
repository = "https://gitlab.teltek.es/rubenrua/ndi-rs.git"
|
||||
license = "unknow" # TODO MIT/Apache-2.0
|
||||
|
||||
|
@ -12,6 +12,7 @@ gstreamer = "0.11"
|
|||
gstreamer-base = "0.11"
|
||||
gstreamer-video = "0.11"
|
||||
gstreamer-audio = "0.11"
|
||||
lazy_static = "1.1.0"
|
||||
byte-slice-cast = "0.1" # TODO delete
|
||||
num-traits = "0.2" # TODO delete
|
||||
|
||||
|
|
|
@ -56,46 +56,64 @@ static mut ndi_struct: Ndi = Ndi{
|
|||
start_pts: 0,
|
||||
};
|
||||
|
||||
struct ndi_receiver_info{
|
||||
stream_name: String,
|
||||
ip: String,
|
||||
video: bool,
|
||||
audio: bool,
|
||||
ndi_instance: NdiInstance,
|
||||
id: i8,
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref hashmap_receivers: Mutex<HashMap<String, NdiInstance>> = {
|
||||
static ref hashmap_receivers: Mutex<HashMap<i8, ndi_receiver_info>> = {
|
||||
let mut m = HashMap::new();
|
||||
Mutex::new(m)
|
||||
};
|
||||
}
|
||||
|
||||
fn connect_ndi(cat: gst::DebugCategory , element: &BaseSrc, ip: String, stream_name: String) -> bool{
|
||||
static mut id_receiver: i8 = 0;
|
||||
|
||||
fn connect_ndi(cat: gst::DebugCategory , element: &BaseSrc, ip: String, stream_name: String) -> i8{
|
||||
unsafe {
|
||||
gst_debug!(cat, obj: element, "Starting NDI connection...");
|
||||
let mut map = hashmap_receivers.lock().unwrap();
|
||||
|
||||
//FIXME It's possible to connect more than one source if an audiosrc is active
|
||||
let mut receivers = hashmap_receivers.lock().unwrap();
|
||||
let mut audio = false;
|
||||
if (element.get_name().contains("ndiaudiosrc")){
|
||||
let mut video = false;
|
||||
id_receiver += 1;
|
||||
|
||||
//FIXME Search for another way to know if the source is an audio or a video source
|
||||
if (element.get_name().contains("audiosrc")){
|
||||
audio = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
video = true;
|
||||
}
|
||||
|
||||
if (map.contains_key(&stream_name) || map.contains_key(&ip)){
|
||||
if (map.contains_key(&stream_name)){
|
||||
let recv = map.get(&stream_name).unwrap();
|
||||
audio = recv.audio;
|
||||
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)){
|
||||
break;
|
||||
}
|
||||
else {
|
||||
if (video){
|
||||
val.video = video;
|
||||
}
|
||||
else{
|
||||
val.audio = audio;
|
||||
}
|
||||
return val.id;
|
||||
}
|
||||
}
|
||||
else if (map.contains_key(&ip)){
|
||||
let recv = map.get(&ip).unwrap();
|
||||
audio = recv.audio;
|
||||
}
|
||||
if (audio){
|
||||
println!("We already have an source, but we need to add an audio source to the pipeline. So we need to reutilize the source...");
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
println!("Already connected to {}{}", ip, stream_name);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if !NDIlib_initialize() {
|
||||
gst_element_error!(element, gst::CoreError::Negotiation, ["Cannot run NDI: NDIlib_initialize error"]);
|
||||
return false;
|
||||
// return false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
let mut source: NDIlib_source_t = NDIlib_source_t{p_ndi_name: ptr::null(),
|
||||
|
@ -107,7 +125,8 @@ fn connect_ndi(cat: gst::DebugCategory , element: &BaseSrc, ip: String, stream
|
|||
let ip_ptr = CString::new(ip.clone()).unwrap();
|
||||
if pNDI_find.is_null() {
|
||||
gst_element_error!(element, gst::CoreError::Negotiation, ["Cannot run NDI: NDIlib_find_create_v2 error"]);
|
||||
return false;
|
||||
// return false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
let mut total_sources: u32 = 0;
|
||||
|
@ -120,7 +139,8 @@ fn connect_ndi(cat: gst::DebugCategory , element: &BaseSrc, ip: String, stream
|
|||
// We need at least one source
|
||||
if p_sources.is_null() {
|
||||
gst_element_error!(element, gst::CoreError::Negotiation, ["Error getting NDIlib_find_get_current_sources"]);
|
||||
return false;
|
||||
// return false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
let mut no_source: isize = -1;
|
||||
|
@ -133,7 +153,8 @@ fn connect_ndi(cat: gst::DebugCategory , element: &BaseSrc, ip: String, stream
|
|||
}
|
||||
if no_source == -1 {
|
||||
gst_element_error!(element, gst::CoreError::Negotiation, ["Stream name not found"]);
|
||||
return false;
|
||||
// return false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
gst_debug!(cat, obj: element, "Total sources in network {}: Connecting to NDI source with name '{}' and address '{}'", total_sources,
|
||||
|
@ -163,7 +184,8 @@ fn connect_ndi(cat: gst::DebugCategory , element: &BaseSrc, ip: String, stream
|
|||
if pNDI_recv.is_null() {
|
||||
//println!("Cannot run NDI: NDIlib_recv_create_v3 error.");
|
||||
gst_element_error!(element, gst::CoreError::Negotiation, ["Cannot run NDI: NDIlib_recv_create_v3 error"]);
|
||||
return false;
|
||||
// return false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Destroy the NDI finder. We needed to have access to the pointers to p_sources[0]
|
||||
|
@ -173,7 +195,7 @@ fn connect_ndi(cat: gst::DebugCategory , element: &BaseSrc, ip: String, stream
|
|||
let tally_state: NDIlib_tally_t = Default::default();
|
||||
NDIlib_recv_set_tally(pNDI_recv, &tally_state);
|
||||
|
||||
// Enable Hardwqre Decompression support if this support has it. Please read the caveats in the documentation
|
||||
// Enable Hardware Decompression support if this support has it. Please read the caveats in the documentation
|
||||
// regarding this. There are times in which it might reduce the performance although on small stream numbers
|
||||
// it almost always yields the same or better performance.
|
||||
let data = CString::new("<ndi_hwaccel enabled=\"true\"/>").unwrap();
|
||||
|
@ -185,8 +207,7 @@ fn connect_ndi(cat: gst::DebugCategory , element: &BaseSrc, ip: String, stream
|
|||
|
||||
NDIlib_recv_send_metadata(pNDI_recv, &enable_hw_accel);
|
||||
|
||||
map.insert(source_name.clone(), NdiInstance{recv: pNDI_recv, audio: audio});
|
||||
map.insert(source_ip.clone(), NdiInstance{recv: pNDI_recv, audio: audio});
|
||||
receivers.insert(id_receiver, ndi_receiver_info{stream_name: source_name.clone(), ip: source_ip.clone(), video:video, audio: audio, ndi_instance: NdiInstance{recv: pNDI_recv}, id: id_receiver});
|
||||
|
||||
// let start = SystemTime::now();
|
||||
// let since_the_epoch = start.duration_since(UNIX_EPOCH)
|
||||
|
@ -195,26 +216,34 @@ fn connect_ndi(cat: gst::DebugCategory , element: &BaseSrc, ip: String, stream
|
|||
// ndi_struct.start_pts = Some(since_the_epoch.as_secs() * 1000000000 +
|
||||
// since_the_epoch.subsec_nanos() as u64);
|
||||
gst_debug!(cat, obj: element, "Started NDI connection");
|
||||
return true;
|
||||
return id_receiver;
|
||||
}
|
||||
}
|
||||
|
||||
fn stop_ndi(cat: gst::DebugCategory , element: &BaseSrc) -> bool{
|
||||
fn stop_ndi(cat: gst::DebugCategory , element: &BaseSrc, id: i8) -> bool{
|
||||
gst_debug!(cat, obj: element, "Closing NDI connection...");
|
||||
unsafe{
|
||||
let recv = match ndi_struct.recv{
|
||||
None => {
|
||||
//TODO Update gst_element_error with one more descriptive
|
||||
//println!("pNDI_recv no encontrado");
|
||||
//gst_element_error!(element, gst::CoreError::Negotiation, ["No encontramos ndi recv"]);
|
||||
let mut receivers = hashmap_receivers.lock().unwrap();
|
||||
|
||||
{
|
||||
let val = receivers.get_mut(&id).unwrap();
|
||||
if val.video && val.audio{
|
||||
if (element.get_name().contains("audiosrc")){
|
||||
val.audio = false;
|
||||
}
|
||||
else{
|
||||
val.video = false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Some(ref recv) => recv.clone(),
|
||||
};
|
||||
let pNDI_recv = recv.recv;
|
||||
NDIlib_recv_destroy(pNDI_recv);
|
||||
ndi_struct.recv = None;
|
||||
NDIlib_destroy();
|
||||
|
||||
let recv = &val.ndi_instance;
|
||||
let pNDI_recv = recv.recv;
|
||||
NDIlib_recv_destroy(pNDI_recv);
|
||||
// ndi_struct.recv = None;
|
||||
NDIlib_destroy();
|
||||
}
|
||||
receivers.remove(&id);
|
||||
gst_debug!(cat, obj: element, "Closed NDI connection");
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -28,6 +28,7 @@ use hashmap_receivers;
|
|||
struct Settings {
|
||||
stream_name: String,
|
||||
ip: String,
|
||||
id_receiver: i8,
|
||||
}
|
||||
|
||||
impl Default for Settings {
|
||||
|
@ -35,6 +36,7 @@ impl Default for Settings {
|
|||
Settings {
|
||||
stream_name: String::from("Fixed ndi stream name"),
|
||||
ip: String::from(""),
|
||||
id_receiver: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -260,16 +262,23 @@ impl NdiAudioSrc {
|
|||
// Reset state
|
||||
*self.state.lock().unwrap() = Default::default();
|
||||
|
||||
let settings = self.settings.lock().unwrap();
|
||||
return connect_ndi(self.cat, element, settings.ip.clone(), settings.stream_name.clone());
|
||||
|
||||
let mut settings = self.settings.lock().unwrap();
|
||||
settings.id_receiver = connect_ndi(self.cat, element, settings.ip.clone(), settings.stream_name.clone());
|
||||
if settings.id_receiver == 0{
|
||||
return false;
|
||||
}
|
||||
else{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Called when shutting down the element so we can release all stream-related state
|
||||
fn stop(&self, element: &BaseSrc) -> bool {
|
||||
// Reset state
|
||||
*self.state.lock().unwrap() = Default::default();
|
||||
stop_ndi(self.cat, element);
|
||||
|
||||
let settings = self.settings.lock().unwrap();
|
||||
stop_ndi(self.cat, element, settings.id_receiver.clone());
|
||||
// Commented because when adding ndi destroy stopped in this line
|
||||
//*self.state.lock().unwrap() = Default::default();
|
||||
true
|
||||
|
@ -320,16 +329,12 @@ impl NdiAudioSrc {
|
|||
fn fixate(&self, element: &BaseSrc, caps: gst::Caps) -> gst::Caps {
|
||||
//We need to set the correct caps resolution and framerate
|
||||
unsafe{
|
||||
let map = hashmap_receivers.lock().unwrap();
|
||||
let receivers = hashmap_receivers.lock().unwrap();
|
||||
let settings = self.settings.lock().unwrap();
|
||||
|
||||
let mut id = &settings.stream_name;
|
||||
if (&settings.ip != ""){
|
||||
id = &settings.ip;
|
||||
}
|
||||
let recv = map.get(id).unwrap();
|
||||
|
||||
let recv = &receivers.get(&settings.id_receiver).unwrap().ndi_instance;
|
||||
let pNDI_recv = recv.recv;
|
||||
|
||||
let mut timestamp_data = self.timestamp_data.lock().unwrap();
|
||||
|
||||
let audio_frame: NDIlib_audio_frame_v2_t = Default::default();
|
||||
|
@ -380,13 +385,15 @@ impl NdiAudioSrc {
|
|||
Some(ref info) => info.clone(),
|
||||
};
|
||||
unsafe{
|
||||
let map = hashmap_receivers.lock().unwrap();
|
||||
let receivers = hashmap_receivers.lock().unwrap();
|
||||
|
||||
let mut id = &_settings.stream_name;
|
||||
|
||||
if (&_settings.ip != ""){
|
||||
id = &_settings.ip;
|
||||
}
|
||||
let recv = map.get(id).unwrap();
|
||||
|
||||
let recv = &receivers.get(&_settings.id_receiver).unwrap().ndi_instance;
|
||||
|
||||
let pNDI_recv = recv.recv;
|
||||
|
||||
|
|
|
@ -152,7 +152,7 @@ pub type NDIlib_recv_instance_t = *mut ::std::os::raw::c_void;
|
|||
//Rust wrapper around *mut ::std::os::raw::c_void
|
||||
pub struct NdiInstance {
|
||||
pub recv: NDIlib_recv_instance_t,
|
||||
pub audio: bool,
|
||||
// pub audio: bool,
|
||||
}
|
||||
|
||||
unsafe impl ::std::marker::Send for NdiInstance {}
|
||||
|
|
|
@ -29,6 +29,7 @@ use hashmap_receivers;
|
|||
struct Settings {
|
||||
stream_name: String,
|
||||
ip: String,
|
||||
id_receiver: i8,
|
||||
}
|
||||
|
||||
impl Default for Settings {
|
||||
|
@ -36,6 +37,7 @@ impl Default for Settings {
|
|||
Settings {
|
||||
stream_name: String::from("Fixed ndi stream name"),
|
||||
ip: String::from(""),
|
||||
id_receiver: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -266,15 +268,23 @@ impl NdiVideoSrc {
|
|||
// Reset state
|
||||
*self.state.lock().unwrap() = Default::default();
|
||||
|
||||
let settings = self.settings.lock().unwrap();
|
||||
return connect_ndi(self.cat, element, settings.ip.clone(), settings.stream_name.clone());
|
||||
let mut settings = self.settings.lock().unwrap();
|
||||
settings.id_receiver = connect_ndi(self.cat, element, settings.ip.clone(), settings.stream_name.clone());
|
||||
if settings.id_receiver == 0{
|
||||
return false;
|
||||
}
|
||||
else{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Called when shutting down the element so we can release all stream-related state
|
||||
fn stop(&self, element: &BaseSrc) -> bool {
|
||||
// Reset state
|
||||
*self.state.lock().unwrap() = Default::default();
|
||||
stop_ndi(self.cat, element);
|
||||
|
||||
let settings = self.settings.lock().unwrap();
|
||||
stop_ndi(self.cat, element, settings.id_receiver.clone());
|
||||
// Commented because when adding ndi destroy stopped in this line
|
||||
//*self.state.lock().unwrap() = Default::default();
|
||||
true
|
||||
|
@ -326,16 +336,12 @@ impl NdiVideoSrc {
|
|||
fn fixate(&self, element: &BaseSrc, caps: gst::Caps) -> gst::Caps {
|
||||
//We need to set the correct caps resolution and framerate
|
||||
unsafe{
|
||||
let map = hashmap_receivers.lock().unwrap();
|
||||
let receivers = hashmap_receivers.lock().unwrap();
|
||||
let settings = self.settings.lock().unwrap();
|
||||
|
||||
let mut id = &settings.stream_name;
|
||||
if (&settings.ip != ""){
|
||||
id = &settings.ip;
|
||||
}
|
||||
let recv = map.get(id).unwrap();
|
||||
|
||||
let recv = &receivers.get(&settings.id_receiver).unwrap().ndi_instance;
|
||||
let pNDI_recv = recv.recv;
|
||||
|
||||
let mut timestamp_data = self.timestamp_data.lock().unwrap();
|
||||
|
||||
let video_frame: NDIlib_video_frame_v2_t = Default::default();
|
||||
|
@ -387,14 +393,9 @@ impl NdiVideoSrc {
|
|||
Some(ref info) => info.clone(),
|
||||
};
|
||||
unsafe{
|
||||
let map = hashmap_receivers.lock().unwrap();
|
||||
let mut id = &_settings.stream_name;
|
||||
|
||||
if (&_settings.ip != ""){
|
||||
id = &_settings.ip;
|
||||
}
|
||||
let recv = map.get(id).unwrap();
|
||||
let receivers = hashmap_receivers.lock().unwrap();
|
||||
|
||||
let recv = &receivers.get(&_settings.id_receiver).unwrap().ndi_instance;
|
||||
let pNDI_recv = recv.recv;
|
||||
|
||||
let pts: u64;
|
||||
|
|
Loading…
Reference in a new issue