tvctl 0.12.1

Play YouTube, Spotify, Netflix, Prime Video and more in windows on your screen and control them from the command line (built for AI agents)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Core Audio access for ducking (macOS): the processes that use audio, and
//! process taps that measure how loud a set of processes is.
//!
//! A tap copies the audio of some processes without changing where it plays.
//! Taps need macOS 14.2 and the "System Audio Recording" permission, which macOS
//! asks for (on behalf of the app that started tvctl) when the first tap starts.

use std::ffi::{CStr, c_void};
use std::path::{Path, PathBuf};
use std::ptr::NonNull;
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::sync::{Arc, Mutex, Once, OnceLock};

use anyhow::{Result, anyhow, bail};
use objc2::AnyThread;
use objc2::rc::Retained;
use objc2::runtime::{AnyClass, AnyObject};
use objc2_core_audio::{
    AudioDeviceCreateIOProcID, AudioDeviceDestroyIOProcID, AudioDeviceIOProcID, AudioDeviceStart,
    AudioDeviceStop, AudioHardwareCreateAggregateDevice, AudioHardwareDestroyAggregateDevice,
    AudioObjectAddPropertyListener, AudioObjectGetPropertyData, AudioObjectGetPropertyDataSize,
    AudioObjectID, AudioObjectPropertyAddress, CATapDescription, CATapMuteBehavior,
    kAudioHardwarePropertyProcessObjectList, kAudioObjectPropertyElementMain,
    kAudioObjectPropertyScopeGlobal, kAudioProcessPropertyBundleID,
    kAudioProcessPropertyIsRunningOutput, kAudioProcessPropertyPID,
};
use objc2_core_audio_types::{AudioBufferList, AudioTimeStamp};
use objc2_core_foundation::{CFDictionary, CFRetained, CFString};
use objc2_foundation::{NSArray, NSBundle, NSDictionary, NSNumber, NSString};

/// `kAudioObjectSystemObject`.
const SYSTEM_OBJECT: AudioObjectID = 1;

/// A process that is a Core Audio client.
#[derive(Debug, Clone)]
pub struct AudioProcess {
    /// Core Audio process object id (what taps refer to).
    pub object: AudioObjectID,
    pub pid: i32,
    pub bundle_id: Option<String>,
    /// The process is playing audio (or holds an output stream open) now.
    pub running_output: bool,
}

fn address(selector: u32) -> AudioObjectPropertyAddress {
    AudioObjectPropertyAddress {
        mSelector: selector,
        mScope: kAudioObjectPropertyScopeGlobal,
        mElement: kAudioObjectPropertyElementMain,
    }
}

/// Reads an array property.
fn property_vec<T: Copy + Default>(object: AudioObjectID, selector: u32) -> Option<Vec<T>> {
    let addr = address(selector);
    let mut size = 0u32;
    // SAFETY: valid pointers to locals; Core Audio writes at most `size` bytes.
    let status = unsafe {
        AudioObjectGetPropertyDataSize(
            object,
            NonNull::from(&addr),
            0,
            std::ptr::null(),
            NonNull::from(&mut size),
        )
    };
    if status != 0 {
        return None;
    }
    let mut items = vec![T::default(); size as usize / size_of::<T>()];
    if items.is_empty() {
        return Some(items);
    }
    let status = unsafe {
        AudioObjectGetPropertyData(
            object,
            NonNull::from(&addr),
            0,
            std::ptr::null(),
            NonNull::from(&mut size),
            NonNull::new(items.as_mut_ptr().cast::<c_void>()).expect("non-null"),
        )
    };
    if status != 0 {
        return None;
    }
    items.truncate(size as usize / size_of::<T>());
    Some(items)
}

/// Reads a fixed-size property.
fn property<T: Copy + Default>(object: AudioObjectID, selector: u32) -> Option<T> {
    let addr = address(selector);
    let mut size = size_of::<T>() as u32;
    let mut value = T::default();
    // SAFETY: `value` has room for `size` bytes.
    let status = unsafe {
        AudioObjectGetPropertyData(
            object,
            NonNull::from(&addr),
            0,
            std::ptr::null(),
            NonNull::from(&mut size),
            NonNull::from(&mut value).cast::<c_void>(),
        )
    };
    (status == 0 && size as usize == size_of::<T>()).then_some(value)
}

