Skip to main content

ferroui_media_camera_windows/
lib.rs

1//! Media Foundation camera acquisition; metadata-only discovery, no microphone.
2#![cfg(windows)]
3#![forbid(unsafe_code)]
4use ferroui_media::{frames::*, MediaError, MediaErrorKind};
5use ferroui_media_camera::*;
6use ferroui_media_opengl::NativeGlFrameProducer;
7#[cfg(feature = "controls")]
8pub use ferroui_media_windows::NativeGlTextureImporter;
9use ferroui_media_windows::{
10    poll_native, NativeRecording, NativeSession, WindowsFrameProducer, WindowsFrameStream,
11};
12use std::{
13    cell::{Cell, RefCell},
14    rc::Rc,
15    sync::{
16        atomic::{AtomicBool, Ordering},
17        Arc,
18    },
19    task::Poll,
20    time::Duration,
21};
22pub struct WindowsCameraProvider<P = NativeGlFrameProducer> {
23    pub frames: P,
24    permission: Rc<Cell<CameraPermission>>,
25    #[cfg(feature = "synthetic-camera")]
26    synthetic: bool,
27}
28impl<P: Clone + Into<WindowsFrameProducer> + 'static> WindowsCameraProvider<P> {
29    pub fn new(frames: P) -> Self {
30        Self {
31            frames,
32            permission: Rc::new(Cell::new(CameraPermission::NotDetermined)),
33            #[cfg(feature = "synthetic-camera")]
34            synthetic: false,
35        }
36    }
37    /// Explicit in-process MF/D3D11 test source. It is not an OS device or a
38    /// fallback, and its permission state does not describe Windows privacy.
39    #[cfg(feature = "synthetic-camera")]
40    pub fn synthetic(frames: P) -> Self {
41        Self {
42            synthetic: true,
43            ..Self::new(frames)
44        }
45    }
46    fn discover(&self) -> CameraFuture<Vec<CameraDevice>> {
47        #[cfg(feature = "synthetic-camera")]
48        if self.synthetic {
49            return Box::pin(async {
50                Ok([
51                    "ferroui:synthetic-camera",
52                    "ferroui:synthetic-camera/disconnect",
53                    "ferroui:synthetic-camera/recovery",
54                ]
55                .into_iter()
56                .map(|id| CameraDevice {
57                    id: id.into(),
58                    label: Some(format!("Synthetic MF GPU camera (in process): {id}")),
59                    facing: CameraFacing::Unknown,
60                    formats: vec![CameraFormat {
61                        width: 320,
62                        height: 240,
63                        frames_per_second: 30000. / 1001.,
64                    }],
65                })
66                .collect())
67            });
68        }
69        Box::pin(async { discovery().await })
70    }
71}
72fn error(kind: CameraErrorKind, s: impl Into<String>) -> CameraError {
73    CameraError::new(kind, s)
74}
75fn native(e: MediaError) -> CameraError {
76    error(
77        match e.kind {
78            MediaErrorKind::Permission => CameraErrorKind::Denied,
79            MediaErrorKind::Unsupported => CameraErrorKind::Unsupported,
80            MediaErrorKind::Cancelled => CameraErrorKind::Cancelled,
81            MediaErrorKind::DeviceLost => CameraErrorKind::DeviceLost,
82            _ => CameraErrorKind::NativeFailure,
83        },
84        e.message,
85    )
86}
87fn discovery() -> CameraFuture<Vec<CameraDevice>> {
88    let (send, receive) = std::sync::mpsc::sync_channel(1);
89    std::thread::spawn(move || {
90        let result = NativeSession::devices().and_then(|text| {
91            let values: serde_json::Value = serde_json::from_str(&text)
92                .map_err(|e| MediaError::new(MediaErrorKind::NativeFailure, e.to_string()))?;
93            Ok(values
94                .as_array()
95                .ok_or_else(|| {
96                    MediaError::new(
97                        MediaErrorKind::NativeFailure,
98                        "Invalid native discovery response",
99                    )
100                })?
101                .iter()
102                .map(|v| CameraDevice {
103                    id: v["id"].as_str().unwrap_or_default().into(),
104                    label: v["label"].as_str().map(str::to_owned),
105                    facing: CameraFacing::Unknown,
106                    formats: Vec::new(),
107                })
108                .collect())
109        });
110        let _ = send.send(result);
111    });
112    Box::pin(async move {
113        poll_native(move || match receive.try_recv() {
114            Ok(v) => Poll::Ready(v),
115            Err(std::sync::mpsc::TryRecvError::Empty) => Poll::Pending,
116            Err(_) => Poll::Ready(Err(MediaError::new(
117                MediaErrorKind::NativeFailure,
118                "Camera discovery worker failed",
119            ))),
120        })
121        .await
122        .map_err(native)
123    })
124}
125impl<P: Clone + Into<WindowsFrameProducer> + 'static> CameraProvider for WindowsCameraProvider<P> {
126    fn devices(&self) -> CameraFuture<Vec<CameraDevice>> {
127        self.discover()
128    }
129    fn permission(&self) -> CameraPermission {
130        self.permission.get()
131    }
132    fn start(
133        &self,
134        options: CameraStartOptions,
135        source: u64,
136        generation: u64,
137        budget: FrameBudget,
138    ) -> CameraFuture<Rc<dyn NativeCameraSession>> {
139        let frames: WindowsFrameProducer = self.frames.clone().into();
140        let frames = frames.with_budget(budget.clone());
141        let permission = self.permission.clone();
142        let devices = self.discover();
143        Box::pin(async move {
144            if options.selection.exact
145                && options
146                    .selection
147                    .preferred_facing
148                    .is_some_and(|f| f != CameraFacing::Unknown)
149            {
150                return Err(error(
151                    CameraErrorKind::Unsupported,
152                    "This native camera profile has no facing metadata",
153                ));
154            }
155            let devices = devices.await?;
156            let device = devices
157                .into_iter()
158                .find(|d| {
159                    options
160                        .selection
161                        .device_id
162                        .as_ref()
163                        .is_none_or(|id| id == &d.id)
164                })
165                .ok_or_else(|| {
166                    error(CameraErrorKind::NoDevice, "Selected camera is unavailable")
167                })?;
168            let size = options
169                .selection
170                .format
171                .map(|f| {
172                    let n = (f.frames_per_second * 1000.).round();
173                    if !n.is_finite() || n < 1. || n > u32::MAX as f64 {
174                        return Err(error(CameraErrorKind::InvalidRequest, "Invalid frame rate"));
175                    }
176                    Ok((f.width, f.height, n as u32, 1000))
177                })
178                .transpose()?;
179            let d3d = frames.native_device().await.map_err(native)?;
180            let session =
181                NativeSession::open(&d3d, &device.id, true, false, size).map_err(native)?;
182            let mut prepared = Some(session);
183            let result = poll_native(move || match prepared.as_ref().unwrap().state() {
184                Err(e) => Poll::Ready(Err(e)),
185                Ok(s) if s.state == 2 && s.width > 0 && s.height > 0 => {
186                    Poll::Ready(Ok(prepared.take().unwrap()))
187                }
188                _ => Poll::Pending,
189            })
190            .await;
191            let session = match result {
192                Ok(v) => v,
193                Err(e) => {
194                    if e.kind == MediaErrorKind::Permission {
195                        permission.set(CameraPermission::Denied);
196                    }
197                    return Err(native(e));
198                }
199            };
200            let s = session.state().map_err(native)?;
201            if s.fps_n == 0 || s.fps_d == 0 {
202                return Err(error(
203                    CameraErrorKind::Unsupported,
204                    "Native camera did not report negotiated frame rate",
205                ));
206            }
207            permission.set(CameraPermission::Granted);
208            let settings = CameraSettings {
209                device,
210                format: CameraFormat {
211                    width: s.width,
212                    height: s.height,
213                    frames_per_second: s.fps_n as f64 / s.fps_d as f64,
214                },
215                native_still: false,
216                torch: false,
217                zoom: None,
218            };
219            Ok(Rc::new(Camera {
220                stream: RefCell::new(Some(WindowsFrameStream::new(
221                    session, frames, source, generation,
222                ))),
223                settings,
224                budget,
225            }) as Rc<dyn NativeCameraSession>)
226        })
227    }
228}
229struct Camera {
230    stream: RefCell<Option<WindowsFrameStream>>,
231    settings: CameraSettings,
232    budget: FrameBudget,
233}
234static PHOTO_ACTIVE: AtomicBool = AtomicBool::new(false);
235#[cfg(feature = "synthetic-camera")]
236static PHOTO_JOBS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
237/// Diagnostic for checking that a canceled fixture photo reached native work.
238#[cfg(feature = "synthetic-camera")]
239pub fn synthetic_photo_jobs_started() -> u64 {
240    PHOTO_JOBS.load(Ordering::Acquire)
241}
242struct PhotoSlot;
243impl Drop for PhotoSlot {
244    fn drop(&mut self) {
245        PHOTO_ACTIVE.store(false, Ordering::Release);
246    }
247}
248impl NativeCameraSession for Camera {
249    fn supports_video_recording(&self) -> bool {
250        self.stream
251            .borrow()
252            .as_ref()
253            .is_some_and(|s| s.native.supports_recording())
254    }
255
256    fn start_recording(&self) -> CameraFuture<Rc<dyn NativeCameraRecording>> {
257        let recording = self
258            .stream
259            .borrow()
260            .as_ref()
261            .ok_or_else(|| error(CameraErrorKind::Cancelled, "Camera stopped"))
262            .and_then(|s| s.native.record().map_err(native));
263        Box::pin(async move {
264            let recording = Rc::new(recording?);
265            let poll = recording.clone();
266            poll_native(move || poll.started()).await.map_err(native)?;
267            Ok(Rc::new(WindowsCameraRecording(recording)) as Rc<dyn NativeCameraRecording>)
268        })
269    }
270
271    fn settings(&self) -> CameraSettings {
272        let mut settings = self.settings.clone();
273        if let Some(s) = self
274            .stream
275            .borrow()
276            .as_ref()
277            .and_then(|s| s.native.state().ok())
278            .filter(|s| s.width > 0 && s.height > 0 && s.fps_d > 0 && s.fps_n > 0)
279        {
280            settings.format = CameraFormat {
281                width: s.width,
282                height: s.height,
283                frames_per_second: s.fps_n as f64 / s.fps_d as f64,
284            };
285        }
286        settings
287    }
288    fn take_frame(&self) -> Result<Option<FrameLease>, CameraError> {
289        self.stream
290            .borrow()
291            .as_ref()
292            .ok_or_else(|| error(CameraErrorKind::Cancelled, "Camera stopped"))?
293            .take_frame()
294            .map_err(native)
295    }
296    fn photo(&self) -> CameraFuture<CameraPhoto> {
297        let frame = self
298            .stream
299            .borrow()
300            .as_ref()
301            .ok_or_else(|| error(CameraErrorKind::Cancelled, "Camera stopped"))
302            .and_then(|s| {
303                s.native.take_frame().map_err(native)?.ok_or_else(|| {
304                    error(
305                        CameraErrorKind::Busy,
306                        "No new native preview sample for snapshot",
307                    )
308                })
309            });
310        let counters = self.budget.counters();
311        let prepared = frame.and_then(|frame| {
312            PHOTO_ACTIVE
313                .compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
314                .map_err(|_| error(CameraErrorKind::Busy, "One native photo may be pending"))?;
315            Ok((frame, PhotoSlot))
316        });
317        let (send, receive) = std::sync::mpsc::sync_channel(1);
318        match prepared {
319            Err(e) => return Box::pin(async move { Err(e) }),
320            Ok((frame, slot)) => {
321                #[cfg(feature = "synthetic-camera")]
322                PHOTO_JOBS.fetch_add(1, Ordering::Release);
323                std::thread::spawn(move || {
324                    let _slot = slot;
325                    let info = frame.info();
326                    let result = (|| {
327                        let rgb = frame.photo_rgb(&counters).map_err(native)?;
328                        let mut encoded = Vec::new();
329                        jpeg_encoder::Encoder::new(&mut encoded, 90)
330                            .encode(
331                                &rgb,
332                                info.width as u16,
333                                info.height as u16,
334                                jpeg_encoder::ColorType::Rgb,
335                            )
336                            .map_err(|e| error(CameraErrorKind::NativeFailure, e.to_string()))?;
337                        if encoded.len() > 64 * 1024 * 1024 {
338                            return Err(error(
339                                CameraErrorKind::Unsupported,
340                                "Encoded photo exceeds 64 MiB",
341                            ));
342                        }
343                        counters
344                            .cpu_transfer(
345                                PixelTransferPurpose::Photo,
346                                encoded.len() as u64,
347                                PresentationRoute::SharedTexture,
348                            )
349                            .map_err(|e| error(CameraErrorKind::NativeFailure, e.to_string()))?;
350                        Ok(CameraPhoto {
351                            encoded: Arc::from(encoded),
352                            content_type: "image/jpeg".into(),
353                            width: info.width,
354                            height: info.height,
355                            timestamp: Duration::from_nanos(info.timestamp_ns),
356                            rotation_degrees: info.rotation as u16,
357                            mirrored: false,
358                            origin: PhotoOrigin::PreviewSnapshot,
359                        })
360                    })();
361                    let _ = send.send(result);
362                });
363            }
364        }
365        Box::pin(async move {
366            poll_native(move || match receive.try_recv() {
367                Ok(v) => Poll::Ready(Ok(v)),
368                Err(std::sync::mpsc::TryRecvError::Empty) => Poll::Pending,
369                Err(_) => Poll::Ready(Err(MediaError::new(
370                    MediaErrorKind::NativeFailure,
371                    "Native photo worker failed",
372                ))),
373            })
374            .await
375            .map_err(native)?
376        })
377    }
378    fn set_control(&self, _: CameraControl) -> Result<(), CameraError> {
379        Err(error(
380            CameraErrorKind::Unsupported,
381            "Camera controls are unavailable in this capture profile",
382        ))
383    }
384    fn stop(&self) {
385        self.stream.borrow_mut().take();
386    }
387}
388impl Drop for Camera {
389    fn drop(&mut self) {
390        self.stream.get_mut().take();
391    }
392}
393
394struct WindowsCameraRecording(Rc<NativeRecording>);
395impl NativeCameraRecording for WindowsCameraRecording {
396    fn finish(&self) -> CameraFuture<CameraVideo> {
397        self.0.finish();
398        let recording = self.0.clone();
399        Box::pin(async move {
400            let output = poll_native(move || recording.encoded())
401                .await
402                .map_err(native)?;
403            Ok(CameraVideo {
404                encoded: output.bytes,
405                content_type: "video/mp4".into(),
406                width: output.width,
407                height: output.height,
408                duration: output.duration,
409            })
410        })
411    }
412
413    fn cancel(&self) {
414        self.0.cancel();
415    }
416}