Skip to content

Commit db89c17

Browse files
committed
wip
1 parent 73c7a9c commit db89c17

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

crates/detect/src/mic/macos.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,54 @@
1+
use cidre::{core_audio as ca, io, os};
2+
13
#[derive(Default)]
24
pub struct Detector {}
35

6+
const DEVICE_IS_RUNNING_SOMEWHERE: ca::PropAddr = ca::PropAddr {
7+
selector: ca::PropSelector::DEVICE_IS_RUNNING_SOMEWHERE,
8+
scope: ca::PropScope::GLOBAL,
9+
element: ca::PropElement::MAIN,
10+
};
11+
412
impl crate::Observer for Detector {
5-
fn start(&mut self, f: crate::DetectCallback) {}
13+
fn start(&mut self, f: crate::DetectCallback) {
14+
extern "C-unwind" fn listener(
15+
_obj_id: ca::Obj,
16+
number_addresses: u32,
17+
addresses: *const ca::PropAddr,
18+
_client_data: *mut (),
19+
) -> os::Status {
20+
let addresses =
21+
unsafe { std::slice::from_raw_parts(addresses, number_addresses as usize) };
22+
23+
for addr in addresses {
24+
match addr.selector {
25+
ca::PropSelector::HW_DEFAULT_INPUT_DEVICE => {
26+
println!("default input device changed");
27+
let device = ca::System::default_input_device().unwrap();
28+
29+
let is_running = device.prop::<u8>(&DEVICE_IS_RUNNING_SOMEWHERE).unwrap();
30+
println!("is_running: {is_running}");
31+
}
32+
ca::PropSelector::HW_DEFAULT_OUTPUT_DEVICE => {
33+
println!("default output device changed");
34+
let device = ca::System::default_output_device().unwrap();
35+
36+
let streams = device.streams().unwrap();
37+
let headphones = streams
38+
.iter()
39+
.find(|s| {
40+
let term_type = s.terminal_type().unwrap();
41+
term_type.0 == io::audio::output_term::HEADPHONES
42+
|| term_type == ca::StreamTerminalType::HEADPHONES
43+
})
44+
.is_some();
45+
println!("headphones connected {headphones}");
46+
}
47+
_ => panic!("unregistered selector"),
48+
}
49+
}
50+
os::Status::NO_ERR
51+
}
52+
}
653
fn stop(&mut self) {}
754
}

0 commit comments

Comments
 (0)