Open the camera device at `index` (embedded in the `device` string). 打开 `device` 字符串中 `index` 指定的摄像头设备。
(&mut self, device: &str, config: &CameraConfig)
| 119 | /// Open the camera device at `index` (embedded in the `device` string). |
| 120 | /// 打开 `device` 字符串中 `index` 指定的摄像头设备。 |
| 121 | pub fn open(&mut self, device: &str, config: &CameraConfig) -> Result<ResolvedConfig> { |
| 122 | // On macOS the "device path" is just the numeric index as a string. |
| 123 | // 在 macOS 上,"设备路径"只是数字索引的字符串形式。 |
| 124 | let index: c_uint = device.parse().unwrap_or(0); |
| 125 | let width = config.width.unwrap_or(640) as c_uint; |
| 126 | let height = config.height.unwrap_or(480) as c_uint; |
| 127 | let fps = config.fps.unwrap_or(30) as c_uint; |
| 128 | |
| 129 | let mut actual_w: c_uint = width; |
| 130 | let mut actual_h: c_uint = height; |
| 131 | let mut cam_ptr: *mut sys::AvfCameraOpaque = std::ptr::null_mut(); |
| 132 | |
| 133 | let ret = unsafe { |
| 134 | sys::avf_camera_open( |
| 135 | index, |
| 136 | width, |
| 137 | height, |
| 138 | fps, |
| 139 | &mut actual_w, |
| 140 | &mut actual_h, |
| 141 | &mut cam_ptr, |
| 142 | ) |
| 143 | }; |
| 144 | |
| 145 | match ret { |
| 146 | AVF_OK => {} |
| 147 | AVF_ERR_PERMISSION => { |
| 148 | return Err(CameraError::DeviceNotFound( |
| 149 | "camera access denied — allow camera in System Settings > Privacy".to_string(), |
| 150 | )); |
| 151 | } |
| 152 | AVF_ERR_DEVICE_NOT_FOUND => { |
| 153 | return Err(CameraError::DeviceNotFound(format!( |
| 154 | "no camera at index {}", |
| 155 | index |
| 156 | ))); |
| 157 | } |
| 158 | code => { |
| 159 | return Err(CameraError::DeviceNotFound(format!( |
| 160 | "AVFoundation session error (code {})", |
| 161 | code |
| 162 | ))); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | self.cam = cam_ptr; |
| 167 | |
| 168 | // Pre-allocate the frame buffer for the requested resolution (BGRA32). |
| 169 | // The bridge (bridge.m) guarantees frames are delivered at exactly |
| 170 | // (actual_w × actual_h) via vImage scaling, so no headroom is needed. |
| 171 | // 按请求分辨率预分配帧缓冲区(BGRA32)。 |
| 172 | // bridge 通过 vImage 缩放保证每帧都以 (actual_w × actual_h) 交付, |
| 173 | // 无需额外预留空间。 |
| 174 | self.frame_buf.resize((actual_w * actual_h * 4) as usize, 0); |
| 175 | |
| 176 | Ok(ResolvedConfig { |
| 177 | width: actual_w, |
| 178 | height: actual_h, |
nothing calls this directly
no outgoing calls
no test coverage detected