Created
April 14, 2024 19:31
-
-
Save thewh1teagle/d02415b9768fd816a780f9af6a3f2bdb to your computer and use it in GitHub Desktop.
Capture speakers output on macos with rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::sync::mpsc::{sync_channel, SyncSender}; | |
use screencapturekit::sc_stream::SCStream; | |
use screencapturekit::{ | |
cm_sample_buffer::CMSampleBuffer, | |
sc_content_filter::InitParams::Display, | |
sc_content_filter::SCContentFilter, | |
sc_error_handler::StreamErrorHandler, | |
sc_output_handler::{SCStreamOutputType, StreamOutput}, | |
sc_shareable_content::SCShareableContent, | |
sc_stream_configuration::SCStreamConfiguration, | |
}; | |
struct SomeErrorHandler {} | |
struct ScreenOutput { | |
pub video_tx: SyncSender<CMSampleBuffer>, | |
} | |
struct AudioOutput { | |
pub audio_tx: SyncSender<CMSampleBuffer>, | |
} | |
impl StreamErrorHandler for SomeErrorHandler { | |
fn on_error(&self) {} | |
} | |
impl StreamOutput for AudioOutput { | |
fn did_output_sample_buffer(&self, sample: CMSampleBuffer, of_type: SCStreamOutputType) { | |
match of_type { | |
SCStreamOutputType::Screen => {} | |
SCStreamOutputType::Audio => { | |
self.audio_tx.send(sample).ok(); | |
} | |
}; | |
} | |
} | |
impl StreamOutput for ScreenOutput { | |
fn did_output_sample_buffer(&self, sample: CMSampleBuffer, of_type: SCStreamOutputType) { | |
match of_type { | |
SCStreamOutputType::Screen => { | |
self.video_tx.send(sample).ok(); | |
} | |
SCStreamOutputType::Audio => {} | |
} | |
} | |
} | |
fn main() { | |
let mut content = SCShareableContent::current(); | |
let display = content.displays.pop().unwrap(); | |
let filter = SCContentFilter::new(Display(display)); | |
let config = SCStreamConfiguration { | |
width: 100, | |
height: 100, | |
captures_audio: true, | |
..Default::default() | |
}; | |
let (audio_tx, audio_rx) = sync_channel(1); | |
let mut stream = SCStream::new(filter, config, SomeErrorHandler {}); | |
let w = AudioOutput { audio_tx }; | |
stream.add_output(w, SCStreamOutputType::Audio); | |
stream.start_capture().ok(); | |
loop { | |
let output = audio_rx.recv().unwrap(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment