|
| 1 | +//! macOS system audio recording permission helpers. |
| 2 | +//! |
| 3 | +//! These functions check and request the "System Audio Recording" permission |
| 4 | +//! (`kTCCServiceAudioCapture`) via the private TCC framework — required for |
| 5 | +//! loopback recording via [`default_output_device`](super::enumerate::default_output_device). |
| 6 | +
|
| 7 | +use block2::StackBlock; |
| 8 | +use libloading::{Library, Symbol}; |
| 9 | +use objc2_core_foundation::{CFRetained, CFString}; |
| 10 | +use std::ffi::c_void; |
| 11 | + |
| 12 | +const TCC_FRAMEWORK: &str = "/System/Library/PrivateFrameworks/TCC.framework/Versions/A/TCC"; |
| 13 | +const TCC_SERVICE: &str = "kTCCServiceAudioCapture"; |
| 14 | + |
| 15 | +fn load_tcc() -> Option<Library> { |
| 16 | + unsafe { Library::new(TCC_FRAMEWORK) }.ok() |
| 17 | +} |
| 18 | + |
| 19 | +fn tcc_service() -> CFRetained<CFString> { |
| 20 | + CFString::from_str(TCC_SERVICE) |
| 21 | +} |
| 22 | + |
| 23 | +/// Silently check whether system audio recording permission is granted. |
| 24 | +/// Returns `true` if already granted, `false` otherwise. No UI is shown. |
| 25 | +pub fn check_system_audio_permission() -> bool { |
| 26 | + let Some(lib) = load_tcc() else { return false }; |
| 27 | + unsafe { |
| 28 | + let Ok(preflight): Result< |
| 29 | + Symbol<unsafe extern "C" fn(*const c_void, *const c_void) -> u32>, |
| 30 | + _, |
| 31 | + > = lib.get(b"TCCAccessPreflight\0") else { |
| 32 | + return false; |
| 33 | + }; |
| 34 | + let service = tcc_service(); |
| 35 | + preflight(&*service as *const _ as *const c_void, std::ptr::null()) == 0 |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/// Request system audio recording permission, showing the system prompt if needed. |
| 40 | +/// |
| 41 | +/// **Blocking** — does not return until the user responds. |
| 42 | +/// Returns `false` immediately (without showing UI) if previously denied — |
| 43 | +/// call [`open_system_audio_settings`] in that case. |
| 44 | +pub fn request_system_audio_permission() -> bool { |
| 45 | + let Some(lib) = load_tcc() else { return false }; |
| 46 | + unsafe { |
| 47 | + let Ok(request_fn): Result< |
| 48 | + Symbol<unsafe extern "C" fn(*const c_void, *const c_void, *const c_void)>, |
| 49 | + _, |
| 50 | + > = lib.get(b"TCCAccessRequest\0") else { |
| 51 | + return false; |
| 52 | + }; |
| 53 | + |
| 54 | + let (tx, rx) = std::sync::mpsc::sync_channel::<bool>(1); |
| 55 | + // Store as usize (Copy) so TCC's internal block memcpy doesn't double-drop the sender. |
| 56 | + let tx_ptr = Box::into_raw(Box::new(tx)) as usize; |
| 57 | + |
| 58 | + let completion = StackBlock::new(move |granted: u8| { |
| 59 | + let tx = Box::from_raw(tx_ptr as *mut std::sync::mpsc::SyncSender<bool>); |
| 60 | + tx.send(granted != 0).ok(); |
| 61 | + }); |
| 62 | + |
| 63 | + let service = tcc_service(); |
| 64 | + request_fn( |
| 65 | + &*service as *const _ as *const c_void, |
| 66 | + std::ptr::null(), |
| 67 | + &completion as *const _ as *const c_void, |
| 68 | + ); |
| 69 | + |
| 70 | + rx.recv().unwrap_or(false) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/// Open Privacy & Security > System Audio Recording in System Settings. |
| 75 | +/// |
| 76 | +/// Call this when [`request_system_audio_permission`] returns `false` (previously denied). |
| 77 | +pub fn open_system_audio_settings() { |
| 78 | + std::process::Command::new("open") |
| 79 | + .arg("x-apple.systempreferences:com.apple.settings.PrivacySecurity.extension?Privacy_AudioCapture") |
| 80 | + .spawn() |
| 81 | + .ok(); |
| 82 | +} |
0 commit comments