Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

39.6 KB
Raw Blame
#[cfg(target_os = "macos")]
use crate::SendableShareableContent;
use cap_cursor_capture::CursorCropBounds;
use cap_media_info::{AudioInfo, VideoInfo, ensure_even};
use scap_targets::{Display, DisplayId, Window, WindowId, bounds::*};
use serde::{Deserialize, Serialize};
use specta::Type;
use std::time::SystemTime;
use tracing::*;

pub mod cadence;

#[cfg(target_os = "windows")]
mod windows;
#[cfg(target_os = "windows")]
pub use windows::*;

#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "macos")]
pub use macos::*;

#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "linux")]
mod linux_dmabuf_import;
#[cfg(target_os = "linux")]
mod linux_shm;
#[cfg(target_os = "linux")]
pub use linux::*;

pub struct StopCapturing;

#[derive(Debug, Clone, thiserror::Error)]
pub enum StopCapturingError {
    #[error("NotCapturing")]
    NotCapturing,
}

#[derive(Debug, Clone, Serialize, Deserialize, Type)]
pub struct CaptureWindow {
    pub id: WindowId,
    pub owner_name: String,
    pub name: String,
    pub bounds: LogicalBounds,
    pub refresh_rate: u32,
    pub bundle_identifier: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Type)]
pub struct CaptureDisplay {
    pub id: DisplayId,
    pub name: String,
    pub refresh_rate: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize, Type)]
pub struct CaptureArea {
    pub screen: CaptureDisplay,
    pub bounds: LogicalBounds,
}

#[derive(Debug, Clone, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase", tag = "variant")]
pub enum ScreenCaptureTarget {
    Window {
        id: WindowId,
    },
    Display {
        id: DisplayId,
    },
    Area {
        screen: DisplayId,
        bounds: LogicalBounds,
    },
    CameraOnly,
}

#[cfg(target_os = "linux")]
#[derive(Clone, Debug)]
pub enum LinuxCaptureSource {
    Display,
    Window { id: WindowId },
    Area,
}

#[cfg(target_os = "linux")]
impl LinuxCaptureSource {
    pub fn from_target(target: &ScreenCaptureTarget) -> Self {
        match target {
            ScreenCaptureTarget::Window { id } => Self::Window { id: id.clone() },
            ScreenCaptureTarget::Area { .. } => Self::Area,
            ScreenCaptureTarget::Display { .. } | ScreenCaptureTarget::CameraOnly => Self::Display,
        }
    }
}

impl ScreenCaptureTarget {
    pub fn display(&self) -> Option<Display> {
        match self {
            Self::Display { id } => Display::from_id(id),
            Self::Window { id } => Window::from_id(id).and_then(|w| w.display()),
            Self::Area { screen, .. } => Display::from_id(screen),
            Self::CameraOnly => None,
        }
    }

    pub fn window(&self) -> Option<WindowId> {
        match self {
            Self::Window { id } => Some(id.clone()),
            _ => None,
        }
    }

