Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

feat: macos output for non beta version of macos

Louis Beaumont316a94f26d9c
diff --git a/screenpipe-audio/src/bin/screenpipe-audio.rs b/screenpipe-audio/src/bin/screenpipe-audio.rs
index fb822509f..09e087ca7 100644
--- a/screenpipe-audio/src/bin/screenpipe-audio.rs
+++ b/screenpipe-audio/src/bin/screenpipe-audio.rs
@@ -30,9 +30,12 @@ struct Args {
fn print_devices(devices: &[AudioDevice]) {
println!("Available audio devices:");
- for (i, device) in devices.iter().enumerate() {
- println!(" {}. {}", i + 1, device);
+ for (_, device) in devices.iter().enumerate() {
+ println!(" {}", device);
}
+
+ #[cfg(target_os = "macos")]
+ println!("On macOS, it's not intuitive but output devices are your displays");
}
// TODO - kinda bad cli here
@@ -49,7 +52,7 @@ async fn main() -> Result<()> {
let args = Args::parse();
- let devices = list_audio_devices()?;
+ let devices = list_audio_devices().await?;
if args.list_audio_devices {
print_devices(&devices);
@@ -57,7 +60,7 @@ async fn main() -> Result<()> {
}
let devices = if args.audio_device.is_empty() {
- vec![default_input_device()?, default_output_device()?]
+ vec![default_input_device()?, default_output_device().await?]
} else {
args.audio_device
.iter()
diff --git a/screenpipe-audio/src/core.rs b/screenpipe-audio/src/core.rs
index 59cde8c50..8a30a6ca1 100644
--- a/screenpipe-audio/src/core.rs
+++ b/screenpipe-audio/src/core.rs
@@ -16,6 +16,16 @@ use tokio::sync::mpsc::{self, UnboundedSender};
use crate::AudioInput;
+async fn get_macos_version() -> Option<f32> {
+ let output = Command::new("sw_vers")
+ .arg("-productVersion")
+ .output()
+ .await
+ .ok()?;
+ let version = String::from_utf8(output.stdout).ok()?;
+ version.trim().parse().ok()
+}
+
#[derive(Clone, Debug, PartialEq)]
pub enum AudioTranscriptionEngine {
#[allow(dead_code)]
@@ -95,12 +105,12 @@ pub fn parse_audio_device(name: &str) -> Result<AudioDevice> {
AudioDevice::from_name(name)
}
-fn get_device_and_config(
+async fn get_device_and_config(
audio_device: &AudioDevice,
) -> Result<(cpal::Device, cpal::SupportedStreamConfig)> {
let host = match audio_device.device_type {
#[cfg(target_os = "macos")]
- DeviceType::Output => cpal::default_host(),
+ DeviceType::Output => cpal::host_from_id(cpal::HostId::ScreenCaptureKit)?,
_ => cpal::default_host(),
};
@@ -117,6 +127,25 @@ fn get_device_and_config(
DeviceType::Output => host.output_devices()?,
};
+ #[cfg(target_os = "macos")]
+ {
+ if audio_device.device_type == DeviceType::Output {
+ match get_macos_version().await {
+ Some(version) if version < 15.0 => {
+ if let Ok(screen_capture_host) =
+ cpal::host_from_id(cpal::HostId::ScreenCaptureKit)
+ {
+ devices = screen_capture_host.input_devices()?;
+ }
+ }
+ _ => {
+ warn!("Audio output not supported on macOS 15.0 and above");
+ return Err(anyhow!("Audio output not supported on this macOS version"));
+ }
+ }
+ }
+ }
+
devices.find(|x| {
x.name()
.map(|y| {
@@ -224,7 +253,7 @@ pub async fn record_and_transcribe(
whisper_sender: UnboundedSender<AudioInput>,
is_running: Arc<AtomicBool>,
) -> Result<PathBuf> {
- let (cpal_audio_device, config) = get_device_and_config(&audio_device)?;
+ let (cpal_audio_device, config) = get_device_and_config(&audio_device).await?;
let sample_rate = config.sample_rate().0;
let channels = config.channels() as u16;
debug!(
@@ -315,7 +344,7 @@ pub async fn record_and_transcribe(
Ok(output_path_clone_2.to_path_buf())
}
-pub fn list_audio_devices() -> Result<Vec<AudioDevice>> {
+pub async fn list_audio_devices() -> Result<Vec<AudioDevice>> {
let host = cpal::default_host();
let mut devices = Vec::new();
@@ -329,11 +358,18 @@ pub fn list_audio_devices() -> Result<Vec<AudioDevice>> {
{
// !HACK macos is suppoed to use special macos feature "display capture"
// ! see https://github.com/RustAudio/cpal/pull/894
- let host = cpal::default_host();
- // let host = cpal::host_from_id(cpal::HostId::ScreenCaptureKit)?;
- for device in host.output_devices()? {
- if let Ok(name) = device.name() {
- devices.push(AudioDevice::new(name, DeviceType::Output));
+ match get_macos_version().await {
+ Some(version) if version < 15.0 => {
+ if let Ok(host) = cpal::host_from_id(cpal::HostId::ScreenCaptureKit) {
+ for device in host.input_devices()? {
+ if let Ok(name) = device.name() {
+ devices.push(AudioDevice::new(name, DeviceType::Output));
+ }
+ }
+ }
+ }
+ _ => {
+ warn!("Audio output disabled for macOS");
}
}
}
@@ -356,18 +392,24 @@ pub fn default_input_device() -> Result<AudioDevice> {
Ok(AudioDevice::new(device.name()?, DeviceType::Input))
}
-pub fn default_output_device() -> Result<AudioDevice> {
+pub async fn default_output_device() -> Result<AudioDevice> {
#[cfg(target_os = "macos")]
{
- // !HACK macos is suppoed to use special macos feature "display capture"
// ! see https://github.com/RustAudio/cpal/pull/894
- let host = cpal::default_host();
- // let host = cpal::host_from_id(cpal::HostId::ScreenCaptureKit)?;
- let device = host
- .default_output_device()
- .ok_or_else(|| anyhow!("No default input device found"))?;
- info!("Using display capture device: {}", device.name()?);
- return Ok(AudioDevice::new(device.name()?, DeviceType::Output));
+ match get_macos_version().await {
+ Some(version) if version < 15.0 => {
+ if let Ok(host) = cpal::host_from_id(cpal::HostId::ScreenCaptureKit) {
+ if let Some(device) = host.default_input_device() {
+ if let Ok(name) = device.name() {
+ info!("Using display capture device: {}", name);
+ return Ok(AudioDevice::new(name, DeviceType::Output));
+ }
+ }
+ }
+ }
+ _ => {}
+ }
+ Err(anyhow!("Audio output disabled for macOS"))
}
#[cfg(not(target_os = "macos"))]
diff --git a/screenpipe-server/src/bin/screenpipe-server.rs b/screenpipe-server/src/bin/screenpipe-server.rs
index ade6555ed..d1df82c59 100644
--- a/screenpipe-server/src/bin/screenpipe-server.rs
+++ b/screenpipe-server/src/bin/screenpipe-server.rs
@@ -15,7 +15,7 @@ use crossbeam::queue::SegQueue;
use dirs::home_dir;
use log::{debug, error, info, LevelFilter};
use screenpipe_audio::{
- default_input_device, list_audio_devices, parse_audio_device, DeviceControl,
+ default_input_device, list_audio_devices, parse_audio_device, AudioDevice, DeviceControl
};
use screenpipe_vision::{monitor::{get_monitor_by_id, list_monitors}, OcrEngine};
use std::io::Write;
@@ -69,7 +69,15 @@ impl From<CliOcrEngine> for CoreOcrEngine {
}
}
+fn print_devices(devices: &[AudioDevice]) {
+ println!("Available audio devices:");
+ for (_, device) in devices.iter().enumerate() {
+ println!(" {}", device);
+ }
+ #[cfg(target_os = "macos")]
+ println!("On macOS, it's not intuitive but output devices are your displays");
+}
// keep in mind this is the most important feature ever // TODO: add a pipe and a ⭐️ e.g screen | ⭐️ somehow in ascii ♥️🤓
const DISPLAY: &str = r"
@@ -244,13 +252,10 @@ async fn main() -> anyhow::Result<()> {
.underline()
);
}
- let all_audio_devices = list_audio_devices()?;
+ let all_audio_devices = list_audio_devices().await?;
let mut devices_status = HashMap::new();
if cli.list_audio_devices {
- println!("Available audio devices:");
- for (i, device) in all_audio_devices.iter().enumerate() {
- println!(" {}. {}", i + 1, device);
- }
+ print_devices(&all_audio_devices);
return Ok(());
}
let all_monitors = list_monitors().await;
@@ -292,7 +297,7 @@ async fn main() -> anyhow::Result<()> {
// see https://github.com/louis030195/screen-pipe/pull/106
if cfg!(target_os = "linux") {
use screenpipe_audio::default_output_device;
- if let Ok(output_device) = default_output_device() {
+ if let Ok(output_device) = default_output_device().await {
audio_devices.push(Arc::new(output_device.clone()));
let device_control = DeviceControl {
is_running: true,