-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathmacos.rs
More file actions
90 lines (79 loc) · 2.39 KB
/
macos.rs
File metadata and controls
90 lines (79 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use crate::{
output_pipeline::{AudioFrame, AudioMuxer, Muxer, TaskPool, VideoMuxer},
sources::screen_capture,
};
use anyhow::anyhow;
use cap_media_info::{AudioInfo, VideoInfo};
use std::{
path::PathBuf,
sync::{Arc, Mutex, atomic::AtomicBool},
time::Duration,
};
#[derive(Clone)]
pub struct AVFoundationMp4Muxer(
Arc<Mutex<cap_enc_avfoundation::MP4Encoder>>,
Arc<AtomicBool>,
);
#[derive(Default)]
pub struct AVFoundationMp4MuxerConfig {
pub output_height: Option<u32>,
}
impl Muxer for AVFoundationMp4Muxer {
type Config = AVFoundationMp4MuxerConfig;
async fn setup(
config: Self::Config,
output_path: PathBuf,
video_config: Option<VideoInfo>,
audio_config: Option<AudioInfo>,
pause_flag: Arc<AtomicBool>,
_tasks: &mut TaskPool,
) -> anyhow::Result<Self> {
let video_config =
video_config.ok_or_else(|| anyhow!("Invariant: No video source provided"))?;
Ok(Self(
Arc::new(Mutex::new(
cap_enc_avfoundation::MP4Encoder::init(
output_path,
video_config,
audio_config,
config.output_height,
)
.map_err(|e| anyhow!("{e}"))?,
)),
pause_flag,
))
}
fn finish(&mut self, timestamp: Duration) -> anyhow::Result<()> {
self.0
.lock()
.map_err(|e| anyhow!("{e}"))?
.finish(Some(timestamp));
Ok(())
}
}
impl VideoMuxer for AVFoundationMp4Muxer {
type VideoFrame = screen_capture::VideoFrame;
fn send_video_frame(
&mut self,
frame: Self::VideoFrame,
timestamp: Duration,
) -> anyhow::Result<()> {
let mut mp4 = self.0.lock().map_err(|e| anyhow!("MuxerLock/{e}"))?;
if self.1.load(std::sync::atomic::Ordering::Relaxed) {
mp4.pause();
} else {
mp4.resume();
}
mp4.queue_video_frame(frame.sample_buf, timestamp)
.map_err(|e| anyhow!("QueueVideoFrame/{e}"))
}
}
impl AudioMuxer for AVFoundationMp4Muxer {
fn send_audio_frame(&mut self, frame: AudioFrame, timestamp: Duration) -> anyhow::Result<()> {
self.0
.lock()
.map_err(|e| anyhow!("{e}"))?
.queue_audio_frame(frame.inner, timestamp)
.map_err(|e| anyhow!("{e}"))
}
}