Skip to content

Commit

Permalink
fix: #751
Browse files Browse the repository at this point in the history
  • Loading branch information
louis030195 committed Nov 28, 2024
1 parent 50aa3d4 commit ce54a73
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
22 changes: 20 additions & 2 deletions screenpipe-audio/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@ use crate::AudioInput;
use anyhow::{anyhow, Result};
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use cpal::StreamError;
use lazy_static::lazy_static;
use log::{debug, error, info, warn};
use serde::{Deserialize, Serialize};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::mpsc;
use std::sync::Arc;
use std::time::Duration;
use std::{fmt, thread};
use tokio::sync::{broadcast, oneshot};
lazy_static! {
pub static ref LAST_AUDIO_CAPTURE: AtomicU64 = AtomicU64::new(
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs()
);
}

#[derive(Clone, Debug, PartialEq)]
pub enum AudioTranscriptionEngine {
Expand Down Expand Up @@ -217,7 +226,16 @@ async fn run_record_and_transcribe(

while start_time.elapsed() < duration && is_running.load(Ordering::Relaxed) {
match tokio::time::timeout(Duration::from_millis(100), receiver.recv()).await {
Ok(Ok(chunk)) => collected_audio.extend(chunk),
Ok(Ok(chunk)) => {
collected_audio.extend(chunk);
LAST_AUDIO_CAPTURE.store(
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs(),
Ordering::Relaxed,
);
}
Ok(Err(e)) => {
error!("error receiving audio data: {}", e);
return Err(anyhow!("Audio stream error: {}", e));
Expand Down
2 changes: 1 addition & 1 deletion screenpipe-audio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub mod whisper;
pub use core::{
default_input_device, default_output_device, get_device_and_config, list_audio_devices,
parse_audio_device, record_and_transcribe, trigger_audio_permission, AudioDevice, AudioStream,
AudioTranscriptionEngine, DeviceControl, DeviceType,
AudioTranscriptionEngine, DeviceControl, DeviceType, LAST_AUDIO_CAPTURE,
};
pub use encode::encode_single_audio;
pub use pcm_decode::pcm_decode;
Expand Down
39 changes: 20 additions & 19 deletions screenpipe-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ use std::{
convert::Infallible,
net::SocketAddr,
path::PathBuf,
sync::{atomic::AtomicBool, Arc},
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
time::Duration,
};

Expand All @@ -49,6 +52,8 @@ use tower_http::{cors::CorsLayer, trace::DefaultMakeSpan};
#[cfg(feature = "experimental")]
use enigo::{Enigo, Key, Settings};

use screenpipe_audio::LAST_AUDIO_CAPTURE;

pub struct AppState {
pub db: Arc<DatabaseManager>,
pub vision_control: Arc<AtomicBool>,
Expand Down Expand Up @@ -505,7 +510,15 @@ pub(crate) async fn remove_tags(
}

pub async fn health_check(State(state): State<Arc<AppState>>) -> JsonResponse<HealthCheckResponse> {
let (last_frame, last_audio, last_ui) = match state.db.get_latest_timestamps().await {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs();

let last_capture = LAST_AUDIO_CAPTURE.load(Ordering::Relaxed);
let audio_active = now - last_capture < 5; // Consider active if captured in last 5 seconds

let (last_frame, _, last_ui) = match state.db.get_latest_timestamps().await {
Ok((frame, audio, ui)) => (frame, audio, ui),
Err(e) => {
error!("failed to get latest timestamps: {}", e);
Expand All @@ -514,8 +527,7 @@ pub async fn health_check(State(state): State<Arc<AppState>>) -> JsonResponse<He
};

let now = Utc::now();
let threshold = Duration::from_secs(3600); // 1 hour
let app_start_threshold = Duration::from_secs(120); // 2 minutes - ideally should be audio duration chunk
let threshold = Duration::from_secs(3600); // 1 hour

let frame_status = if state.vision_disabled {
"disabled"
Expand All @@ -534,21 +546,10 @@ pub async fn health_check(State(state): State<Arc<AppState>>) -> JsonResponse<He

let audio_status = if state.audio_disabled {
"disabled"
} else if now.signed_duration_since(state.app_start_time)
< chrono::Duration::from_std(app_start_threshold).unwrap()
{
"ok" // Consider audio healthy if app started recently
} else if audio_active {
"ok"
} else {
match last_audio {
Some(timestamp)
if now.signed_duration_since(timestamp)
< chrono::Duration::from_std(threshold).unwrap() =>
{
"ok"
}
Some(_) => "stale",
None => "no data",
}
"stale"
};

let ui_status = if !state.ui_monitoring_enabled {
Expand Down Expand Up @@ -603,7 +604,7 @@ pub async fn health_check(State(state): State<Arc<AppState>>) -> JsonResponse<He
JsonResponse(HealthCheckResponse {
status: overall_status.to_string(),
last_frame_timestamp: last_frame,
last_audio_timestamp: last_audio,
last_audio_timestamp: None,
last_ui_timestamp: last_ui,
frame_status: frame_status.to_string(),
audio_status: audio_status.to_string(),
Expand Down

0 comments on commit ce54a73

Please sign in to comment.