#[cfg(not(coverage))]
impl LauncherPreview {
    pub fn new(server_addr: String) -> Result<Self> {
        gst::init().context("initialising preview gstreamer")?;
        let server_addr = Arc::new(Mutex::new(server_addr));
        let log_sink = Arc::new(Mutex::new(None));
        let inline_feeds = Arc::new(Mutex::new([
            PreviewFeed::spawn(
                Arc::clone(&server_addr),
                0,
                PreviewSurface::Inline.profile(),
                Arc::clone(&log_sink),
            )?,
            PreviewFeed::spawn(
                Arc::clone(&server_addr),
                1,
                PreviewSurface::Inline.profile(),
                Arc::clone(&log_sink),
            )?,
        ]));
        let window_feeds = Arc::new(Mutex::new([
            PreviewFeed::spawn(
                Arc::clone(&server_addr),
                0,
                PreviewSurface::Window.profile(),
                Arc::clone(&log_sink),
            )?,
            PreviewFeed::spawn(
                Arc::clone(&server_addr),
                1,
                PreviewSurface::Window.profile(),
                Arc::clone(&log_sink),
            )?,
        ]));
        Ok(Self {
            server_addr: Arc::clone(&server_addr),
            log_sink: Arc::clone(&log_sink),
            inline_feeds,
            window_feeds,
        })
    }

    pub fn set_log_sink(&self, tx: std::sync::mpsc::Sender<String>) {
        if let Ok(mut slot) = self.log_sink.lock() {
            *slot = Some(tx);
        }
    }

    pub fn set_server_addr(&self, server_addr: String) {
        if let Ok(mut slot) = self.server_addr.lock() {
            *slot = server_addr;
        }
    }

    pub fn set_session_active(&self, active: bool) {
        if let Ok(feeds) = self.inline_feeds.lock() {
            for feed in feeds.iter() {
                feed.set_active(active);
            }
        }
        if let Ok(feeds) = self.window_feeds.lock() {
            for feed in feeds.iter() {
                feed.set_active(active);
            }
        }
    }

    pub fn shutdown_all(&self) {
        if let Ok(feeds) = self.inline_feeds.lock() {
            for feed in feeds.iter() {
                feed.shutdown();
            }
        }
        if let Ok(feeds) = self.window_feeds.lock() {
            for feed in feeds.iter() {
                feed.shutdown();
            }
        }
    }

    pub fn install_on_picture(
        &self,
        monitor_id: usize,
        surface: PreviewSurface,
        picture: &gtk::Picture,
        status_label: &gtk::Label,
    ) -> Option<PreviewBinding> {
        match surface {
            PreviewSurface::Inline => self
                .inline_feeds
                .lock()
                .ok()
                .and_then(|feeds| feeds.get(monitor_id).cloned())
                .map(|feed| feed.install_on_picture(picture, status_label)),
            PreviewSurface::Window => self
                .window_feeds
                .lock()
                .ok()
                .and_then(|feeds| feeds.get(monitor_id).cloned())
                .map(|feed| feed.install_on_picture(picture, status_label)),
        }
    }

    pub fn snapshot_metrics(
        &self,
        monitor_id: usize,
        surface: PreviewSurface,
    ) -> Option<PreviewMetricsSnapshot> {
        match surface {
            PreviewSurface::Inline => self
                .inline_feeds
                .lock()
                .ok()
                .and_then(|feeds| feeds.get(monitor_id).cloned())
                .map(|feed| feed.snapshot_metrics()),
            PreviewSurface::Window => self
                .window_feeds
                .lock()
                .ok()
                .and_then(|feeds| feeds.get(monitor_id).cloned())
                .map(|feed| feed.snapshot_metrics()),
        }
    }

    pub fn start_recording_tap(
        &self,
        monitor_id: usize,
        surface: PreviewSurface,
    ) -> Option<PreviewRecordingTap> {
        match surface {
            PreviewSurface::Inline => self
                .inline_feeds
                .lock()
                .ok()
                .and_then(|feeds| feeds.get(monitor_id).cloned())
                .and_then(|feed| feed.start_recording_tap()),
            PreviewSurface::Window => self
                .window_feeds
                .lock()
                .ok()
                .and_then(|feeds| feeds.get(monitor_id).cloned())
                .and_then(|feed| feed.start_recording_tap()),
        }
    }

    pub fn set_capture_profile(
        &self,
        monitor_id: usize,
        source_monitor_id: usize,
        requested_width: i32,
        requested_height: i32,
        requested_fps: u32,
        max_bitrate_kbit: u32,
    ) {
        let (
            inline_requested_width,
            inline_requested_height,
            inline_requested_fps,
            inline_max_bitrate_kbit,
        ) = sanitize_preview_request(
            requested_width,
            requested_height,
            requested_fps,
            max_bitrate_kbit,
        );
        self.rebuild_feed(
            &self.inline_feeds,
            monitor_id,
            Some((
                source_monitor_id,
                inline_requested_width,
                inline_requested_height,
                inline_requested_fps,
                inline_max_bitrate_kbit,
            )),
            None,
        );
        self.rebuild_feed(
            &self.window_feeds,
            monitor_id,
            Some((
                source_monitor_id,
                requested_width,
                requested_height,
                requested_fps,
                max_bitrate_kbit,
            )),
            None,
        );
    }