/// Reads a CFString property (returned retained by Core Audio).
fn string_property(object: AudioObjectID, selector: u32) -> Option<String> {
    let ptr = property::<usize>(object, selector)?;
    let ptr = NonNull::new(ptr as *mut CFString)?;
    // SAFETY: Core Audio returns a +1 CFStringRef; CFRetained releases it.
    let s = unsafe { CFRetained::from_raw(ptr) };
    Some(s.to_string()).filter(|s| !s.is_empty())
}

/// All processes that currently are Core Audio clients.
pub fn processes() -> Vec<AudioProcess> {
    let objects =
        property_vec::<AudioObjectID>(SYSTEM_OBJECT, kAudioHardwarePropertyProcessObjectList)
            .unwrap_or_default();
    objects
        .into_iter()
        .filter_map(|object| {
            Some(AudioProcess {
                object,
                pid: property::<i32>(object, kAudioProcessPropertyPID).filter(|p| *p > 0)?,
                bundle_id: string_property(object, kAudioProcessPropertyBundleID),
                running_output: running_output(object),
            })
        })
        .collect()
}

/// Whether a process object is playing audio (or holds an output stream open) now.
pub fn running_output(object: AudioObjectID) -> bool {
    property::<u32>(object, kAudioProcessPropertyIsRunningOutput).is_some_and(|v| v != 0)
}

static PROCESS_LIST_CHANGED: AtomicBool = AtomicBool::new(true);

unsafe extern "C-unwind" fn on_process_list_changed(
    _object: AudioObjectID,
    _count: u32,
    _addresses: NonNull<AudioObjectPropertyAddress>,
    _data: *mut c_void,
) -> i32 {
    PROCESS_LIST_CHANGED.store(true, Ordering::SeqCst);
    0
}

/// True once after the set of Core Audio client processes changed (always true
/// the first time).
pub fn take_process_list_changed() -> bool {
    static LISTEN: Once = Once::new();
    LISTEN.call_once(|| {
        let addr = address(kAudioHardwarePropertyProcessObjectList);
        // SAFETY: the listener is a static function without client data.
        unsafe {
            AudioObjectAddPropertyListener(
                SYSTEM_OBJECT,
                NonNull::from(&addr),
                Some(on_process_list_changed),
                std::ptr::null_mut(),
            );
        }
    });
    PROCESS_LIST_CHANGED.swap(false, Ordering::SeqCst)
}

/// What a tap listens to (Core Audio process object ids).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Target {
    /// Only these processes.
    Include(Vec<AudioObjectID>),
    /// Every process except these.
    Exclude(Vec<AudioObjectID>),
}

type CreateTapFn = unsafe extern "C-unwind" fn(*const AnyObject, *mut AudioObjectID) -> i32;
type DestroyTapFn = unsafe extern "C-unwind" fn(AudioObjectID) -> i32;

/// The tap functions, looked up at run time so tvctl still starts on macOS
/// versions without them (linking them directly would fail at launch).
fn tap_functions() -> Option<(CreateTapFn, DestroyTapFn)> {
    static FNS: OnceLock<Option<(CreateTapFn, DestroyTapFn)>> = OnceLock::new();
    *FNS.get_or_init(|| {
        let create = lookup(c"AudioHardwareCreateProcessTap")?;
        let destroy = lookup(c"AudioHardwareDestroyProcessTap")?;
        // SAFETY: the symbols have these C signatures (CoreAudio/AudioHardware.h).
        unsafe {
            Some((
                std::mem::transmute::<*mut c_void, CreateTapFn>(create),
                std::mem::transmute::<*mut c_void, DestroyTapFn>(destroy),
            ))
        }
    })
}

fn lookup(name: &CStr) -> Option<*mut c_void> {
    // SAFETY: dlsym with a valid C string.
    let ptr = unsafe { libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr()) };
    (!ptr.is_null()).then_some(ptr)
}

/// A running tap and the private aggregate device it is read through. Both
/// disappear when dropped (or when the process exits).
pub struct Tap {
    tap: AudioObjectID,
    device: AudioObjectID,
    io_proc: AudioDeviceIOProcID,
    peak: *const AtomicU32,
    destroy_tap: DestroyTapFn,
}

// SAFETY: the raw pointer is an `Arc<AtomicU32>` owned by this value.
unsafe impl Send for Tap {}

