impl CameraCapture {
    #[allow(dead_code)] // helper kept for future heuristics
    #[cfg(not(coverage))]
    fn pick_encoder() -> (&'static str, &'static str) {
        let encoders = &[
            ("nvh264enc", "video/x-raw(memory:NVMM),format=NV12"),
            ("vaapih264enc", "video/x-raw,format=NV12"),
            ("v4l2h264enc", "video/x-raw"), // RPi, Jetson, etc.
            ("x264enc", "video/x-raw"),     // software
        ];
        for (name, caps) in encoders {
            if gst::ElementFactory::find(name).is_some() {
                return (name, caps);
            }
        }
        // last resort – software
        ("x264enc", "video/x-raw")
    }

    #[cfg(coverage)]
    fn pick_encoder() -> (&'static str, &'static str) {
        ("x264enc", "video/x-raw")
    }

    #[cfg(not(coverage))]
    fn choose_encoder() -> (&'static str, Option<&'static str>) {
        if buildable_encoder("nvh264enc") {
            return (
                "nvh264enc",
                supported_encoder_property(
                    "nvh264enc",
                    &["iframeinterval", "idrinterval", "gop-size"],
                ),
            );
        }
        if buildable_encoder("vaapih264enc") {
            return (
                "vaapih264enc",
                supported_encoder_property("vaapih264enc", &["keyframe-period"]),
            );
        }
        if buildable_encoder("v4l2h264enc") {
            return (
                "v4l2h264enc",
                supported_encoder_property("v4l2h264enc", &["idrcount"]),
            );
        }
        ("x264enc", Some("key-int-max"))
    }

    #[cfg(coverage)]
    fn choose_encoder() -> (&'static str, Option<&'static str>) {
        match std::env::var("LESAVKA_CAM_TEST_ENCODER")
            .ok()
            .as_deref()
            .map(str::trim)
        {
            Some("nvh264enc") => ("nvh264enc", None),
            Some("vaapih264enc") => ("vaapih264enc", Some("keyframe-period")),
            Some("v4l2h264enc") => ("v4l2h264enc", Some("idrcount")),
            _ => ("x264enc", Some("key-int-max")),
        }
    }

    fn encoder_options(
        enc: &'static str,
        kf_prop: Option<&'static str>,
        keyframe_interval: u32,
    ) -> String {
        if enc == "x264enc" {
            let bitrate_kbit = env_u32("LESAVKA_CAM_H264_KBIT", 4500);
            let keyframe_opt = kf_prop
                .map(|property| format!(" {property}={keyframe_interval}"))
                .unwrap_or_default();
            format!(
                "{enc} tune=zerolatency speed-preset=faster bitrate={bitrate_kbit}{keyframe_opt}"
            )
        } else if let Some(property) = kf_prop {
            format!("{enc} {property}={keyframe_interval}")
        } else {
            enc.to_string()
        }
    }

}
