diff options
author | Sebastian Dröge <sebastian@centricular.com> | 2020-06-22 11:03:52 +0300 |
---|---|---|
committer | Sebastian Dröge <sebastian@centricular.com> | 2020-06-22 11:28:19 +0300 |
commit | 9bb3e75fb9f64b531fa5ac60776c4c934b635e47 (patch) | |
tree | 7bd5447060340b6b0e12975734f1f9a94c610caa /utils | |
parent | c917e77687f6d7b4de22c5a27bcf48a27982b2cc (diff) |
Update to use the new pad builders for safely setting pad functions
Only two uses of unsafely setting the pad functions is left:
- fallbacksrc for overriding the chain function of the proxy pad of a
ghost pad
- threadshare for overriding the pad functions after creationg, which
probably needs some fixing at some point
Diffstat (limited to 'utils')
-rw-r--r-- | utils/fallbackswitch/src/fallbacksrc.rs | 45 | ||||
-rw-r--r-- | utils/togglerecord/src/togglerecord.rs | 169 |
2 files changed, 138 insertions, 76 deletions
diff --git a/utils/fallbackswitch/src/fallbacksrc.rs b/utils/fallbackswitch/src/fallbacksrc.rs index 9883bb32f..1a9325c44 100644 --- a/utils/fallbackswitch/src/fallbacksrc.rs +++ b/utils/fallbackswitch/src/fallbacksrc.rs @@ -704,7 +704,11 @@ impl FallbackSrc { }; input - .add_pad(&gst::GhostPad::new(Some("src"), &srcpad).unwrap()) + .add_pad( + &gst::GhostPad::builder(Some("src"), gst::PadDirection::Src) + .build_with_target(&srcpad) + .unwrap(), + ) .unwrap(); Ok(input.upcast()) @@ -724,7 +728,11 @@ impl FallbackSrc { let srcpad = audiotestsrc.get_static_pad("src").unwrap(); input - .add_pad(&gst::GhostPad::new(Some("src"), &srcpad).unwrap()) + .add_pad( + &gst::GhostPad::builder(Some("src"), gst::PadDirection::Src) + .build_with_target(&srcpad) + .unwrap(), + ) .unwrap(); Ok(input.upcast()) @@ -772,22 +780,27 @@ impl FallbackSrc { let templ = element .get_pad_template(if is_audio { "audio" } else { "video" }) .unwrap(); - let ghostpad = - gst::GhostPad::from_template(Some(&templ.get_name()), &srcpad, &templ).unwrap(); - - element.add_pad(&ghostpad).unwrap(); + let ghostpad = gst::GhostPad::builder_with_template(&templ, Some(&templ.get_name())) + .build_with_target(&srcpad) + .unwrap(); let proxypad = ghostpad.get_internal().expect("no internal pad"); let element_weak = element.downgrade(); - proxypad.set_chain_function(move |pad, _parent, buffer| { - let element = match element_weak.upgrade() { - None => return Err(gst::FlowError::Flushing), - Some(element) => element, - }; + // Safety: Nothing else can have a reference to the proxy pad yet apart from the ghost pad + // itself, so changing the chain function is still safe. + unsafe { + proxypad.set_chain_function(move |pad, _parent, buffer| { + let element = match element_weak.upgrade() { + None => return Err(gst::FlowError::Flushing), + Some(element) => element, + }; - let src = FallbackSrc::from_instance(&element); - src.proxy_pad_chain(&element, pad, buffer) - }); + let src = FallbackSrc::from_instance(&element); + src.proxy_pad_chain(&element, pad, buffer) + }); + } + + element.add_pad(&ghostpad).unwrap(); Ok(Stream { fallback_input, @@ -2215,7 +2228,9 @@ mod custom_source { (element.get_pad_template("video_%u").unwrap(), name) }; - let ghost_pad = gst::GhostPad::from_template(Some(&name), pad, &templ).unwrap(); + let ghost_pad = gst::GhostPad::builder_with_template(&templ, Some(&name)) + .build_with_target(pad) + .unwrap(); let stream = Stream { source_pad: pad.clone(), diff --git a/utils/togglerecord/src/togglerecord.rs b/utils/togglerecord/src/togglerecord.rs index 201fce387..146aa3a0b 100644 --- a/utils/togglerecord/src/togglerecord.rs +++ b/utils/togglerecord/src/togglerecord.rs @@ -362,59 +362,6 @@ lazy_static! { } impl ToggleRecord { - fn set_pad_functions(sinkpad: &gst::Pad, srcpad: &gst::Pad) { - sinkpad.set_chain_function(|pad, parent, buffer| { - ToggleRecord::catch_panic_pad_function( - parent, - || Err(gst::FlowError::Error), - |togglerecord, element| togglerecord.sink_chain(pad, element, buffer), - ) - }); - sinkpad.set_event_function(|pad, parent, event| { - ToggleRecord::catch_panic_pad_function( - parent, - || false, - |togglerecord, element| togglerecord.sink_event(pad, element, event), - ) - }); - sinkpad.set_query_function(|pad, parent, query| { - ToggleRecord::catch_panic_pad_function( - parent, - || false, - |togglerecord, element| togglerecord.sink_query(pad, element, query), - ) - }); - sinkpad.set_iterate_internal_links_function(|pad, parent| { - ToggleRecord::catch_panic_pad_function( - parent, - || gst::Iterator::from_vec(vec![]), - |togglerecord, element| togglerecord.iterate_internal_links(pad, element), - ) - }); - - srcpad.set_event_function(|pad, parent, event| { - ToggleRecord::catch_panic_pad_function( - parent, - || false, - |togglerecord, element| togglerecord.src_event(pad, element, event), - ) - }); - srcpad.set_query_function(|pad, parent, query| { - ToggleRecord::catch_panic_pad_function( - parent, - || false, - |togglerecord, element| togglerecord.src_query(pad, element, query), - ) - }); - srcpad.set_iterate_internal_links_function(|pad, parent| { - ToggleRecord::catch_panic_pad_function( - parent, - || gst::Iterator::from_vec(vec![]), - |togglerecord, element| togglerecord.iterate_internal_links(pad, element), - ) - }); - } - fn handle_main_stream<T: HandleData>( &self, element: &gst::Element, @@ -1537,11 +1484,61 @@ impl ObjectSubclass for ToggleRecord { fn with_class(klass: &subclass::simple::ClassStruct<Self>) -> Self { let templ = klass.get_pad_template("sink").unwrap(); - let sinkpad = gst::Pad::from_template(&templ, Some("sink")); - let templ = klass.get_pad_template("src").unwrap(); - let srcpad = gst::Pad::from_template(&templ, Some("src")); + let sinkpad = gst::Pad::builder_with_template(&templ, Some("sink")) + .chain_function(|pad, parent, buffer| { + ToggleRecord::catch_panic_pad_function( + parent, + || Err(gst::FlowError::Error), + |togglerecord, element| togglerecord.sink_chain(pad, element, buffer), + ) + }) + .event_function(|pad, parent, event| { + ToggleRecord::catch_panic_pad_function( + parent, + || false, + |togglerecord, element| togglerecord.sink_event(pad, element, event), + ) + }) + .query_function(|pad, parent, query| { + ToggleRecord::catch_panic_pad_function( + parent, + || false, + |togglerecord, element| togglerecord.sink_query(pad, element, query), + ) + }) + .iterate_internal_links_function(|pad, parent| { + ToggleRecord::catch_panic_pad_function( + parent, + || gst::Iterator::from_vec(vec![]), + |togglerecord, element| togglerecord.iterate_internal_links(pad, element), + ) + }) + .build(); - ToggleRecord::set_pad_functions(&sinkpad, &srcpad); + let templ = klass.get_pad_template("src").unwrap(); + let srcpad = gst::Pad::builder_with_template(&templ, Some("src")) + .event_function(|pad, parent, event| { + ToggleRecord::catch_panic_pad_function( + parent, + || false, + |togglerecord, element| togglerecord.src_event(pad, element, event), + ) + }) + .query_function(|pad, parent, query| { + ToggleRecord::catch_panic_pad_function( + parent, + || false, + |togglerecord, element| togglerecord.src_query(pad, element, query), + ) + }) + .iterate_internal_links_function(|pad, parent| { + ToggleRecord::catch_panic_pad_function( + parent, + || gst::Iterator::from_vec(vec![]), + |togglerecord, element| togglerecord.iterate_internal_links(pad, element), + ) + }) + .build(); let main_stream = Stream::new(sinkpad, srcpad); @@ -1734,12 +1731,62 @@ impl ElementImpl for ToggleRecord { *pad_count += 1; let templ = element.get_pad_template("sink_%u").unwrap(); - let sinkpad = gst::Pad::from_template(&templ, Some(format!("sink_{}", id).as_str())); + let sinkpad = + gst::Pad::builder_with_template(&templ, Some(format!("sink_{}", id).as_str())) + .chain_function(|pad, parent, buffer| { + ToggleRecord::catch_panic_pad_function( + parent, + || Err(gst::FlowError::Error), + |togglerecord, element| togglerecord.sink_chain(pad, element, buffer), + ) + }) + .event_function(|pad, parent, event| { + ToggleRecord::catch_panic_pad_function( + parent, + || false, + |togglerecord, element| togglerecord.sink_event(pad, element, event), + ) + }) + .query_function(|pad, parent, query| { + ToggleRecord::catch_panic_pad_function( + parent, + || false, + |togglerecord, element| togglerecord.sink_query(pad, element, query), + ) + }) + .iterate_internal_links_function(|pad, parent| { + ToggleRecord::catch_panic_pad_function( + parent, + || gst::Iterator::from_vec(vec![]), + |togglerecord, element| togglerecord.iterate_internal_links(pad, element), + ) + }) + .build(); let templ = element.get_pad_template("src_%u").unwrap(); - let srcpad = gst::Pad::from_template(&templ, Some(format!("src_{}", id).as_str())); - - ToggleRecord::set_pad_functions(&sinkpad, &srcpad); + let srcpad = gst::Pad::builder_with_template(&templ, Some(format!("src_{}", id).as_str())) + .event_function(|pad, parent, event| { + ToggleRecord::catch_panic_pad_function( + parent, + || false, + |togglerecord, element| togglerecord.src_event(pad, element, event), + ) + }) + .query_function(|pad, parent, query| { + ToggleRecord::catch_panic_pad_function( + parent, + || false, + |togglerecord, element| togglerecord.src_query(pad, element, query), + ) + }) + .iterate_internal_links_function(|pad, parent| { + ToggleRecord::catch_panic_pad_function( + parent, + || gst::Iterator::from_vec(vec![]), + |togglerecord, element| togglerecord.iterate_internal_links(pad, element), + ) + }) + .build(); sinkpad.set_active(true).unwrap(); srcpad.set_active(true).unwrap(); |