unsafe extern "C-unwind" fn measure_peak(
    _device: AudioObjectID,
    _now: NonNull<AudioTimeStamp>,
    input: NonNull<AudioBufferList>,
    _input_time: NonNull<AudioTimeStamp>,
    _output: NonNull<AudioBufferList>,
    _output_time: NonNull<AudioTimeStamp>,
    data: *mut c_void,
) -> i32 {
    // SAFETY: `data` is the tap's `AtomicU32`, alive until the IOProc is destroyed;
    // the buffer list is valid for this call and holds f32 samples.
    unsafe {
        let peak_slot = &*(data as *const AtomicU32);
        let list = input.as_ref();
        let buffers =
            std::slice::from_raw_parts(list.mBuffers.as_ptr(), list.mNumberBuffers as usize);
        let mut peak = 0f32;
        for b in buffers {
            if b.mData.is_null() {
                continue;
            }
            let samples = std::slice::from_raw_parts(
                b.mData as *const f32,
                b.mDataByteSize as usize / size_of::<f32>(),
            );
            for s in samples {
                peak = peak.max(s.abs());
            }
        }
        if peak > f32::from_bits(peak_slot.load(Ordering::Relaxed)) {
            peak_slot.store(peak.to_bits(), Ordering::Relaxed);
        }
    }
    0
}

fn any<T: objc2::Message>(object: Retained<T>) -> Retained<AnyObject> {
    // SAFETY: every Objective-C object is an AnyObject.
    unsafe { Retained::cast_unchecked(object) }
}

fn dictionary(
    entries: Vec<(&str, Retained<AnyObject>)>,
) -> Retained<NSDictionary<NSString, AnyObject>> {
    let keys: Vec<Retained<NSString>> =
        entries.iter().map(|(k, _)| NSString::from_str(k)).collect();
    let key_refs: Vec<&NSString> = keys.iter().map(|k| &**k).collect();
    let values: Vec<Retained<AnyObject>> = entries.into_iter().map(|(_, v)| v).collect();
    NSDictionary::from_retained_objects(&key_refs, &values)
}

impl Tap {
    /// Starts a mono tap of `target`. Blocks while macOS asks the user for the
    /// audio recording permission.
    pub fn start(target: &Target) -> Result<Tap> {
        let (create_tap, destroy_tap) = tap_functions()
            .ok_or_else(|| anyhow!("ducking needs macOS 14.2 or later (process taps)"))?;
        if AnyClass::get(c"CATapDescription").is_none() {
            bail!("ducking needs macOS 14.2 or later (process taps)");
        }
        let ids: Vec<Retained<NSNumber>> = match target {
            Target::Include(ids) | Target::Exclude(ids) => {
                ids.iter().map(|id| NSNumber::new_u32(*id)).collect()
            }
        };
        let ids = NSArray::from_retained_slice(&ids);
        // SAFETY: CATapDescription exists (checked above); arguments are valid.
        let description = unsafe {
            let d = match target {
                Target::Include(_) => {
                    CATapDescription::initMonoMixdownOfProcesses(CATapDescription::alloc(), &ids)
                }
                Target::Exclude(_) => CATapDescription::initMonoGlobalTapButExcludeProcesses(
                    CATapDescription::alloc(),
                    &ids,
                ),
            };
            d.setPrivate(true);
            d.setMuteBehavior(CATapMuteBehavior::Unmuted);
            d.setName(&NSString::from_str("tvctl duck"));
            d
        };
        let mut tap: AudioObjectID = 0;
        // SAFETY: valid description and out pointer.
        let status = unsafe { create_tap(Retained::as_ptr(&description).cast(), &mut tap) };
        if status != 0 {
            bail!("could not create the audio tap (OSStatus {status})");
        }
        // SAFETY: the description is a valid object.
        let tap_uid = unsafe { description.UUID().UUIDString() };
        let tap_entry = dictionary(vec![
            ("uid", any(tap_uid)),
            ("drift", any(NSNumber::new_bool(true))),
        ]);
        let uid = format!("tvctl-duck-{}-{tap}", std::process::id());
        let description = dictionary(vec![
            ("uid", any(NSString::from_str(&uid))),
            ("name", any(NSString::from_str("tvctl duck"))),
            ("private", any(NSNumber::new_bool(true))),
            ("taps", any(NSArray::from_retained_slice(&[tap_entry]))),
            ("tapautostart", any(NSNumber::new_bool(true))),
        ]);
        let mut device: AudioObjectID = 0;
        // SAFETY: NSDictionary is toll-free bridged to CFDictionary.
        let status = unsafe {
            let cf = &*(Retained::as_ptr(&description) as *const CFDictionary);
            AudioHardwareCreateAggregateDevice(cf, NonNull::from(&mut device))
        };
        if status != 0 {
            unsafe { destroy_tap(tap) };
            bail!("could not create the tap device (OSStatus {status})");
        }
        let peak = Arc::into_raw(Arc::new(AtomicU32::new(0)));
        let mut proc_id: AudioDeviceIOProcID = None;
        // SAFETY: `peak` stays valid until after the IOProc is destroyed in Drop.
        let mut status = unsafe {
            AudioDeviceCreateIOProcID(
                device,
                Some(measure_peak),
                peak as *mut c_void,
                NonNull::from(&mut proc_id),
            )
        };
        if status == 0 {
            status = unsafe { AudioDeviceStart(device, proc_id) };
        }
        let tap = Tap {
            tap,
            device,
            io_proc: proc_id,
            peak,
            destroy_tap,
        };
        if status != 0 {
            bail!("could not start the audio tap (OSStatus {status})");
        }
        Ok(tap)
    }