    pub fn cursor_crop(&self) -> Option<CursorCropBounds> {
        match self {
            Self::Display { .. } => {
                #[cfg(target_os = "macos")]
                #[allow(clippy::needless_return)]
                {
                    let display = self.display()?;
                    return Some(CursorCropBounds::new_macos(LogicalBounds::new(
                        LogicalPosition::new(0.0, 0.0),
                        display.raw_handle().logical_size()?,
                    )));
                }

                #[cfg(windows)]
                #[allow(clippy::needless_return)]
                {
                    let display = self.display()?;
                    return Some(CursorCropBounds::new_windows(PhysicalBounds::new(
                        PhysicalPosition::new(0.0, 0.0),
                        display.raw_handle().physical_size()?,
                    )));
                }

                #[cfg(target_os = "linux")]
                #[allow(clippy::needless_return)]
                {
                    let display = self.display()?;
                    return Some(CursorCropBounds::new_linux(PhysicalBounds::new(
                        PhysicalPosition::new(0.0, 0.0),
                        display.raw_handle().physical_size()?,
                    )));
                }
            }
            Self::Window { id } => {
                let window = Window::from_id(id)?;

                #[cfg(target_os = "macos")]
                #[allow(clippy::needless_return)]
                {
                    let display = self.display()?;
                    let display_position = display.raw_handle().logical_position();
                    let window_bounds = window.raw_handle().logical_bounds()?;

                    return Some(CursorCropBounds::new_macos(LogicalBounds::new(
                        LogicalPosition::new(
                            window_bounds.position().x() - display_position.x(),
                            window_bounds.position().y() - display_position.y(),
                        ),
                        window_bounds.size(),
                    )));
                }

                #[cfg(windows)]
                #[allow(clippy::needless_return)]
                {
                    let display_bounds = self.display()?.raw_handle().physical_bounds()?;
                    let window_bounds = window.raw_handle().physical_bounds()?;

                    return Some(CursorCropBounds::new_windows(PhysicalBounds::new(
                        PhysicalPosition::new(
                            window_bounds.position().x() - display_bounds.position().x(),
                            window_bounds.position().y() - display_bounds.position().y(),
                        ),
                        PhysicalSize::new(
                            window_bounds.size().width(),
                            window_bounds.size().height(),
                        ),
                    )));
                }

                #[cfg(target_os = "linux")]
                #[allow(clippy::needless_return)]
                {
                    let display_bounds = self.display()?.raw_handle().physical_bounds()?;
                    let window_bounds = window.raw_handle().physical_bounds()?;

                    return Some(CursorCropBounds::new_linux(PhysicalBounds::new(
                        PhysicalPosition::new(
                            window_bounds.position().x() - display_bounds.position().x(),
                            window_bounds.position().y() - display_bounds.position().y(),
                        ),
                        PhysicalSize::new(
                            window_bounds.size().width(),
                            window_bounds.size().height(),
                        ),
                    )));
                }
            }
            Self::Area { bounds, .. } => {
                #[cfg(target_os = "macos")]
                #[allow(clippy::needless_return)]
                {
                    return Some(CursorCropBounds::new_macos(*bounds));
                }

                #[cfg(windows)]
                #[allow(clippy::needless_return)]
                {
                    let display = self.display()?;
                    let bounds = logical_area_to_physical_bounds(
                        *bounds,
                        display.logical_size()?,
                        display.physical_size()?,
                    )?;

                    return Some(CursorCropBounds::new_windows(bounds));
                }

                #[cfg(target_os = "linux")]
                #[allow(clippy::needless_return)]
                {
                    let display = self.display()?;
                    let bounds = logical_area_to_physical_bounds(
                        *bounds,
                        display.logical_size()?,
                        display.physical_size()?,
                    )?;

                    return Some(CursorCropBounds::new_linux(bounds));
                }
            }
            Self::CameraOnly => None,
        }
    }

    pub fn physical_size(&self) -> Option<PhysicalSize> {
        match self {
            Self::Display { id } => Display::from_id(id).and_then(|d| d.physical_size()),
            Self::Window { id } => Window::from_id(id).and_then(|w| w.physical_size()),
            Self::Area { bounds, .. } => {
                let display = self.display()?;

                #[cfg(target_os = "macos")]
                {
                    let scale = display.physical_size()?.width() / display.logical_size()?.width();
                    let size = bounds.size();

                    Some(PhysicalSize::new(
                        size.width() * scale,
                        size.height() * scale,
                    ))
                }

                #[cfg(any(windows, target_os = "linux"))]
                {
                    Some(
                        logical_area_to_physical_bounds(
                            *bounds,
                            display.logical_size()?,
                            display.physical_size()?,
                        )?
                        .size(),
                    )
                }
            }
            Self::CameraOnly => None,
        }
    }

    pub fn title(&self) -> Option<String> {
        match self {
            Self::Display { id } => Display::from_id(id).and_then(|d| d.name()),
            Self::Window { id } => Window::from_id(id).and_then(|w| w.name()),
            Self::Area { screen, .. } => Display::from_id(screen).and_then(|d| d.name()),
            Self::CameraOnly => Some("Camera".to_string()),
        }
    }

    pub fn kind_str(&self) -> &str {
        match self {
            ScreenCaptureTarget::Display { .. } => "Display",
            ScreenCaptureTarget::Window { .. } => "Window",
            ScreenCaptureTarget::Area { .. } => "Area",
            ScreenCaptureTarget::CameraOnly => "Camera",
        }
    }
}

pub struct ScreenCaptureConfig<TCaptureFormat: ScreenCaptureFormat> {
    config: Config,
    video_info: VideoInfo,
    start_time: SystemTime,
    pub system_audio: bool,
    _phantom: std::marker::PhantomData<TCaptureFormat>,
    #[cfg(windows)]
    d3d_device: ::windows::Win32::Graphics::Direct3D11::ID3D11Device,
    #[cfg(target_os = "macos")]
    shareable_content: cidre::arc::R<cidre::sc::ShareableContent>,
    #[cfg(target_os = "macos")]
    pub excluded_windows: Vec<WindowId>,
}