    pub fn set_breakout_profile(&self, monitor_id: usize, width: i32, height: i32) {
        self.rebuild_feed(&self.window_feeds, monitor_id, None, Some((width, height)));
    }

    #[cfg(test)]
    pub(crate) fn profile_for_test(
        &self,
        monitor_id: usize,
        surface: PreviewSurface,
    ) -> Option<(u32, i32, i32, i32, i32, u32, u32)> {
        let feed = match surface {
            PreviewSurface::Inline => self.inline_feeds.lock().ok()?.get(monitor_id).cloned(),
            PreviewSurface::Window => self.window_feeds.lock().ok()?.get(monitor_id).cloned(),
        }?;
        let profile = feed.profile();
        Some((
            profile.source_monitor_id,
            profile.display_width,
            profile.display_height,
            profile.requested_width,
            profile.requested_height,
            profile.requested_fps,
            profile.max_bitrate_kbit,
        ))
    }

    #[cfg(test)]
    pub(crate) fn feed_disabled_for_test(
        &self,
        monitor_id: usize,
        surface: PreviewSurface,
    ) -> Option<bool> {
        let feed = match surface {
            PreviewSurface::Inline => self.inline_feeds.lock().ok()?.get(monitor_id).cloned(),
            PreviewSurface::Window => self.window_feeds.lock().ok()?.get(monitor_id).cloned(),
        }?;
        Some(feed.is_disabled())
    }

    fn rebuild_feed(
        &self,
        feeds: &Arc<Mutex<[PreviewFeed; 2]>>,
        monitor_id: usize,
        requested: Option<(usize, i32, i32, u32, u32)>,
        display: Option<(i32, i32)>,
    ) {
        let Ok(mut feeds) = feeds.lock() else {
            return;
        };
        let Some(existing) = feeds.get(monitor_id).cloned() else {
            return;
        };
        let was_active = existing.is_active();
        let keep_disabled = existing.is_disabled();
        let mut profile = existing.profile();
        if let Some((
            source_monitor_id,
            requested_width,
            requested_height,
            requested_fps,
            max_bitrate_kbit,
        )) = requested
        {
            profile.source_monitor_id = source_monitor_id as u32;
            profile.requested_width = requested_width.max(2);
            profile.requested_height = requested_height.max(2);
            profile.requested_fps = requested_fps.max(1);
            profile.max_bitrate_kbit = max_bitrate_kbit.max(800);
        }
        if let Some((display_width, display_height)) = display {
            profile.display_width = display_width.max(2);
            profile.display_height = display_height.max(2);
        }
        let next_feed = if keep_disabled {
            Some(PreviewFeed::spawn_disabled(profile))
        } else {
            match PreviewFeed::spawn(
                Arc::clone(&self.server_addr),
                monitor_id as u32,
                profile,
                Arc::clone(&self.log_sink),
            ) {
                Ok(feed) => Some(feed),
                Err(err) => {
                    warn!(monitor_id, ?err, "could not rebuild preview feed");
                    None
                }
            }
        };
        if let Some(feed) = next_feed {
            if was_active {
                feed.set_active(true);
            }
            existing.shutdown();
            feeds[monitor_id] = feed;
        }
    }

    pub fn set_monitor_enabled(&self, monitor_id: usize, enabled: bool) {
        self.set_feed_enabled(&self.inline_feeds, monitor_id, enabled);
        self.set_feed_enabled(&self.window_feeds, monitor_id, enabled);
    }

    fn set_feed_enabled(
        &self,
        feeds: &Arc<Mutex<[PreviewFeed; 2]>>,
        monitor_id: usize,
        enabled: bool,
    ) {
        let Ok(mut feeds) = feeds.lock() else {
            return;
        };
        let Some(existing) = feeds.get(monitor_id).cloned() else {
            return;
        };
        if existing.is_disabled() != enabled {
            return;
        }
        let was_active = existing.is_active();
        let profile = existing.profile();
        let replacement = if enabled {
            match PreviewFeed::spawn(
                Arc::clone(&self.server_addr),
                monitor_id as u32,
                profile,
                Arc::clone(&self.log_sink),
            ) {
                Ok(feed) => feed,
                Err(err) => {
                    warn!(monitor_id, ?err, "could not enable preview feed");
                    return;
                }
            }
        } else {
            PreviewFeed::spawn_disabled(profile)
        };
        if was_active {
            replacement.set_active(true);
        }
        existing.shutdown();
        feeds[monitor_id] = replacement;
    }
}