    /// The highest sample level (0..1) since the last call.
    pub fn take_peak(&self) -> f32 {
        // SAFETY: `peak` is alive while `self` is.
        f32::from_bits(unsafe { &*self.peak }.swap(0, Ordering::Relaxed))
    }
}

impl Drop for Tap {
    fn drop(&mut self) {
        // SAFETY: ids created in `start`; after the IOProc is destroyed Core Audio
        // no longer uses `peak`, so it can be freed.
        unsafe {
            if self.io_proc.is_some() {
                AudioDeviceStop(self.device, self.io_proc);
                AudioDeviceDestroyIOProcID(self.device, self.io_proc);
            }
            AudioHardwareDestroyAggregateDevice(self.device);
            (self.destroy_tap)(self.tap);
            drop(Arc::from_raw(self.peak));
        }
    }
}

/// Process name (executable name, up to 32 bytes).
pub fn process_name(pid: i32) -> Option<String> {
    let mut buf = [0u8; 256];
    // SAFETY: buffer of the given size.
    let n = unsafe { libc::proc_name(pid, buf.as_mut_ptr().cast(), buf.len() as u32) };
    (n > 0).then(|| String::from_utf8_lossy(&buf[..n as usize]).into_owned())
}

/// Executable path.
pub fn process_path(pid: i32) -> Option<PathBuf> {
    let mut buf = vec![0u8; libc::PROC_PIDPATHINFO_MAXSIZE as usize];
    // SAFETY: buffer of the given size.
    let n = unsafe { libc::proc_pidpath(pid, buf.as_mut_ptr().cast(), buf.len() as u32) };
    (n > 0).then(|| PathBuf::from(String::from_utf8_lossy(&buf[..n as usize]).into_owned()))
}

/// Parent process id.
pub fn parent_pid(pid: i32) -> Option<i32> {
    // SAFETY: zeroed POD struct of the size passed.
    let mut info: libc::proc_bsdinfo = unsafe { std::mem::zeroed() };
    let n = unsafe {
        libc::proc_pidinfo(
            pid,
            libc::PROC_PIDTBSDINFO,
            0,
            (&mut info as *mut libc::proc_bsdinfo).cast(),
            size_of::<libc::proc_bsdinfo>() as i32,
        )
    };
    (n as usize == size_of::<libc::proc_bsdinfo>()).then_some(info.pbi_ppid as i32)
}

/// The process macOS holds responsible for `pid` (the app that launched it, for
/// helpers and command line tools). Private API, looked up at run time.
pub fn responsible_pid(pid: i32) -> Option<i32> {
    type Fn = unsafe extern "C" fn(i32) -> i32;
    static FN: OnceLock<Option<Fn>> = OnceLock::new();
    let f = (*FN.get_or_init(|| {
        let ptr = lookup(c"responsibility_get_pid_responsible_for_pid")?;
        // SAFETY: `pid_t responsibility_get_pid_responsible_for_pid(pid_t)`.
        Some(unsafe { std::mem::transmute::<*mut c_void, Fn>(ptr) })
    }))?;
    // SAFETY: plain C call.
    let r = unsafe { f(pid) };
    (r > 0).then_some(r)
}

/// `CFBundleIdentifier` of the bundle at `path` (cached).
pub fn bundle_id(path: &Path) -> Option<String> {
    static CACHE: OnceLock<Mutex<std::collections::HashMap<PathBuf, Option<String>>>> =
        OnceLock::new();
    let cache = CACHE.get_or_init(Default::default);
    if let Some(v) = cache.lock().unwrap().get(path) {
        return v.clone();
    }
    let id = NSBundle::bundleWithPath(&NSString::from_str(&path.to_string_lossy()))
        .and_then(|b| b.bundleIdentifier())
        .map(|s| s.to_string());
    cache.lock().unwrap().insert(path.to_path_buf(), id.clone());
    id
}