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

chore: small screenpipe-audio macos optimisation

Louis Beaumonte5559a2ee16d
diff --git a/screenpipe-audio/Cargo.toml b/screenpipe-audio/Cargo.toml
index da64ec14c..e7d79de0d 100644
--- a/screenpipe-audio/Cargo.toml
+++ b/screenpipe-audio/Cargo.toml
@@ -61,6 +61,9 @@ reqwest = { workspace = true }
screenpipe-core = { path = "../screenpipe-core" }
+[target.'cfg(target_os = "macos")'.dependencies]
+once_cell = "1.17.1"
+
[dev-dependencies]
tempfile = "3.3.0"
infer = "0.15"
diff --git a/screenpipe-audio/src/core.rs b/screenpipe-audio/src/core.rs
index 07c349705..fcae16c36 100644
--- a/screenpipe-audio/src/core.rs
+++ b/screenpipe-audio/src/core.rs
@@ -15,12 +15,15 @@ use tokio::process::Command;
use tokio::sync::mpsc::{self, UnboundedSender};
use crate::AudioInput;
-
-async fn get_macos_version() -> Option<f32> {
- let output = Command::new("sw_vers")
+#[cfg(target_os = "macos")]
+use once_cell::sync::Lazy;
+#[cfg(target_os = "macos")]
+static MACOS_VERSION: Lazy<f32> = Lazy::new(|| get_macos_version().unwrap_or(0.0));
+#[cfg(target_os = "macos")]
+fn get_macos_version() -> Option<f32> {
+ let output = std::process::Command::new("sw_vers")
.arg("-productVersion")
.output()
- .await
.ok()?;
let version = String::from_utf8(output.stdout).ok()?;
version.split('.').next()?.parse().ok()
@@ -142,17 +145,12 @@ async fn get_device_and_config(
#[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"));
+ let version = *MACOS_VERSION;
+ if version < 15.0 {
+ if let Ok(screen_capture_host) =
+ cpal::host_from_id(cpal::HostId::ScreenCaptureKit)
+ {
+ devices = screen_capture_host.input_devices()?;
}
}
}
@@ -410,17 +408,15 @@ pub async 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
- 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));
- }
+ let version = *MACOS_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));
}
}
}
- _ => {}
}
}
@@ -445,18 +441,16 @@ pub fn default_input_device() -> Result<AudioDevice> {
pub async fn default_output_device() -> Result<AudioDevice> {
#[cfg(target_os = "macos")]
{
+ let version = *MACOS_VERSION;
// ! see https://github.com/RustAudio/cpal/pull/894
- 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() {
- return Ok(AudioDevice::new(name, DeviceType::Output));
- }
+ 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() {
+ return Ok(AudioDevice::new(name, DeviceType::Output));
}
}
}
- _ => {}
}
let host = cpal::default_host();
let device = host