fn constrain_capture_size(size: PhysicalSize, max_size: Option<(u32, u32)>) -> PhysicalSize {
    let width = size.width() as u32;
    let height = size.height() as u32;
    let Some((max_width, max_height)) = max_size else {
        return PhysicalSize::new(ensure_even(width) as f64, ensure_even(height) as f64);
    };

    if width <= max_width && height <= max_height {
        return PhysicalSize::new(ensure_even(width) as f64, ensure_even(height) as f64);
    }

    let width_scale = max_width as f64 / width as f64;
    let height_scale = max_height as f64 / height as f64;
    let scale = width_scale.min(height_scale);
    let constrained_width = ensure_even((width as f64 * scale).round() as u32);
    let constrained_height = ensure_even((height as f64 * scale).round() as u32);

    tracing::info!(
        input_width = width,
        input_height = height,
        output_width = constrained_width,
        output_height = constrained_height,
        max_width,
        max_height,
        "Screen capture input constrained for camera recording"
    );

    PhysicalSize::new(constrained_width as f64, constrained_height as f64)
}

impl<T: ScreenCaptureFormat> std::fmt::Debug for ScreenCaptureConfig<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ScreenCaptureSource")
            // .field("bounds", &self.bounds)
            // .field("output_resolution", &self.output_resolution)
            .field("fps", &self.config.fps)
            .field("video_info", &self.video_info)
            .finish()
    }
}

unsafe impl<T: ScreenCaptureFormat> Send for ScreenCaptureConfig<T> {}
unsafe impl<T: ScreenCaptureFormat> Sync for ScreenCaptureConfig<T> {}

pub trait ScreenCaptureFormat {
    type VideoFormat;

    fn pixel_format() -> ffmpeg::format::Pixel;

    fn audio_info() -> AudioInfo;
}

impl<TCaptureFormat: ScreenCaptureFormat> Clone for ScreenCaptureConfig<TCaptureFormat> {
    fn clone(&self) -> Self {
        Self {
            config: self.config.clone(),
            video_info: self.video_info,
            start_time: self.start_time,
            system_audio: self.system_audio,
            _phantom: std::marker::PhantomData,
            #[cfg(windows)]
            d3d_device: self.d3d_device.clone(),
            #[cfg(target_os = "macos")]
            shareable_content: self.shareable_content.clone(),
            #[cfg(target_os = "macos")]
            excluded_windows: self.excluded_windows.clone(),
        }
    }
}

#[derive(Clone, Debug)]
pub struct Config {
    display: DisplayId,
    crop_bounds: Option<CropBounds>,
    fps: u32,
    show_cursor: bool,
    #[cfg(target_os = "linux")]
    linux_source: LinuxCaptureSource,
    /// Capture this window itself rather than a crop of its display.
    #[cfg(any(target_os = "macos", windows))]
    window: Option<WindowId>,
}

#[cfg(target_os = "macos")]
pub type CropBounds = LogicalBounds;

/// macOS hands a single window over with its rounded corners transparent,
/// which the encoder fills with black. Window-only capture reads this many
/// points in from every edge: a corner's curve clears 0.29 of its radius in,
/// and this covers radii up to 27 pt.
#[cfg(target_os = "macos")]
pub const WINDOW_CORNER_TRIM: f64 = 8.0;

/// `bounds` shrunk to whole, even pixel dimensions at `scale`. The encoder
/// needs even frames; a region an odd number of pixels across would be read
/// whole and squeezed a pixel smaller, resampling, and so softening, every
/// line of text. Reading the even region instead copies pixels one to one.
#[cfg(target_os = "macos")]
pub fn even_pixel_crop(bounds: CropBounds, scale: f64) -> CropBounds {
    let size = bounds.size();
    let pixels = |points: f64| ensure_even((points * scale).floor() as u32) as f64 / scale;
    LogicalBounds::new(
        bounds.position(),
        LogicalSize::new(pixels(size.width()), pixels(size.height())),
    )
}

/// `bounds` with [`WINDOW_CORNER_TRIM`] taken off every side, or as it was
/// when the window is too small to trim.
#[cfg(target_os = "macos")]
pub fn trim_window_corners(bounds: CropBounds) -> CropBounds {
    let size = bounds.size();
    let trim = WINDOW_CORNER_TRIM;
    if size.width() <= trim * 4. || size.height() <= trim * 4. {
        return bounds;
    }
    let position = bounds.position();
    LogicalBounds::new(
        LogicalPosition::new(position.x() + trim, position.y() + trim),
        LogicalSize::new(size.width() - 2. * trim, size.height() - 2. * trim),
    )
}

#[cfg(windows)]
pub type CropBounds = PhysicalBounds;

#[cfg(target_os = "linux")]
pub type CropBounds = PhysicalBounds;

#[cfg(any(windows, target_os = "linux", test))]
pub(crate) fn logical_area_to_physical_bounds(
    bounds: LogicalBounds,
    logical_display_size: LogicalSize,
    physical_display_size: PhysicalSize,
) -> Option<PhysicalBounds> {
    let (x, width) = logical_axis_to_physical(
        bounds.position().x(),
        bounds.size().width(),
        logical_display_size.width(),
        physical_display_size.width(),
    )?;
    let (y, height) = logical_axis_to_physical(
        bounds.position().y(),
        bounds.size().height(),
        logical_display_size.height(),
        physical_display_size.height(),
    )?;

    Some(PhysicalBounds::new(
        PhysicalPosition::new(x, y),
        PhysicalSize::new(width, height),
    ))
}

