Update to new clone macro syntax

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1463>
This commit is contained in:
Sebastian Dröge 2024-06-17 17:25:42 +03:00
parent 0be8b364f8
commit 5ea912d702
5 changed files with 172 additions and 149 deletions

26
Cargo.lock generated
View file

@ -169,7 +169,7 @@ checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9"
[[package]] [[package]]
name = "cairo-rs" name = "cairo-rs"
version = "0.20.0" version = "0.20.0"
source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#d88a7c473e2af36a4718a648406b7894d288ec1b" source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#dc97710becdf432e5a97e0b7c22d4d1daa8cfec6"
dependencies = [ dependencies = [
"bitflags 2.5.0", "bitflags 2.5.0",
"cairo-sys-rs", "cairo-sys-rs",
@ -181,7 +181,7 @@ dependencies = [
[[package]] [[package]]
name = "cairo-sys-rs" name = "cairo-sys-rs"
version = "0.20.0" version = "0.20.0"
source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#d88a7c473e2af36a4718a648406b7894d288ec1b" source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#dc97710becdf432e5a97e0b7c22d4d1daa8cfec6"
dependencies = [ dependencies = [
"glib-sys", "glib-sys",
"libc", "libc",
@ -678,7 +678,7 @@ dependencies = [
[[package]] [[package]]
name = "gio" name = "gio"
version = "0.20.0" version = "0.20.0"
source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#d88a7c473e2af36a4718a648406b7894d288ec1b" source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#dc97710becdf432e5a97e0b7c22d4d1daa8cfec6"
dependencies = [ dependencies = [
"futures-channel", "futures-channel",
"futures-core", "futures-core",
@ -695,7 +695,7 @@ dependencies = [
[[package]] [[package]]
name = "gio-sys" name = "gio-sys"
version = "0.20.0" version = "0.20.0"
source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#d88a7c473e2af36a4718a648406b7894d288ec1b" source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#dc97710becdf432e5a97e0b7c22d4d1daa8cfec6"
dependencies = [ dependencies = [
"glib-sys", "glib-sys",
"gobject-sys", "gobject-sys",
@ -724,7 +724,7 @@ dependencies = [
[[package]] [[package]]
name = "glib" name = "glib"
version = "0.20.0" version = "0.20.0"
source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#d88a7c473e2af36a4718a648406b7894d288ec1b" source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#dc97710becdf432e5a97e0b7c22d4d1daa8cfec6"
dependencies = [ dependencies = [
"bitflags 2.5.0", "bitflags 2.5.0",
"futures-channel", "futures-channel",
@ -745,7 +745,7 @@ dependencies = [
[[package]] [[package]]
name = "glib-macros" name = "glib-macros"
version = "0.20.0" version = "0.20.0"
source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#d88a7c473e2af36a4718a648406b7894d288ec1b" source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#dc97710becdf432e5a97e0b7c22d4d1daa8cfec6"
dependencies = [ dependencies = [
"heck", "heck",
"proc-macro-crate", "proc-macro-crate",
@ -757,7 +757,7 @@ dependencies = [
[[package]] [[package]]
name = "glib-sys" name = "glib-sys"
version = "0.20.0" version = "0.20.0"
source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#d88a7c473e2af36a4718a648406b7894d288ec1b" source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#dc97710becdf432e5a97e0b7c22d4d1daa8cfec6"
dependencies = [ dependencies = [
"libc", "libc",
"system-deps", "system-deps",
@ -820,7 +820,7 @@ dependencies = [
[[package]] [[package]]
name = "gobject-sys" name = "gobject-sys"
version = "0.20.0" version = "0.20.0"
source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#d88a7c473e2af36a4718a648406b7894d288ec1b" source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#dc97710becdf432e5a97e0b7c22d4d1daa8cfec6"
dependencies = [ dependencies = [
"glib-sys", "glib-sys",
"libc", "libc",
@ -1679,7 +1679,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19" checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"windows-targets 0.52.5", "windows-targets 0.48.5",
] ]
[[package]] [[package]]
@ -1898,7 +1898,7 @@ dependencies = [
[[package]] [[package]]
name = "pango" name = "pango"
version = "0.20.0" version = "0.20.0"
source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#d88a7c473e2af36a4718a648406b7894d288ec1b" source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#dc97710becdf432e5a97e0b7c22d4d1daa8cfec6"
dependencies = [ dependencies = [
"gio", "gio",
"glib", "glib",
@ -1909,7 +1909,7 @@ dependencies = [
[[package]] [[package]]
name = "pango-sys" name = "pango-sys"
version = "0.20.0" version = "0.20.0"
source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#d88a7c473e2af36a4718a648406b7894d288ec1b" source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#dc97710becdf432e5a97e0b7c22d4d1daa8cfec6"
dependencies = [ dependencies = [
"glib-sys", "glib-sys",
"gobject-sys", "gobject-sys",
@ -1920,7 +1920,7 @@ dependencies = [
[[package]] [[package]]
name = "pangocairo" name = "pangocairo"
version = "0.20.0" version = "0.20.0"
source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#d88a7c473e2af36a4718a648406b7894d288ec1b" source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#dc97710becdf432e5a97e0b7c22d4d1daa8cfec6"
dependencies = [ dependencies = [
"cairo-rs", "cairo-rs",
"glib", "glib",
@ -1932,7 +1932,7 @@ dependencies = [
[[package]] [[package]]
name = "pangocairo-sys" name = "pangocairo-sys"
version = "0.20.0" version = "0.20.0"
source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#d88a7c473e2af36a4718a648406b7894d288ec1b" source = "git+https://github.com/gtk-rs/gtk-rs-core?branch=master#dc97710becdf432e5a97e0b7c22d4d1daa8cfec6"
dependencies = [ dependencies = [
"cairo-sys-rs", "cairo-sys-rs",
"glib-sys", "glib-sys",

View file

@ -132,116 +132,125 @@ fn create_pipeline() -> Result<gst::Pipeline, Error> {
overlay.connect_closure( overlay.connect_closure(
"draw", "draw",
false, false,
glib::closure!(@strong drawer => move |_overlay: &gst::Element, glib::closure!(
sample: &gst::Sample| { #[strong]
use std::f64::consts::PI; drawer,
move |_overlay: &gst::Element, sample: &gst::Sample| {
use std::f64::consts::PI;
let drawer = drawer.lock().unwrap(); let drawer = drawer.lock().unwrap();
let buffer = sample.buffer().unwrap(); let buffer = sample.buffer().unwrap();
let timestamp = buffer.pts().unwrap(); let timestamp = buffer.pts().unwrap();
let info = drawer.info.as_ref().unwrap(); let info = drawer.info.as_ref().unwrap();
let text_layout = &drawer.text_layout; let text_layout = &drawer.text_layout;
let bitmap = drawer.bitmap.as_ref().unwrap(); let bitmap = drawer.bitmap.as_ref().unwrap();
let render_target = drawer.render_target.as_ref().unwrap(); let render_target = drawer.render_target.as_ref().unwrap();
let global_angle = 360. * (timestamp % (10 * gst::ClockTime::SECOND)).nseconds() as f64 let global_angle = 360.
/ (10.0 * gst::ClockTime::SECOND.nseconds() as f64); * (timestamp % (10 * gst::ClockTime::SECOND)).nseconds() as f64
let center_x = (info.width() / 2) as f32; / (10.0 * gst::ClockTime::SECOND.nseconds() as f64);
let center_y = (info.height() / 2) as f32; let center_x = (info.width() / 2) as f32;
let top_margin = (info.height() / 20) as f32; let center_y = (info.height() / 2) as f32;
let top_margin = (info.height() / 20) as f32;
unsafe {
// Begin drawing
render_target.BeginDraw();
// Clear background
render_target.Clear(Some(&D2D1_COLOR_F {
r: 0f32,
g: 0f32,
b: 0f32,
a: 0f32,
}));
// This loop will render 10 times the string "GStreamer" in a circle
for i in 0..10 {
let angle = (360. * f64::from(i)) / 10.0;
let red = ((1.0 + f64::cos((angle - 60.0) * PI / 180.0)) / 2.0) as f32;
let text_brush = render_target
.CreateSolidColorBrush(
&D2D1_COLOR_F {
r: red,
g: 0f32,
b: 1f32 - red,
a: 1f32,
},
None,
)
.unwrap();
let angle = (angle + global_angle) as f32;
let matrix = Matrix3x2::rotation(angle, center_x, center_y);
render_target.SetTransform(&matrix);
render_target.DrawTextLayout(
D2D_POINT_2F { x: 0f32, y: top_margin },
text_layout,
&text_brush,
D2D1_DRAW_TEXT_OPTIONS_NONE,
);
}
// EndDraw may not be successful for some reasons.
// Ignores any error in this example
let _ = render_target.EndDraw(None, None);
// Make sure all operations is completed before copying
// bitmap to buffer
let _ = render_target.Flush(None::<*mut u64>, None::<*mut u64>);
}
let mut buffer = gst::Buffer::with_size((info.width() * info.height() * 4) as usize).unwrap();
{
let buffer_mut = buffer.get_mut().unwrap();
let mut map = buffer_mut.map_writable().unwrap();
let dst = map.as_mut_slice_of::<u8>().unwrap();
unsafe { unsafe {
// Bitmap size is equal to the background image size. // Begin drawing
// Copy entire memory render_target.BeginDraw();
bitmap.CopyPixels(std::ptr::null(), info.width() * 4, dst).unwrap();
// Clear background
render_target.Clear(Some(&D2D1_COLOR_F {
r: 0f32,
g: 0f32,
b: 0f32,
a: 0f32,
}));
// This loop will render 10 times the string "GStreamer" in a circle
for i in 0..10 {
let angle = (360. * f64::from(i)) / 10.0;
let red = ((1.0 + f64::cos((angle - 60.0) * PI / 180.0)) / 2.0) as f32;
let text_brush = render_target
.CreateSolidColorBrush(
&D2D1_COLOR_F {
r: red,
g: 0f32,
b: 1f32 - red,
a: 1f32,
},
None,
)
.unwrap();
let angle = (angle + global_angle) as f32;
let matrix = Matrix3x2::rotation(angle, center_x, center_y);
render_target.SetTransform(&matrix);
render_target.DrawTextLayout(
D2D_POINT_2F {
x: 0f32,
y: top_margin,
},
text_layout,
&text_brush,
D2D1_DRAW_TEXT_OPTIONS_NONE,
);
}
// EndDraw may not be successful for some reasons.
// Ignores any error in this example
let _ = render_target.EndDraw(None, None);
// Make sure all operations is completed before copying
// bitmap to buffer
let _ = render_target.Flush(None::<*mut u64>, None::<*mut u64>);
} }
let mut buffer =
gst::Buffer::with_size((info.width() * info.height() * 4) as usize).unwrap();
{
let buffer_mut = buffer.get_mut().unwrap();
let mut map = buffer_mut.map_writable().unwrap();
let dst = map.as_mut_slice_of::<u8>().unwrap();
unsafe {
// Bitmap size is equal to the background image size.
// Copy entire memory
bitmap
.CopyPixels(std::ptr::null(), info.width() * 4, dst)
.unwrap();
}
}
gst_video::VideoMeta::add_full(
buffer.get_mut().unwrap(),
gst_video::VideoFrameFlags::empty(),
gst_video::VideoFormat::Bgra,
info.width(),
info.height(),
&[0],
&[(info.width() * 4) as i32],
)
.unwrap();
// Turn the buffer into a VideoOverlayRectangle, then place
// that into a VideoOverlayComposition and return it.
//
// A VideoOverlayComposition can take a Vec of such rectangles
// spaced around the video frame, but we're just outputting 1
// here
let rect = gst_video::VideoOverlayRectangle::new_raw(
&buffer,
0,
0,
info.width(),
info.height(),
gst_video::VideoOverlayFormatFlags::PREMULTIPLIED_ALPHA,
);
gst_video::VideoOverlayComposition::new(Some(&rect)).unwrap()
} }
),
gst_video::VideoMeta::add_full(
buffer.get_mut().unwrap(),
gst_video::VideoFrameFlags::empty(),
gst_video::VideoFormat::Bgra,
info.width(),
info.height(),
&[0],
&[(info.width() * 4) as i32],
)
.unwrap();
// Turn the buffer into a VideoOverlayRectangle, then place
// that into a VideoOverlayComposition and return it.
//
// A VideoOverlayComposition can take a Vec of such rectangles
// spaced around the video frame, but we're just outputting 1
// here
let rect = gst_video::VideoOverlayRectangle::new_raw(
&buffer,
0,
0,
info.width(),
info.height(),
gst_video::VideoOverlayFormatFlags::PREMULTIPLIED_ALPHA,
);
gst_video::VideoOverlayComposition::new(Some(&rect))
.unwrap()
}),
); );
// Add a signal handler to the overlay's "caps-changed" signal. This could e.g. // Add a signal handler to the overlay's "caps-changed" signal. This could e.g.

View file

@ -119,8 +119,9 @@ fn create_pipeline() -> Result<gst::Pipeline, Error> {
overlay.connect_closure( overlay.connect_closure(
"draw", "draw",
false, false,
glib::closure!(@strong drawer => move |_overlay: &gst::Element, glib::closure!(
sample: &gst::Sample| { #[strong] drawer,
move |_overlay: &gst::Element, sample: &gst::Sample| {
use std::f64::consts::PI; use std::f64::consts::PI;
let drawer = drawer.lock().unwrap(); let drawer = drawer.lock().unwrap();

View file

@ -195,20 +195,25 @@ impl StreamProducer {
let fku_probe_id = srcpad let fku_probe_id = srcpad
.add_probe( .add_probe(
gst::PadProbeType::EVENT_UPSTREAM, gst::PadProbeType::EVENT_UPSTREAM,
glib::clone!(@weak appsink, @weak consumer => @default-panic, move |_pad, info| { glib::clone!(
let Some(event) = info.event() else { #[weak]
return gst::PadProbeReturn::Ok; appsink,
}; #[upgrade_or_panic]
move |_pad, info| {
let Some(event) = info.event() else {
return gst::PadProbeReturn::Ok;
};
if gst_video::UpstreamForceKeyUnitEvent::parse(event).is_ok() { if gst_video::UpstreamForceKeyUnitEvent::parse(event).is_ok() {
gst::debug!(CAT, obj: &appsink, "Requesting keyframe"); gst::debug!(CAT, obj: &appsink, "Requesting keyframe");
// Do not use `gst_element_send_event()` as it takes the state lock which may lead to dead locks. // Do not use `gst_element_send_event()` as it takes the state lock which may lead to dead locks.
let pad = appsink.static_pad("sink").unwrap(); let pad = appsink.static_pad("sink").unwrap();
let _ = pad.push_event(event.clone()); let _ = pad.push_event(event.clone());
}
gst::PadProbeReturn::Ok
} }
),
gst::PadProbeReturn::Ok
}),
) )
.unwrap(); .unwrap();
@ -410,7 +415,7 @@ impl<'a> From<&'a gst_app::AppSink> for StreamProducer {
appsink.set_callbacks( appsink.set_callbacks(
gst_app::AppSinkCallbacks::builder() gst_app::AppSinkCallbacks::builder()
.new_sample(glib::clone!(@strong consumers => move |appsink| { .new_sample(glib::clone!(#[strong] consumers, move |appsink| {
let mut consumers = consumers.lock().unwrap(); let mut consumers = consumers.lock().unwrap();
let sample = match appsink.pull_sample() { let sample = match appsink.pull_sample() {
@ -429,7 +434,7 @@ impl<'a> From<&'a gst_app::AppSink> for StreamProducer {
StreamProducer::process_sample(sample, appsink, consumers) StreamProducer::process_sample(sample, appsink, consumers)
})) }))
.new_preroll(glib::clone!(@strong consumers => move |appsink| { .new_preroll(glib::clone!(#[strong] consumers, move |appsink| {
let mut consumers = consumers.lock().unwrap(); let mut consumers = consumers.lock().unwrap();
let sample = match appsink.pull_preroll() { let sample = match appsink.pull_preroll() {
@ -448,7 +453,7 @@ impl<'a> From<&'a gst_app::AppSink> for StreamProducer {
Ok(gst::FlowSuccess::Ok) Ok(gst::FlowSuccess::Ok)
} }
})) }))
.new_event(glib::clone!(@strong consumers => move |appsink| { .new_event(glib::clone!(#[strong] consumers, move |appsink| {
match appsink.pull_object().map(|obj| obj.downcast::<gst::Event>()) { match appsink.pull_object().map(|obj| obj.downcast::<gst::Event>()) {
Ok(Ok(event)) => { Ok(Ok(event)) => {
let (events_to_forward, appsrcs) = { let (events_to_forward, appsrcs) = {
@ -472,7 +477,7 @@ impl<'a> From<&'a gst_app::AppSink> for StreamProducer {
false false
})) }))
.eos(glib::clone!(@strong consumers => move |appsink| { .eos(glib::clone!(#[strong] consumers, move |appsink| {
let stream_consumers = consumers let stream_consumers = consumers
.lock() .lock()
.unwrap(); .unwrap();
@ -499,21 +504,25 @@ impl<'a> From<&'a gst_app::AppSink> for StreamProducer {
let sinkpad = appsink.static_pad("sink").unwrap(); let sinkpad = appsink.static_pad("sink").unwrap();
sinkpad.add_probe( sinkpad.add_probe(
gst::PadProbeType::EVENT_UPSTREAM, gst::PadProbeType::EVENT_UPSTREAM,
glib::clone!(@strong consumers => move |_pad, info| { glib::clone!(
let Some(event) = info.event() else { #[strong]
return gst::PadProbeReturn::Ok; consumers,
}; move |_pad, info| {
let Some(event) = info.event() else {
return gst::PadProbeReturn::Ok;
};
let gst::EventView::Latency(event) = event.view() else { let gst::EventView::Latency(event) = event.view() else {
return gst::PadProbeReturn::Ok; return gst::PadProbeReturn::Ok;
}; };
let latency = event.latency(); let latency = event.latency();
let mut consumers = consumers.lock().unwrap(); let mut consumers = consumers.lock().unwrap();
consumers.current_latency = Some(latency); consumers.current_latency = Some(latency);
gst::PadProbeReturn::Ok gst::PadProbeReturn::Ok
}), }
),
); );
StreamProducer { StreamProducer {

View file

@ -387,11 +387,15 @@ mod tests {
let called = Arc::new(Mutex::new(false)); let called = Arc::new(Mutex::new(false));
let succeeding_action_type = crate::ActionTypeBuilder::new( let succeeding_action_type = crate::ActionTypeBuilder::new(
"succeeds", "succeeds",
glib::clone!(@strong called => move |_, _action| { glib::clone!(
*called.lock().unwrap() = true; #[strong]
called,
move |_, _action| {
*called.lock().unwrap() = true;
Ok(crate::ActionSuccess::Ok) Ok(crate::ActionSuccess::Ok)
}), }
),
) )
.parameter( .parameter(
crate::ActionParameterBuilder::new("always", "Does the action always succeeds") crate::ActionParameterBuilder::new("always", "Does the action always succeeds")