Skip to content

Fixes: #146 #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
995 changes: 884 additions & 111 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ categories = ["graphics", "multimedia", "multimedia::video"]

[dependencies]
sysinfo = "0.30.0"
cpal = "0.15.3"
hound = "3.5.1"
rodio = "0.20.1"

[target.'cfg(target_os = "windows")'.dependencies]
windows-capture = "1.3.6"
Expand Down
90 changes: 90 additions & 0 deletions src/capturer/engine/mac/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,93 @@ pub fn process_sample_buffer(

None
}

pub fn record_system_audio(system_audio: bool, stop_flag: Arc<Mutex<bool>>) {
if !system_audio {
println!("System audio capture is disabled.");
return;
}

let config = screencapturekit::SCStreamConfiguration {
enable_audio: true,
enable_video: false,
};

let mut stream: screencapturekit::SCStreamRef = ptr::null_mut();
let result = unsafe { screencapturekit::SCStreamCreate(&config, &mut stream) };
if result != 0 || stream.is_null() {
eprintln!(
"Failed to create ScreenCaptureKit stream. Error code: {}",
result
);
eprintln!("Please ensure you are running on macOS 12+ and that your app has the necessary screen and audio permissions.");
return;
}

let spec = WavSpec {
channels: 2,
sample_rate: 44100,
bits_per_sample: 16,
sample_format: hound::SampleFormat::Int,
};

let file = match File::create("system_audio.wav") {
Ok(f) => f,
Err(e) => {
eprintln!(
"Failed to create 'system_audio.wav': {}. Please check file permissions.",
e
);
unsafe { screencapturekit::SCStreamRelease(stream) };
return;
}
};
let writer = Arc::new(Mutex::new(Some(
WavWriter::new(BufWriter::new(file), spec).unwrap(),
)));

let mut capture_state = CaptureState {
writer: Arc::clone(&writer),
};

let context_ptr: *mut c_void = &mut capture_state as *mut _ as *mut c_void;
let handler_result =
unsafe { screencapturekit::SCStreamSetOutputHandler(stream, output_handler, context_ptr) };
if handler_result != 0 {
eprintln!(
"Failed to set output handler. Error code: {}",
handler_result
);
unsafe { screencapturekit::SCStreamRelease(stream) };
return;
}

let start_result = unsafe { screencapturekit::SCStreamStartCapture(stream) };
if start_result != 0 {
eprintln!("Failed to start capture. Error code: {}", start_result);
unsafe { screencapturekit::SCStreamRelease(stream) };
return;
}
println!("🎤 Audio recording started using ScreenCaptureKit. Recording will continue until stop flag is set.");

loop {
if *stop_flag.lock().unwrap() {
break;
}
thread::sleep(Duration::from_millis(100));
}

let stop_result = unsafe { screencapturekit::SCStreamStopCapture(stream) };
if stop_result != 0 {
eprintln!("Failed to stop capture. Error code: {}", stop_result);
}
unsafe { screencapturekit::SCStreamRelease(stream) };

let mut writer_guard = writer.lock().unwrap();
if let Some(w) = writer_guard.take() {
if let Err(e) = w.finalize() {
eprintln!("Error finalizing WAV file: {}", e);
}
}
println!("✅ Audio recording saved as 'system_audio.wav'");
}
22 changes: 20 additions & 2 deletions src/capturer/engine/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
use std::sync::mpsc;

use super::Options;
use crate::frame::Frame;
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use cpal::{SampleFormat, StreamConfig};
use hound::{WavSpec, WavWriter};
use std::fs::File;
use std::io::BufWriter;
use std::sync::mpsc;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

#[cfg(target_os = "macos")]
pub mod mac;
Expand Down Expand Up @@ -121,6 +128,17 @@ impl Engine {
}
}

pub fn record_system_audio(system_audio: bool, stop_flag: Arc<Mutex<bool>>) {
#[cfg(target_os = "macos")]
{
mac::record_system_audio(system_audio, stop_flag);
}
#[cfg(target_os = "windows")]
{
win::WCStream::record_system_audio(system_audio, stop_flag);
}
}