#[cfg(any(windows, target_os = "linux", test))]
fn logical_axis_to_physical(
    position: f64,
    size: f64,
    logical_extent: f64,
    physical_extent: f64,
) -> Option<(f64, f64)> {
    if !position.is_finite()
        || !size.is_finite()
        || !logical_extent.is_finite()
        || !physical_extent.is_finite()
        || logical_extent <= 0.0
        || physical_extent <= 0.0
    {
        return None;
    }

    let start = position.clamp(0.0, logical_extent);
    let end = (position + size).clamp(0.0, logical_extent);
    let scale = physical_extent / logical_extent;
    let physical_start = (start.min(end) * scale).round();
    let physical_end = (start.max(end) * scale).round();
    let min_size = 2.0_f64.min(physical_extent);
    let size = (physical_end - physical_start).clamp(min_size, physical_extent);
    let position = physical_start.min(physical_extent - size).max(0.0);

    Some((position, size))
}

impl Config {
    pub fn fps(&self) -> u32 {
        self.fps
    }
}

#[derive(Debug, Clone, thiserror::Error)]
pub enum ScreenCaptureInitError {
    #[error("NoDisplay")]
    NoDisplay,
    #[error("NoWindow")]
    NoWindow,
    #[error("Bounds")]
    NoBounds,
}

impl<TCaptureFormat: ScreenCaptureFormat> ScreenCaptureConfig<TCaptureFormat> {
    #[allow(clippy::too_many_arguments)]
    pub async fn init(
        display: scap_targets::Display,
        crop_bounds: Option<CropBounds>,
        show_cursor: bool,
        max_fps: u32,
        max_capture_size: Option<(u32, u32)>,
        start_time: SystemTime,
        system_audio: bool,
        #[cfg(any(target_os = "macos", windows))] window: Option<WindowId>,
        #[cfg(target_os = "linux")] linux_source: LinuxCaptureSource,
        #[cfg(windows)] d3d_device: ::windows::Win32::Graphics::Direct3D11::ID3D11Device,
        #[cfg(target_os = "macos")] shareable_content: SendableShareableContent,
        #[cfg(target_os = "macos")] excluded_windows: Vec<WindowId>,
    ) -> Result<Self, ScreenCaptureInitError> {
        cap_fail::fail!("ScreenCaptureSource::init");

        let target_refresh = validated_refresh_rate(display.refresh_rate());
        let fps = std::cmp::max(1, std::cmp::min(max_fps, target_refresh));

        // The capture reads the window inside its corners (see
        // `WINDOW_CORNER_TRIM`), so the output and the cursor crop match that.
        #[cfg(target_os = "macos")]
        let crop_bounds = match window {
            Some(_) => crop_bounds.map(trim_window_corners),
            None => crop_bounds,
        };
        // Read exactly as many pixels as the frame holds (see `even_pixel_crop`).
        #[cfg(target_os = "macos")]
        let crop_bounds = crop_bounds
            .map(|bounds| even_pixel_crop(bounds, display.raw_handle().scale().unwrap_or(1.0)));

        let native_output_size: PhysicalSize = {
            #[cfg(target_os = "macos")]
            {
                crop_bounds.and_then(|b| {
                    let logical_size = b.size();
                    let scale = display.raw_handle().scale()?;
                    let width = ensure_even((logical_size.width() * scale).round() as u32) as f64;
                    let height = ensure_even((logical_size.height() * scale).round() as u32) as f64;
                    Some(PhysicalSize::new(width, height))
                })
            }

            #[cfg(target_os = "windows")]
            {
                window
                    .as_ref()
                    .and_then(window_capture_size)
                    .or_else(|| crop_bounds.map(|b| b.size().map(|v| (v / 2.0).floor() * 2.0)))
            }

            #[cfg(target_os = "linux")]
            {
                crop_bounds.map(|b| b.size().map(|v| (v / 2.0).floor() * 2.0))
            }
        }
        .or_else(|| display.physical_size())
        .ok_or(ScreenCaptureInitError::NoBounds)?;
        let output_size = constrain_capture_size(native_output_size, max_capture_size);

        // Diagnostic anchor for area/crop capture: `crop_bounds` is the region the
        // capturer will read (physical px on Windows/Linux, logical pts on macOS).
        // For a selection spanning the whole monitor the crop size should match the
        // display's physical size — a smaller value here means the overlay/crop was
        // computed against the wrong coordinate space.
        // (Hoisted into a local because `use tracing::*` glob-imports a `display`
        // helper that would otherwise shadow the `display` binding inside the macro.)
        let display_physical_size = display.physical_size();
        tracing::info!(
            crop_bounds = ?crop_bounds,
            ?display_physical_size,
            native_output_width = native_output_size.width(),
            native_output_height = native_output_size.height(),
            output_width = output_size.width(),
            output_height = output_size.height(),
            "Screen capture configured"
        );

        Ok(Self {
            config: Config {
                display: display.id(),
                crop_bounds,
                fps,
                show_cursor,
                #[cfg(target_os = "linux")]
                linux_source,
                #[cfg(any(target_os = "macos", windows))]
                window,
            },
            video_info: VideoInfo::from_raw_ffmpeg(
                TCaptureFormat::pixel_format(),
                output_size.width() as u32,
                output_size.height() as u32,
                fps,
            ),
            start_time,
            system_audio,
            _phantom: std::marker::PhantomData,
            #[cfg(windows)]
            d3d_device,
            #[cfg(target_os = "macos")]
            shareable_content: shareable_content.retained(),
            #[cfg(target_os = "macos")]
            excluded_windows,
        })
    }

