Refactor unsafe blocks

This commit is contained in:
Daniel Vilar 2018-09-12 09:44:46 +02:00
parent 1aaf429f9e
commit c92d63ee88
3 changed files with 108 additions and 107 deletions

View file

@ -73,40 +73,40 @@ lazy_static! {
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...");
gst_debug!(cat, obj: element, "Starting NDI connection...");
let mut receivers = hashmap_receivers.lock().unwrap();
let mut audio = false;
let mut video = false;
let mut receivers = hashmap_receivers.lock().unwrap();
let mut audio = false;
let mut video = false;
//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;
}
//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;
}
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){
continue;
}
else {
if video {
val.video = video;
}
else{
val.audio = audio;
}
return val.id;
}
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){
continue;
}
else {
if video {
val.video = video;
}
else{
val.audio = audio;
}
return val.id;
}
}
}
unsafe {
if !NDIlib_initialize() {
gst_element_error!(element, gst::CoreError::Negotiation, ["Cannot run NDI: NDIlib_initialize error"]);
// return false;
@ -217,30 +217,30 @@ fn connect_ndi(cat: gst::DebugCategory , element: &BaseSrc, ip: String, stream
fn stop_ndi(cat: gst::DebugCategory , element: &BaseSrc, id: i8) -> bool{
gst_debug!(cat, obj: element, "Closing NDI connection...");
unsafe{
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;
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;
}
let recv = &val.ndi_instance;
let pNDI_recv = recv.recv;
let recv = &val.ndi_instance;
let pNDI_recv = recv.recv;
unsafe{
NDIlib_recv_destroy(pNDI_recv);
// ndi_struct.recv = None;
NDIlib_destroy();
}
receivers.remove(&id);
gst_debug!(cat, obj: element, "Closed NDI connection");
return true;
}
receivers.remove(&id);
gst_debug!(cat, obj: element, "Closed NDI connection");
return true;
}
// Static plugin metdata that is directly stored in the plugin shared object and read by GStreamer

View file

@ -348,35 +348,35 @@ impl NdiAudioSrc {
fn fixate(&self, element: &BaseSrc, caps: gst::Caps) -> gst::Caps {
//We need to set the correct caps resolution and framerate
unsafe{
let receivers = hashmap_receivers.lock().unwrap();
let settings = self.settings.lock().unwrap();
let receivers = hashmap_receivers.lock().unwrap();
let settings = self.settings.lock().unwrap();
let receiver = receivers.get(&settings.id_receiver).unwrap();
let receiver = receivers.get(&settings.id_receiver).unwrap();
let recv = &receiver.ndi_instance;
let pNDI_recv = recv.recv;
let recv = &receiver.ndi_instance;
let pNDI_recv = recv.recv;
let audio_frame: NDIlib_audio_frame_v2_t = Default::default();
let audio_frame: NDIlib_audio_frame_v2_t = Default::default();
let mut frame_type: NDIlib_frame_type_e = NDIlib_frame_type_e::NDIlib_frame_type_none;
while frame_type != NDIlib_frame_type_e::NDIlib_frame_type_audio{
let mut frame_type: NDIlib_frame_type_e = NDIlib_frame_type_e::NDIlib_frame_type_none;
while frame_type != NDIlib_frame_type_e::NDIlib_frame_type_audio{
unsafe{
frame_type = NDIlib_recv_capture_v2(pNDI_recv, ptr::null(), &audio_frame, ptr::null(), 1000);
}
let mut caps = gst::Caps::truncate(caps);
{
let caps = caps.make_mut();
let s = caps.get_mut_structure(0).unwrap();
//s.fixate_field_nearest_int("rate", audio_frame.sample_rate);
s.fixate_field_nearest_int("rate", audio_frame.sample_rate / audio_frame.no_channels);
s.fixate_field_nearest_int("channels", audio_frame.no_channels);
}
// Let BaseSrc fixate anything else for us. We could've alternatively have
// called Caps::fixate() here
element.parent_fixate(caps)
}
let mut caps = gst::Caps::truncate(caps);
{
let caps = caps.make_mut();
let s = caps.get_mut_structure(0).unwrap();
//s.fixate_field_nearest_int("rate", audio_frame.sample_rate);
s.fixate_field_nearest_int("rate", audio_frame.sample_rate / audio_frame.no_channels);
s.fixate_field_nearest_int("channels", audio_frame.no_channels);
}
// Let BaseSrc fixate anything else for us. We could've alternatively have
// called Caps::fixate() here
element.parent_fixate(caps)
// }
}
//Creates the audio buffers
@ -401,15 +401,15 @@ impl NdiAudioSrc {
}
Some(ref info) => info.clone(),
};
let receivers = hashmap_receivers.lock().unwrap();
let recv = &receivers.get(&_settings.id_receiver).unwrap().ndi_instance;
let pNDI_recv = recv.recv;
let pts: u64;
let audio_frame: NDIlib_audio_frame_v2_t = Default::default();
unsafe{
let receivers = hashmap_receivers.lock().unwrap();
let recv = &receivers.get(&_settings.id_receiver).unwrap().ndi_instance;
let pNDI_recv = recv.recv;
let pts: u64;
let audio_frame: NDIlib_audio_frame_v2_t = Default::default();
let time = ndi_struct.initial_timestamp;
let mut skip_frame = true;

View file

@ -364,36 +364,36 @@ impl NdiVideoSrc {
fn fixate(&self, element: &BaseSrc, caps: gst::Caps) -> gst::Caps {
//We need to set the correct caps resolution and framerate
unsafe{
let receivers = hashmap_receivers.lock().unwrap();
let settings = self.settings.lock().unwrap();
let receivers = hashmap_receivers.lock().unwrap();
let settings = self.settings.lock().unwrap();
let receiver = receivers.get(&settings.id_receiver).unwrap();
let recv = &receiver.ndi_instance;
let pNDI_recv = recv.recv;
let receiver = receivers.get(&settings.id_receiver).unwrap();
let recv = &receiver.ndi_instance;
let pNDI_recv = recv.recv;
let video_frame: NDIlib_video_frame_v2_t = Default::default();
let video_frame: NDIlib_video_frame_v2_t = Default::default();
let mut frame_type: NDIlib_frame_type_e = NDIlib_frame_type_e::NDIlib_frame_type_none;
while frame_type != NDIlib_frame_type_e::NDIlib_frame_type_video{
let mut frame_type: NDIlib_frame_type_e = NDIlib_frame_type_e::NDIlib_frame_type_none;
while frame_type != NDIlib_frame_type_e::NDIlib_frame_type_video{
unsafe{
frame_type = NDIlib_recv_capture_v2(pNDI_recv, &video_frame, ptr::null(), ptr::null(), 1000);
}
let mut caps = gst::Caps::truncate(caps);
{
let caps = caps.make_mut();
let s = caps.get_mut_structure(0).unwrap();
s.fixate_field_nearest_int("width", video_frame.xres);
s.fixate_field_nearest_int("height", video_frame.yres);
s.fixate_field_nearest_fraction("framerate", Fraction::new(video_frame.frame_rate_N, video_frame.frame_rate_D));
}
// Let BaseSrc fixate anything else for us. We could've alternatively have
// called Caps::fixate() here
let _ = element.post_message(&gst::Message::new_latency().src(Some(element)).build());
element.parent_fixate(caps)
}
let mut caps = gst::Caps::truncate(caps);
{
let caps = caps.make_mut();
let s = caps.get_mut_structure(0).unwrap();
s.fixate_field_nearest_int("width", video_frame.xres);
s.fixate_field_nearest_int("height", video_frame.yres);
s.fixate_field_nearest_fraction("framerate", Fraction::new(video_frame.frame_rate_N, video_frame.frame_rate_D));
}
// Let BaseSrc fixate anything else for us. We could've alternatively have
// called Caps::fixate() here
let _ = element.post_message(&gst::Message::new_latency().src(Some(element)).build());
element.parent_fixate(caps)
}
//Creates the video buffers
@ -418,15 +418,16 @@ impl NdiVideoSrc {
}
Some(ref info) => info.clone(),
};
// unsafe{
let receivers = hashmap_receivers.lock().unwrap();
let recv = &receivers.get(&_settings.id_receiver).unwrap().ndi_instance;
let pNDI_recv = recv.recv;
let pts: u64;
let video_frame: NDIlib_video_frame_v2_t = Default::default();
unsafe{
let receivers = hashmap_receivers.lock().unwrap();
let recv = &receivers.get(&_settings.id_receiver).unwrap().ndi_instance;
let pNDI_recv = recv.recv;
let pts: u64;
let video_frame: NDIlib_video_frame_v2_t = Default::default();
let time = ndi_struct.initial_timestamp;
let mut skip_frame = true;