pub fn get_output_frame_size(&mut self) -> [u32; 2] {
get_output_frame_size(&self.options)
}
Expand Down
73 changes: 71 additions & 2 deletions src/capturer/engine/win/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@ use crate::{
frame::{BGRAFrame, Frame, FrameType},
targets::{self, get_scale_factor, Target},
};
use std::cmp;
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use cpal::{SampleFormat, StreamConfig};
use hound::{WavSpec, WavWriter};
use std::fs::File;
use std::io::BufWriter;
use std::sync::mpsc;
use std::sync::Mutex;
use std::thread;
use std::time::Duration;
use std::time::{SystemTime, UNIX_EPOCH};
use std::{cmp, sync::Arc};
use windows_capture::capture::Context;
use windows_capture::{
capture::{CaptureControl, GraphicsCaptureApiHandler},
frame::Frame as WCFrame,
Expand All @@ -14,7 +23,6 @@ use windows_capture::{
settings::{ColorFormat, CursorCaptureSettings, DrawBorderSettings, Settings as WCSettings},
window::Window as WCWindow,
};
use windows_capture::capture::Context;

#[derive(Debug)]
struct Capturer {
Expand Down Expand Up @@ -124,6 +132,67 @@ impl WCStream {
self.capture_control = Some(cc)
}

pub fn record_system_audio(system_audio: bool, stop_flag: Arc<Mutex<bool>>) {
if !system_audio {
return;
}
let host = cpal::default_host();
let device = host
.default_output_device()
.expect("Failed to get default output device");

print!("Default output device: {}", device.name().unwrap());

let config: StreamConfig = device
.default_output_config()
.expect("Failed to get default output config")
.into();

let spec: WavSpec = WavSpec {
channels: config.channels as u16,
sample_rate: config.sample_rate.0,
bits_per_sample: 16,
sample_format: hound::SampleFormat::Int,
};

//for testing purpose

let file = std::fs::File::create("system_audio.wav").expect("Failed to create file");
let writer = Arc::new(Mutex::new(Some(
WavWriter::new(BufWriter::new(file), spec).unwrap(),
)));

let writer_clone = Arc::clone(&writer);
let stream = device
.build_input_stream(
&config,
move |data: &[f32], _: &cpal::InputCallbackInfo| {
let mut writer_guard = writer_clone.lock().unwrap();
if let Some(writer) = writer_guard.as_mut() {
for &sample in data {
let sample_i16 = (sample * i16::MAX as f32) as i16;
writer.write_sample(sample_i16).unwrap();
}
}
},
|err| eprintln!("Error: {:?}", err),
None,
)
.expect("Failed to create input stream");

stream.play().unwrap();
loop {
if *stop_flag.lock().unwrap() {
break;
}
thread::sleep(Duration::from_millis(100));
}

drop(stream);

println!("✅ Recording saved as 'recorded_audio.wav'");
}

pub fn stop_capture(&mut self) {
let capture_control = self.capture_control.take().unwrap();
let _ = capture_control.stop();
Expand Down
6 changes: 6 additions & 0 deletions src/capturer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod engine;

use std::sync::{Arc, Mutex};
use std::{error::Error, sync::mpsc};

use engine::ChannelItem;
Expand Down Expand Up @@ -63,6 +64,7 @@ pub struct Area {
pub struct Options {
pub fps: u32,
pub show_cursor: bool,
pub system_audio: bool,
pub show_highlight: bool,
pub target: Option<Target>,
pub crop_area: Option<Area>,
Expand Down Expand Up @@ -162,3 +164,7 @@ impl Capturer {
pub struct RawCapturer<'a> {
capturer: &'a Capturer,
}

pub fn record_system_audio(system_audio: bool, stop_flag: Arc<Mutex<bool>>) {
engine::Engine::record_system_audio(system_audio, stop_flag);
}
20 changes: 15 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
// Refer to `lib.rs` for the library source code

use scap::{
capturer::{Area, Capturer, Options, Point, Size},
frame::Frame
,
capturer::{self, Area, Capturer, Options, Point, Size},
frame::Frame,
};
use std::process;
use std::{
cmp,
sync::{Arc, Mutex},
thread,
};
use windows_capture::capture;

fn main() {
// Check if the platform is supported
Expand All @@ -32,6 +37,7 @@ fn main() {
let options = Options {
fps: 60,
show_cursor: true,
system_audio: true,
show_highlight: true,
excluded_targets: None,
output_type: scap::frame::FrameType::BGRAFrame,
Expand All @@ -54,9 +60,13 @@ fn main() {

// Start Capture
recorder.start_capture();

// Capture 100 frames
let mut start_time: u64 = 0;
let stop_flag = Arc::new(Mutex::new(false));
let stop_flag_clone = Arc::clone(&stop_flag);
let audio_thread = thread::spawn(move || {
capturer::record_system_audio(true, stop_flag_clone);
});

for i in 0..100 {
let frame = recorder.get_next_frame().expect("Error");

Expand Down
Binary file added system_audio.wav
Binary file not shown.