    #[cfg(windows)]
    pub fn d3d_device(&self) -> &::windows::Win32::Graphics::Direct3D11::ID3D11Device {
        &self.d3d_device
    }

    pub fn config(&self) -> &Config {
        &self.config
    }

    pub fn info(&self) -> VideoInfo {
        self.video_info
    }

    pub fn audio_info(&self) -> AudioInfo {
        TCaptureFormat::audio_info()
    }
}

fn validated_refresh_rate<T>(reported_refresh_rate: T) -> u32
where
    T: Into<f64>,
{
    let reported_refresh_rate = reported_refresh_rate.into();
    let fallback_refresh = 60;
    let rounded_refresh = reported_refresh_rate.round();
    let is_invalid_refresh = !rounded_refresh.is_finite() || rounded_refresh <= 0.0;
    let capped_refresh = if is_invalid_refresh {
        fallback_refresh as f64
    } else {
        rounded_refresh.min(500.0)
    };

    if is_invalid_refresh {
        warn!(
            ?reported_refresh_rate,
            fallback = fallback_refresh,
            "Display reported invalid refresh rate; falling back to default"
        );
        fallback_refresh
    } else {
        capped_refresh as u32
    }
}

pub fn list_displays() -> Vec<(CaptureDisplay, Display)> {
    scap_targets::Display::list()
        .into_iter()
        .map(|display| {
            let refresh_rate = validated_refresh_rate(display.raw_handle().refresh_rate());

            (
                CaptureDisplay {
                    id: display.id(),
                    name: display
                        .name()
                        .unwrap_or_else(|| format!("Display {}", display.id())),
                    refresh_rate,
                },
                display,
            )
        })
        .collect()
}

// NSFloatingWindowLevel is 3; Stay On Top moves Simulator windows to this level.
#[cfg(target_os = "macos")]
const MACOS_FLOATING_WINDOW_LEVEL: i32 = 3;

#[cfg(target_os = "macos")]
pub fn is_listable_macos_window(
    level: Option<i32>,
    owner_name: &str,
    bundle_identifier: Option<&str>,
    include_accessory_panels: bool,
    is_accessory_application: bool,
) -> bool {
    if owner_name == "Window Server" {
        return false;
    }

    if level == Some(0) {
        return true;
    }

    let is_third_party_bundle = bundle_identifier.is_some_and(|identifier| {
        !identifier.starts_with("com.apple.") && !identifier.starts_with("so.cap.desktop")
    });

    if is_accessory_application {
        return matches!(level, Some(level) if level > 0)
            && include_accessory_panels
            && is_third_party_bundle;
    }

    level == Some(MACOS_FLOATING_WINDOW_LEVEL)
        && bundle_identifier.is_some_and(|identifier| !identifier.starts_with("so.cap.desktop"))
}

pub fn list_windows() -> Vec<(CaptureWindow, Window)> {
    list_windows_inner(false)
}

pub fn list_excludable_windows() -> Vec<(CaptureWindow, Window)> {
    list_windows_inner(true)
}

