playa_ffmpeg/device/mod.rs
1//! Hardware device input and output.
2//!
3//! This module provides access to hardware capture and playback devices.
4//! It wraps FFmpeg's `libavdevice` library.
5//!
6//! # Capabilities
7//!
8//! - Video capture (webcams, screen capture, video capture cards)
9//! - Audio capture (microphones, line-in)
10//! - Video/audio playback to hardware devices
11//! - Device enumeration and discovery
12//!
13//! # Submodules
14//!
15//! - [`input`] - Input devices (capture)
16//! - [`output`] - Output devices (playback)
17//! - [`extensions`] - Device-specific extensions
18//!
19//! # Platform Support
20//!
21//! Available devices vary by platform:
22//! - **Linux**: v4l2 (video), alsa (audio), xcbgrab (screen capture)
23//! - **Windows**: dshow (DirectShow), gdigrab (screen capture)
24//! - **macOS**: avfoundation (video/audio), screencapture
25
26pub mod extensions;
27pub mod input;
28pub mod output;
29
30use std::{ffi::CStr, marker::PhantomData, str::from_utf8_unchecked};
31
32use crate::ffi::*;
33
34/// Information about a hardware device.
35///
36/// Provides device name and human-readable description for enumerated devices.
37pub struct Info<'a> {
38 ptr: *mut AVDeviceInfo,
39
40 _marker: PhantomData<&'a ()>,
41}
42
43impl<'a> Info<'a> {
44 /// Wraps a raw FFmpeg device info pointer.
45 pub unsafe fn wrap(ptr: *mut AVDeviceInfo) -> Self {
46 Info { ptr, _marker: PhantomData }
47 }
48
49 /// Returns the raw pointer.
50 pub unsafe fn as_ptr(&self) -> *const AVDeviceInfo {
51 self.ptr as *const _
52 }
53
54 /// Returns the mutable raw pointer.
55 pub unsafe fn as_mut_ptr(&mut self) -> *mut AVDeviceInfo {
56 self.ptr
57 }
58}
59
60impl<'a> Info<'a> {
61 /// Returns the device name.
62 ///
63 /// This is typically the system identifier for the device (e.g., "/dev/video0", "video=0").
64 pub fn name(&self) -> &str {
65 unsafe { from_utf8_unchecked(CStr::from_ptr((*self.as_ptr()).device_name).to_bytes()) }
66 }
67
68 /// Returns a human-readable device description.
69 ///
70 /// This is a user-friendly name (e.g., "HD Webcam", "Built-in Microphone").
71 pub fn description(&self) -> &str {
72 unsafe { from_utf8_unchecked(CStr::from_ptr((*self.as_ptr()).device_description).to_bytes()) }
73 }
74}
75
76/// Registers all available devices.
77///
78/// Must be called before using device functionality. Called automatically by [`crate::init()`].
79pub fn register_all() {
80 unsafe {
81 avdevice_register_all();
82 }
83}
84
85/// Returns the libavdevice version number.
86pub fn version() -> u32 {
87 unsafe { avdevice_version() }
88}
89
90/// Returns the libavdevice build configuration string.
91pub fn configuration() -> &'static str {
92 unsafe { from_utf8_unchecked(CStr::from_ptr(avdevice_configuration()).to_bytes()) }
93}
94
95/// Returns the libavdevice license string.
96pub fn license() -> &'static str {
97 unsafe { from_utf8_unchecked(CStr::from_ptr(avdevice_license()).to_bytes()) }
98}