Slightly improve code making use of element factories retrieved from an element

We can use `is_some_and(...)` instead of `map_or(false, ...)`.

Also in a few places the factory was retrieved multiple times, one time
with unwrapping and another time with handling the `None` case
correctly. Instead of unwrapping, move code to handle the `None` case.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/merge_requests/1630>
This commit is contained in:
Sebastian Dröge 2024-06-19 10:08:23 +03:00 committed by GStreamer Marge Bot
parent 8f96509f03
commit 23d998a1db
4 changed files with 27 additions and 27 deletions

View file

@ -740,7 +740,11 @@ impl ObjectImpl for S3HlsSink {
// The signature on delete-fragment signal is different for // The signature on delete-fragment signal is different for
// hlssink2 and hlssink3. // hlssink2 and hlssink3.
if self_.hlssink.factory().unwrap().name().contains("hlssink3") { if self_
.hlssink
.factory()
.is_some_and(|factory| factory.name() == "hlssink3")
{
if res.is_ok() { if res.is_ok() {
Some(true.to_value()) Some(true.to_value())
} else { } else {

View file

@ -40,12 +40,13 @@ fn main() -> Result<(), Error> {
pipeline.connect("deep-element-added", false, |values| { pipeline.connect("deep-element-added", false, |values| {
let element = values[2].get::<gst::Element>().unwrap(); let element = values[2].get::<gst::Element>().unwrap();
if let Some(factory) = element.factory() { if element
if factory.name().as_str() == "videoscale" { .factory()
.is_some_and(|factory| factory.name() == "videoscale")
{
println!("Tuning videoscale"); println!("Tuning videoscale");
element.set_property_from_str("method", "lanczos"); element.set_property_from_str("method", "lanczos");
} }
}
None None
}); });
@ -61,9 +62,9 @@ fn main() -> Result<(), Error> {
webrtcsink.connect("encoder-setup", true, |values| { webrtcsink.connect("encoder-setup", true, |values| {
let encoder = values[3].get::<gst::Element>().unwrap(); let encoder = values[3].get::<gst::Element>().unwrap();
println!("Encoder: {}", encoder.factory().unwrap().name());
if let Some(factory) = encoder.factory() { if let Some(factory) = encoder.factory() {
println!("Encoder: {}", factory.name());
match factory.name().as_str() { match factory.name().as_str() {
"x264enc" => { "x264enc" => {
println!("Applying extra configuration to x264enc"); println!("Applying extra configuration to x264enc");

View file

@ -137,9 +137,8 @@ async fn run(args: Args) -> Result<(), Error> {
ws.connect("encoder-setup", false, |values| { ws.connect("encoder-setup", false, |values| {
let encoder = values[3].get::<gst::Element>().unwrap(); let encoder = values[3].get::<gst::Element>().unwrap();
info!("Encoder: {}", encoder.factory().unwrap().name());
let configured = if let Some(factory) = encoder.factory() { let configured = if let Some(factory) = encoder.factory() {
info!("Encoder: {}", factory.name());
matches!(factory.name().as_str(), "does-not-exist") matches!(factory.name().as_str(), "does-not-exist")
} else { } else {
false false

View file

@ -559,7 +559,7 @@ fn make_converter_for_video_caps(caps: &gst::Caps, codec: &Codec) -> Result<gst:
// corresponding converter // corresponding converter
|| codec || codec
.encoder_factory() .encoder_factory()
.map_or(false, |factory| factory.name().starts_with("nvv4l2")) .is_some_and(|factory| factory.name().starts_with("nvv4l2"))
{ {
let queue = make_element("queue", None)?; let queue = make_element("queue", None)?;
let nvconvert = if let Ok(nvconvert) = make_element("nvvideoconvert", None) { let nvconvert = if let Ok(nvconvert) = make_element("nvvideoconvert", None) {
@ -788,7 +788,8 @@ fn configure_encoder(enc: &gst::Element, start_bitrate: u32) {
fn configure_payloader(pay: &gst::Element) { fn configure_payloader(pay: &gst::Element) {
pay.set_property("mtu", 1200_u32); pay.set_property("mtu", 1200_u32);
match pay.factory().unwrap().name().as_str() { if let Some(factory) = pay.factory() {
match factory.name().as_str() {
"rtpvp8pay" | "rtpvp9pay" => { "rtpvp8pay" | "rtpvp9pay" => {
pay.set_property_from_str("picture-id-mode", "15-bit"); pay.set_property_from_str("picture-id-mode", "15-bit");
} }
@ -799,6 +800,7 @@ fn configure_payloader(pay: &gst::Element) {
_ => (), _ => (),
} }
} }
}
fn setup_signal_accumulator( fn setup_signal_accumulator(
_hint: &glib::subclass::SignalInvocationHint, _hint: &glib::subclass::SignalInvocationHint,
@ -950,13 +952,7 @@ impl VideoEncoder {
) -> Option<Self> { ) -> Option<Self> {
let halved_framerate = video_info.fps().mul(gst::Fraction::new(1, 2)); let halved_framerate = video_info.fps().mul(gst::Fraction::new(1, 2));
Some(Self { Some(Self {
factory_name: encoding_elements factory_name: encoding_elements.encoder.as_ref()?.factory()?.name().into(),
.encoder
.as_ref()?
.factory()
.unwrap()
.name()
.into(),
codec_name: codec_name.to_string(), codec_name: codec_name.to_string(),
element: encoding_elements.encoder.as_ref()?.clone(), element: encoding_elements.encoder.as_ref()?.clone(),
filter: encoding_elements.raw_filter.as_ref()?.clone(), filter: encoding_elements.raw_filter.as_ref()?.clone(),
@ -2634,7 +2630,7 @@ impl BaseWebRTCSink {
glib::closure!(@watch element, @strong session_id glib::closure!(@watch element, @strong session_id
=> move |_webrtcbin: gst::Element, _bin: gst::Bin, e: gst::Element| { => move |_webrtcbin: gst::Element, _bin: gst::Bin, e: gst::Element| {
if e.factory().map_or(false, |f| f.name() == "rtprtxsend") { if e.factory().is_some_and(|f| f.name() == "rtprtxsend") {
if e.has_property("stuffing-kbps", Some(i32::static_type())) { if e.has_property("stuffing-kbps", Some(i32::static_type())) {
element.imp().set_rtptrxsend(element, &session_id, e); element.imp().set_rtptrxsend(element, &session_id, e);
} else { } else {