fn list_windows_inner(_include_accessory_panels: bool) -> Vec<(CaptureWindow, Window)> {
    scap_targets::Window::list()
        .into_iter()
        .flat_map(|v| {
            let name = v.name()?;

            if name.is_empty() {
                return None;
            }

            #[cfg(target_os = "macos")]
            let (owner_name, bundle_identifier) = {
                let owner_name = v.owner_name()?;
                let level = v.raw_handle().level();
                let bundle_identifier = v.raw_handle().bundle_identifier();
                let is_accessory_application = matches!(level, Some(level) if level > 0)
                    && v.raw_handle().is_accessory_application();

                if !is_listable_macos_window(
                    level,
                    &owner_name,
                    bundle_identifier.as_deref(),
                    _include_accessory_panels,
                    is_accessory_application,
                ) {
                    return None;
                }

                (owner_name, bundle_identifier)
            };

            #[cfg(windows)]
            {
                if !v.raw_handle().is_valid() || !v.raw_handle().is_on_screen() {
                    return None;
                }
            }

            #[cfg(not(target_os = "macos"))]
            let owner_name = v.owner_name()?;

            #[cfg(not(target_os = "macos"))]
            let bundle_identifier = None;

            let refresh_rate = v
                .display()
                .map(|display| validated_refresh_rate(display.raw_handle().refresh_rate()))?;

            Some((
                CaptureWindow {
                    id: v.id(),
                    name,
                    owner_name,
                    bounds: v.display_relative_logical_bounds()?,
                    refresh_rate,
                    bundle_identifier,
                },
                v,
            ))
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn h264_bounded_capture_matches_the_encoder_downscale() {
        for (width, height) in [
            (5120, 2880),
            (6016, 3384),
            (5120, 2160),
            (2880, 5120),
            (4096, 2304),
        ] {
            let bounded = constrain_capture_size(
                PhysicalSize::new(width as f64, height as f64),
                Some((crate::H264_MAX_DIMENSION, crate::H264_MAX_DIMENSION)),
            );
            let encoder_size =
                crate::calculate_gpu_compatible_size(width, height, crate::H264_MAX_DIMENSION)
                    .unwrap_or((width, height));
            assert_eq!(
                (bounded.width() as u32, bounded.height() as u32),
                encoder_size,
                "{width}x{height}"
            );
        }
    }

    #[test]
    #[cfg(target_os = "linux")]
    fn linux_window_source_preserves_selected_window_id() {
        let id: WindowId = "247".parse().unwrap();
        let target = ScreenCaptureTarget::Window { id: id.clone() };
        let LinuxCaptureSource::Window { id: selected } = LinuxCaptureSource::from_target(&target)
        else {
            panic!("window target must retain a window source");
        };
        assert_eq!(selected, id);
    }

    #[test]
    fn logical_area_to_physical_bounds_scales_each_axis() {
        let bounds = LogicalBounds::new(
            LogicalPosition::new(120.0, 80.0),
            LogicalSize::new(640.0, 360.0),
        );
        let physical = logical_area_to_physical_bounds(
            bounds,
            LogicalSize::new(1920.0, 1080.0),
            PhysicalSize::new(3840.0, 2160.0),
        )
        .unwrap();

        assert_eq!(physical.position().x(), 240.0);
        assert_eq!(physical.position().y(), 160.0);
        assert_eq!(physical.size().width(), 1280.0);
        assert_eq!(physical.size().height(), 720.0);
    }

    #[test]
    #[cfg(target_os = "macos")]
    fn macos_window_filter_adds_only_third_party_accessory_panels() {
        assert!(is_listable_macos_window(
            Some(0),
            "Example",
            Some("com.example.app"),
            false,
            false,
        ));
        assert!(!is_listable_macos_window(
            Some(3),
            "FreeCastNotes",
            Some("fi.sherbakov.freecastnotes"),
            false,
            true,
        ));
        assert!(is_listable_macos_window(
            Some(3),
            "FreeCastNotes",
            Some("fi.sherbakov.freecastnotes"),
            true,
            true,
        ));
        assert!(is_listable_macos_window(
            Some(3),
            "Example",
            Some("com.example.app"),
            true,
            false,
        ));
        assert!(!is_listable_macos_window(
            Some(25),
            "Control Centre",
            Some("com.apple.controlcenter"),
            true,
            true,
        ));
        assert!(!is_listable_macos_window(
            Some(3),
            "Cap",
            Some("so.cap.desktop.dev"),
            true,
            true,
        ));
        assert!(!is_listable_macos_window(
            Some(0),
            "Window Server",
            None,
            true,
            false,
        ));
        assert!(!is_listable_macos_window(
            None,
            "Example",
            Some("com.example.app"),
            true,
            true,
        ));
    }

    #[test]
    #[cfg(target_os = "macos")]
    fn macos_window_filter_lists_floating_app_windows_in_the_picker() {
        assert!(is_listable_macos_window(
            Some(MACOS_FLOATING_WINDOW_LEVEL),
            "Simulator",
            Some("com.apple.iphonesimulator"),
            false,
            false,
        ));
        assert!(is_listable_macos_window(
            Some(MACOS_FLOATING_WINDOW_LEVEL),
            "Example",
            Some("com.example.app"),
            false,
            false,
        ));
        assert!(!is_listable_macos_window(
            Some(MACOS_FLOATING_WINDOW_LEVEL),
            "Cap",
            Some("so.cap.desktop.dev"),
            false,
            false,
        ));
        assert!(!is_listable_macos_window(
            Some(MACOS_FLOATING_WINDOW_LEVEL),
            "cap-gpui",
            None,
            false,
            false,
        ));
        assert!(!is_listable_macos_window(
            Some(MACOS_FLOATING_WINDOW_LEVEL + 1),
            "Example",
            Some("com.example.app"),
            false,
            false,
        ));
        assert!(!is_listable_macos_window(
            Some(25),
            "Control Centre",
            Some("com.apple.controlcenter"),
            false,
            false,
        ));
    }

    #[test]
    #[cfg(target_os = "macos")]
    fn macos_window_filter_preserves_normal_windows_and_rejects_other_levels() {
        for accessory in [false, true] {
            assert!(is_listable_macos_window(
                Some(0),
                "Example",
                None,
                false,
                accessory
            ));
        }
        for level in [None, Some(-1), Some(1), Some(2), Some(4), Some(25)] {
            for include_accessory_panels in [false, true] {
                assert!(!is_listable_macos_window(
                    level,
                    "Example",
                    Some("com.example.app"),
                    include_accessory_panels,
                    false,
                ));
            }
        }
    }

    #[test]
    #[cfg(target_os = "macos")]
    fn macos_window_filter_preserves_accessory_panel_exclusions() {
        for level in [Some(3), Some(25)] {
            for bundle in [
                None,
                Some("com.apple.controlcenter"),
                Some("so.cap.desktop.dev"),
            ] {
                for include_accessory_panels in [false, true] {
                    assert!(!is_listable_macos_window(
                        level,
                        "Panel",
                        bundle,
                        include_accessory_panels,
                        true,
                    ));
                }
            }
            assert!(!is_listable_macos_window(
                level,
                "Panel",
                Some("com.example.app"),
                false,
                true,
            ));
            assert!(is_listable_macos_window(
                level,
                "Panel",
                Some("com.example.app"),
                true,
                true,
            ));
        }
        for level in [Some(0), Some(3)] {
            assert!(!is_listable_macos_window(
                level,
                "Window Server",
                Some("com.example.app"),
                false,
                false,
            ));
        }
    }

    #[test]
    fn logical_area_to_physical_bounds_clamps_to_display() {
        let bounds = LogicalBounds::new(
            LogicalPosition::new(-10.0, 100.0),
            LogicalSize::new(2_000.0, 1_000.0),
        );
        let physical = logical_area_to_physical_bounds(
            bounds,
            LogicalSize::new(1920.0, 1080.0),
            PhysicalSize::new(3840.0, 2160.0),
        )
        .unwrap();

        assert_eq!(physical.position().x(), 0.0);
        assert_eq!(physical.position().y(), 200.0);
        assert_eq!(physical.size().width(), 3840.0);
        assert_eq!(physical.size().height(), 1960.0);
    }

    #[test]
    fn logical_area_to_physical_bounds_keeps_edge_crop_inside_display() {
        let bounds = LogicalBounds::new(
            LogicalPosition::new(1930.0, -20.0),
            LogicalSize::new(100.0, 0.0),
        );
        let physical = logical_area_to_physical_bounds(
            bounds,
            LogicalSize::new(1920.0, 1080.0),
            PhysicalSize::new(3840.0, 2160.0),
        )
        .unwrap();

        assert_eq!(physical.position().x(), 3838.0);
        assert_eq!(physical.position().y(), 0.0);
        assert_eq!(physical.size().width(), 2.0);
        assert_eq!(physical.size().height(), 2.0);
    }

    // Regression coverage for the HiDPI secondary-monitor area bug: a selection
    // that spans the entire overlay (== the display's logical size) must map to
    // the entire physical display, with no truncation, at every common scale.
    // These pin the invariant that made "records ~70% of the selection" possible
    // once the overlay is correctly sized to cover the monitor.
    #[test]
    fn full_selection_covers_full_display_at_every_scale() {
        // (logical_size, physical_size) pairs for 100/125/150/175/200% scaling.
        let scales = [
            (
                LogicalSize::new(1920.0, 1080.0),
                PhysicalSize::new(1920.0, 1080.0),
            ), // 100%
            (
                LogicalSize::new(1536.0, 864.0),
                PhysicalSize::new(1920.0, 1080.0),
            ), // 125%
            (
                LogicalSize::new(1280.0, 720.0),
                PhysicalSize::new(1920.0, 1080.0),
            ), // 150%
            (
                LogicalSize::new(1_097.142_857_142_857, 617.142_857_142_857),
                PhysicalSize::new(1920.0, 1080.0),
            ), // 175%
            (
                LogicalSize::new(1280.0, 720.0),
                PhysicalSize::new(2560.0, 1440.0),
            ), // 200%
        ];

        for (logical, physical) in scales {
            let selection = LogicalBounds::new(LogicalPosition::new(0.0, 0.0), logical);
            let crop = logical_area_to_physical_bounds(selection, logical, physical).unwrap();

            assert_eq!(
                crop.position().x(),
                0.0,
                "full selection must start at x=0 (logical {logical:?})"
            );
            assert_eq!(
                crop.position().y(),
                0.0,
                "full selection must start at y=0 (logical {logical:?})"
            );
            assert_eq!(
                crop.size().width(),
                physical.width(),
                "full selection must span full physical width (logical {logical:?})"
            );
            assert_eq!(
                crop.size().height(),
                physical.height(),
                "full selection must span full physical height (logical {logical:?})"
            );
        }
    }

    #[test]
    fn fractional_area_edges_round_to_pixels_and_stay_inside_the_display() {
        for (position, size, expected) in [
            (100.2, 800.4, (150.0, 1201.0)),
            (-0.2, 100.4, (0.0, 150.0)),
            (1279.8, 10.0, (1918.0, 2.0)),
        ] {
            assert_eq!(
                logical_axis_to_physical(position, size, 1280.0, 1920.0),
                Some(expected),
            );
        }
    }

    #[test]
    fn full_overlay_selection_keeps_native_resolution_at_fractional_scales() {
        for scale in [1.25, 1.5, 1.75] {
            let physical = PhysicalSize::new(1920.0, 1080.0);
            let logical = LogicalSize::new(physical.width() / scale, physical.height() / scale);
            let overlay_size = LogicalSize::new(
                f64::from(logical.width() as f32),
                f64::from(logical.height() as f32),
            );
            let selection = LogicalBounds::new(LogicalPosition::new(0.0, 0.0), overlay_size);
            let crop = logical_area_to_physical_bounds(selection, logical, physical).unwrap();
            let output = constrain_capture_size(crop.size(), None);

            assert_eq!(output.width(), physical.width(), "scale {scale}");
            assert_eq!(output.height(), physical.height(), "scale {scale}");
        }
    }

    #[test]
    fn partial_selection_scales_by_display_dpi() {
        // 150% display: logical 1280x720 backed by physical 1920x1080.
        let logical = LogicalSize::new(1280.0, 720.0);
        let physical = PhysicalSize::new(1920.0, 1080.0);
        let selection = LogicalBounds::new(
            LogicalPosition::new(100.0, 100.0),
            LogicalSize::new(500.0, 300.0),
        );

        let crop = logical_area_to_physical_bounds(selection, logical, physical).unwrap();

        // Every axis multiplied by the 1.5 device scale.
        assert_eq!(crop.position().x(), 150.0);
        assert_eq!(crop.position().y(), 150.0);
        assert_eq!(crop.size().width(), 750.0);
        assert_eq!(crop.size().height(), 450.0);
    }
}

#[cfg(all(test, target_os = "macos"))]
mod window_corner_tests {
    use super::*;

    #[test]
    fn window_capture_reads_inside_the_rounded_corners() {
        let window = LogicalBounds::new(
            LogicalPosition::new(100., 50.),
            LogicalSize::new(920., 436.),
        );
        let trimmed = trim_window_corners(window);
        assert_eq!(trimmed.position().x(), 108.);
        assert_eq!(trimmed.position().y(), 58.);
        assert_eq!(trimmed.size().width(), 904.);
        assert_eq!(trimmed.size().height(), 420.);
    }

    #[test]
    fn crops_read_whole_even_pixels() {
        let odd = LogicalBounds::new(LogicalPosition::new(8., 8.), LogicalSize::new(1690., 753.));
        let even = even_pixel_crop(odd, 1.0);
        assert_eq!(even.size().width(), 1690.);
        assert_eq!(even.size().height(), 752.);
        assert_eq!(even.position().x(), 8.);
        // At 2x, a half point is a whole pixel: 753.5 pt is 1507 px, read as 1506.
        let retina = even_pixel_crop(
            LogicalBounds::new(LogicalPosition::new(0., 0.), LogicalSize::new(845., 753.5)),
            2.0,
        );
        assert_eq!(retina.size().width() * 2.0, 1690.);
        assert_eq!(retina.size().height() * 2.0, 1506.);
    }

    #[test]
    fn a_tiny_window_is_left_whole() {
        let window = LogicalBounds::new(LogicalPosition::new(0., 0.), LogicalSize::new(30., 200.));
        let kept = trim_window_corners(window);
        assert_eq!(kept.position().x(), 0.);
        assert_eq!(kept.size().width(), 30.);
        assert_eq!(kept.size().height(), 200.);
    }
}