impl Handler {
    /// Generate deterministic media on the server and feed UVC/UAC directly.
    async fn run_output_delay_probe_rpc(
        &self,
        req: Request<OutputDelayProbeRequest>,
    ) -> Result<Response<ReceiverStream<Result<OutputDelayProbeReply, Status>>>, Status> {
        let rpc_id = runtime_support::next_stream_id();
        let request = req.into_inner();
        let camera_cfg = camera::current_camera_config();
        let bundled_leases = self.upstream_media_rt.activate_bundled_session();
        let microphone_lease = bundled_leases.microphone;
        let camera_lease = bundled_leases.camera;
        info!(
            rpc_id,
            session_id = camera_lease.session_id,
            camera_generation = camera_lease.generation,
            microphone_generation = microphone_lease.generation,
            output = camera_cfg.output.as_str(),
            codec = camera_cfg.codec.as_str(),
            width = camera_cfg.width,
            height = camera_cfg.height,
            fps = camera_cfg.fps,
            "🧪 server output-delay probe opened"
        );
        let (camera_session_id, relay, _relay_reused) =
            match self.camera_rt.activate(&camera_cfg).await {
                Ok(active) => active,
                Err(err) => {
                    self.upstream_media_rt.close_camera(camera_lease.generation);
                    self.upstream_media_rt.close_microphone(microphone_lease.generation);
                    return Err(err);
                }
            };
        let Some(microphone_sink_permit) = self
            .upstream_media_rt
            .reserve_microphone_sink(microphone_lease.generation)
            .await
        else {
            self.upstream_media_rt.close_camera(camera_lease.generation);
            self.upstream_media_rt.close_microphone(microphone_lease.generation);
            return Err(Status::aborted(
                "output-delay probe superseded before microphone sink became available",
            ));
        };
        let uac_dev = std::env::var("LESAVKA_UAC_DEV").unwrap_or_else(|_| "hw:UAC2Gadget,0".into());
        let mut sink = runtime_support::open_voice_with_retry(&uac_dev)
            .await
            .map_err(|e| {
                self.upstream_media_rt.close_camera(camera_lease.generation);
                self.upstream_media_rt.close_microphone(microphone_lease.generation);
                Status::internal(format!("{e:#}"))
            })?;
        let camera_rt = self.camera_rt.clone();
        let upstream_media_rt = self.upstream_media_rt.clone();
        let (tx, rx) = tokio::sync::mpsc::channel(1);

        tokio::spawn(async move {
            let _microphone_sink_permit = microphone_sink_permit;
            let result = if !camera_rt.is_active(camera_session_id)
                || !upstream_media_rt.is_camera_active(camera_lease.generation)
                || !upstream_media_rt.is_microphone_active(microphone_lease.generation)
            {
                Err(anyhow::anyhow!("output-delay probe superseded before start"))
            } else {
                lesavka_server::output_delay_probe::run_server_output_delay_probe(
                    relay,
                    &mut sink,
                    &camera_cfg,
                    &request,
                )
                .await
            };
            upstream_media_rt.close_camera(camera_lease.generation);
            upstream_media_rt.close_microphone(microphone_lease.generation);
            match result {
                Ok(summary) => {
                    let detail = format!(
                        "server-generated UVC/UAC output-delay probe complete: video_frames={} audio_packets={} events={}",
                        summary.video_frames, summary.audio_packets, summary.event_count
                    );
                    info!(
                        rpc_id,
                        session_id = camera_lease.session_id,
                        camera_session_id,
                        detail,
                        "🧪 server output-delay probe closed"
                    );
                    tx.send(Ok(OutputDelayProbeReply {
                        ok: true,
                        detail,
                        server_timeline_json: summary.timeline_json,
                    }))
                    .await
                    .ok();
                }
                Err(err) => {
                    warn!(
                        rpc_id,
                        session_id = camera_lease.session_id,
                        camera_session_id,
                        "🧪 server output-delay probe failed: {err:#}"
                    );
                    tx.send(Err(Status::internal(format!("{err:#}")))).await.ok();
                }
            }
        });

        Ok(Response::new(ReceiverStream::new(rx)))
    }
}
