-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathcore.rs
More file actions
784 lines (663 loc) · 20.2 KB
/
core.rs
File metadata and controls
784 lines (663 loc) · 20.2 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
use crate::sources::audio_mixer::AudioMixer;
use anyhow::{Context, anyhow};
use cap_media_info::{AudioInfo, VideoInfo};
use cap_timestamp::{Timestamp, Timestamps};
use futures::{
FutureExt, SinkExt, StreamExt, TryFutureExt,
channel::{mpsc, oneshot},
future::{BoxFuture, Shared},
lock::Mutex,
stream::FuturesUnordered,
};
use std::{
any::Any,
future,
marker::PhantomData,
path::PathBuf,
sync::{
Arc,
atomic::{self, AtomicBool},
},
time::Duration,
};
use tokio::task::JoinHandle;
use tokio_util::sync::{CancellationToken, DropGuard};
use tracing::*;
pub struct OnceSender<T>(Option<oneshot::Sender<T>>);
impl<T> OnceSender<T> {
pub fn send(&mut self, v: T) {
if let Some(tx) = self.0.take() {
let _ = tx.send(v);
}
}
}
impl OutputPipeline {
pub fn builder(path: PathBuf) -> OutputPipelineBuilder<NoVideo> {
OutputPipelineBuilder::<NoVideo> {
path,
video: NoVideo,
audio_sources: vec![],
timestamps: Timestamps::now(),
}
}
}
pub struct SetupCtx {
tasks: TaskPool,
}
impl SetupCtx {
pub fn tasks(&mut self) -> &mut TaskPool {
&mut self.tasks
}
}
type AudioSourceSetupFn = Box<
dyn FnOnce(
mpsc::Sender<AudioFrame>,
&mut SetupCtx,
) -> BoxFuture<'static, anyhow::Result<ErasedAudioSource>>
+ Send,
>;
pub struct OutputPipelineBuilder<TVideo> {
path: PathBuf,
video: TVideo,
audio_sources: Vec<AudioSourceSetupFn>,
timestamps: Timestamps,
}
pub struct NoVideo;
pub struct HasVideo<TVideo: VideoSource> {
config: TVideo::Config,
}
impl<THasVideo> OutputPipelineBuilder<THasVideo> {
pub fn with_audio_source<TAudio: AudioSource>(
mut self,
config: TAudio::Config,
) -> OutputPipelineBuilder<THasVideo> {
self.audio_sources.push(Box::new(move |tx, ctx| {
TAudio::setup(config, tx, ctx)
.map(|v| v.map(ErasedAudioSource::new))
.boxed()
}));
self
}
pub fn set_timestamps(&mut self, timestamps: Timestamps) {
self.timestamps = timestamps;
}
pub fn with_timestamps(mut self, timestamps: Timestamps) -> Self {
self.timestamps = timestamps;
self
}
}
impl OutputPipelineBuilder<NoVideo> {
pub fn with_video<TVideo: VideoSource>(
self,
config: TVideo::Config,
) -> OutputPipelineBuilder<HasVideo<TVideo>> {
OutputPipelineBuilder::<HasVideo<TVideo>> {
video: HasVideo { config },
path: self.path,
audio_sources: self.audio_sources,
timestamps: self.timestamps,
}
}
}
pub struct TaskPool(Vec<(&'static str, JoinHandle<anyhow::Result<()>>)>);
impl TaskPool {
pub fn spawn<F>(&mut self, name: &'static str, future: F)
where
F: Future<Output = anyhow::Result<()>> + Send + 'static,
{
self.0.push((
name,
tokio::spawn(
async {
trace!("Task started");
let res = future.await;
match &res {
Ok(_) => info!("Task finished successfully"),
Err(err) => error!("Task failed: {:#}", err),
}
res
}
.instrument(error_span!("", task = name))
.in_current_span(),
),
));
}
pub fn spawn_thread(&mut self, name: &'static str, cb: impl FnOnce() + Send + 'static) {
let span = error_span!("", task = name);
let (done_tx, done_rx) = oneshot::channel();
std::thread::spawn(move || {
let _guard = span.enter();
trace!("Task started");
cb();
let _ = done_tx.send(());
info!("Task finished");
});
self.0.push((
name,
tokio::spawn(done_rx.map_err(|_| anyhow!("Cancelled"))),
));
}
}
impl<TVideo: VideoSource> OutputPipelineBuilder<HasVideo<TVideo>> {
pub async fn build<TMuxer: VideoMuxer<VideoFrame = TVideo::Frame> + AudioMuxer>(
self,
muxer_config: TMuxer::Config,
) -> anyhow::Result<OutputPipeline> {
let Self {
video,
audio_sources,
timestamps,
path,
..
} = self;
let (mut setup_ctx, stop_token, done_tx, done_rx, pause_flag) = setup_build();
let (video_source, video_rx) =
setup_video_source::<TVideo>(video.config, &mut setup_ctx).await?;
let video_info = video_source.video_info();
let (first_tx, first_rx) = oneshot::channel();
let muxer = setup_muxer::<TMuxer>(
muxer_config,
&path,
Some(video_info),
Some(AudioMixer::INFO),
&pause_flag,
&mut setup_ctx,
)
.await?;
spawn_video_encoder(
&mut setup_ctx,
video_source,
video_rx,
first_tx,
stop_token.clone(),
muxer.clone(),
timestamps,
);
finish_build(
setup_ctx,
audio_sources,
stop_token.clone(),
muxer,
timestamps,
done_tx,
None,
&path,
)
.await?;
Ok(OutputPipeline {
path,
first_timestamp_rx: first_rx,
stop_token: Some(stop_token.drop_guard()),
video_info: Some(video_info),
done_fut: done_rx,
pause_flag,
})
}
}
impl OutputPipelineBuilder<NoVideo> {
pub async fn build<TMuxer: AudioMuxer>(
self,
muxer_config: TMuxer::Config,
) -> anyhow::Result<OutputPipeline> {
let Self {
audio_sources,
timestamps,
path,
..
} = self;
if audio_sources.is_empty() {
return Err(anyhow!("Invariant: No audio sources"));
}
let (mut setup_ctx, stop_token, done_tx, done_rx, pause_flag) = setup_build();
let (first_tx, first_rx) = oneshot::channel();
let muxer = setup_muxer::<TMuxer>(
muxer_config,
&path,
None,
Some(AudioMixer::INFO),
&pause_flag,
&mut setup_ctx,
)
.await?;
finish_build(
setup_ctx,
audio_sources,
stop_token.clone(),
muxer,
timestamps,
done_tx,
Some(first_tx),
&path,
)
.await?;
Ok(OutputPipeline {
path,
first_timestamp_rx: first_rx,
stop_token: Some(stop_token.drop_guard()),
video_info: None,
done_fut: done_rx,
pause_flag,
})
}
}
fn setup_build() -> (
SetupCtx,
CancellationToken,
oneshot::Sender<anyhow::Result<()>>,
DoneFut,
Arc<AtomicBool>,
) {
let stop_token = CancellationToken::new();
let (done_tx, done_rx) = oneshot::channel();
(
SetupCtx {
tasks: TaskPool(vec![]),
},
stop_token,
done_tx,
done_rx
.map(|v| {
v.map_err(|s| anyhow::Error::from(s))
.and_then(|v| v)
.map_err(|e| PipelineDoneError(Arc::new(e)))
})
.boxed()
.shared(),
Arc::new(AtomicBool::new(false)),
)
}
async fn finish_build(
mut setup_ctx: SetupCtx,
audio_sources: Vec<AudioSourceSetupFn>,
stop_token: CancellationToken,
muxer: Arc<Mutex<impl Muxer + AudioMuxer>>,
timestamps: Timestamps,
done_tx: oneshot::Sender<anyhow::Result<()>>,
first_tx: Option<oneshot::Sender<Timestamp>>,
path: &PathBuf,
) -> anyhow::Result<()> {
configure_audio(
&mut setup_ctx,
audio_sources,
stop_token.clone(),
muxer.clone(),
timestamps,
first_tx,
)
.await
.context("audio mixer setup")?;
tokio::spawn(
async move {
let (task_names, task_handles): (Vec<_>, Vec<_>) =
setup_ctx.tasks.0.into_iter().unzip();
let mut futures = FuturesUnordered::from_iter(
task_handles
.into_iter()
.zip(task_names)
.map(|(f, n)| f.map(move |r| (r, n))),
);
while let Some((result, name)) = futures.next().await {
match result {
Err(_) => {
return Err(anyhow::anyhow!("Task {name} failed unexpectedly"));
}
Ok(Err(e)) => {
return Err(anyhow::anyhow!("Task {name} failed: {e}"));
}
_ => {}
}
}
Ok(())
}
.then(async move |res| {
let muxer_res = muxer.lock().await.finish(timestamps.instant().elapsed());
let _ = done_tx.send(match (res, muxer_res) {
(Err(e), _) | (_, Err(e)) => Err(e),
_ => Ok(()),
});
}),
);
info!("Built pipeline for output {}", path.display());
Ok(())
}
async fn setup_video_source<TVideo: VideoSource>(
video_config: TVideo::Config,
setup_ctx: &mut SetupCtx,
) -> anyhow::Result<(TVideo, mpsc::Receiver<TVideo::Frame>)> {
let (video_tx, video_rx) = mpsc::channel(8);
let video_source = TVideo::setup(video_config, video_tx, setup_ctx).await?;
Ok((video_source, video_rx))
}
async fn setup_muxer<TMuxer: Muxer>(
muxer_config: TMuxer::Config,
path: &PathBuf,
video_info: Option<VideoInfo>,
audio_info: Option<AudioInfo>,
pause_flag: &Arc<AtomicBool>,
setup_ctx: &mut SetupCtx,
) -> Result<Arc<Mutex<TMuxer>>, anyhow::Error> {
let muxer = Arc::new(Mutex::new(
TMuxer::setup(
muxer_config,
path.clone(),
video_info,
audio_info,
pause_flag.clone(),
&mut setup_ctx.tasks,
)
.await?,
));
Ok(muxer)
}
fn spawn_video_encoder<TMutex: VideoMuxer<VideoFrame = TVideo::Frame>, TVideo: VideoSource>(
setup_ctx: &mut SetupCtx,
mut video_source: TVideo,
mut video_rx: mpsc::Receiver<TVideo::Frame>,
first_tx: oneshot::Sender<Timestamp>,
stop_token: CancellationToken,
muxer: Arc<Mutex<TMutex>>,
timestamps: Timestamps,
) {
setup_ctx.tasks().spawn("mux-video", async move {
use futures::StreamExt;
let mut first_tx = Some(first_tx);
video_source.start().await?;
stop_token
.run_until_cancelled(async {
while let Some(frame) = video_rx.next().await {
let timestamp = frame.timestamp();
if let Some(first_tx) = first_tx.take() {
let _ = first_tx.send(timestamp);
}
muxer
.lock()
.await
.send_video_frame(frame, timestamp.duration_since(timestamps))
.map_err(|e| anyhow!("Error queueing video frame: {e}"))?;
}
Ok::<(), anyhow::Error>(())
})
.await;
video_source.stop().await.context("video_source_stop")?;
muxer.lock().await.stop();
Ok(())
});
}
async fn configure_audio<TMutex: AudioMuxer>(
setup_ctx: &mut SetupCtx,
audio_sources: Vec<AudioSourceSetupFn>,
stop_token: CancellationToken,
muxer: Arc<Mutex<TMutex>>,
timestamps: Timestamps,
mut first_tx: Option<oneshot::Sender<Timestamp>>,
) -> anyhow::Result<()> {
if audio_sources.len() < 1 {
return Ok(());
}
let mut audio_mixer = AudioMixer::builder();
let mut erased_audio_sources = vec![];
for audio_source_setup in audio_sources {
let (tx, rx) = mpsc::channel(64);
let source = (audio_source_setup)(tx, setup_ctx).await?;
audio_mixer.add_source(source.audio_info, rx);
erased_audio_sources.push(source);
}
let (audio_tx, mut audio_rx) = mpsc::channel(64);
let (ready_tx, ready_rx) = oneshot::channel::<anyhow::Result<()>>();
let stop_flag = Arc::new(AtomicBool::new(false));
setup_ctx.tasks().spawn_thread("audio-mixer", {
let stop_flag = stop_flag.clone();
move || audio_mixer.run(audio_tx, ready_tx, stop_flag)
});
let _ = ready_rx
.await
.map_err(|_| anyhow::format_err!("Audio mixer crashed"))??;
setup_ctx.tasks().spawn(
"audio-mixer-stop",
stop_token.child_token().cancelled_owned().map(move |_| {
stop_flag.store(true, atomic::Ordering::Relaxed);
Ok(())
}),
);
for source in &mut erased_audio_sources {
(source.start_fn)(source.inner.as_mut()).await?;
}
setup_ctx.tasks().spawn("mux-audio", {
let stop_token = stop_token.child_token();
let muxer = muxer.clone();
async move {
stop_token
.run_until_cancelled(async {
while let Some(frame) = audio_rx.next().await {
if let Some(first_tx) = first_tx.take() {
let _ = first_tx.send(frame.timestamp);
}
let timestamp = frame.timestamp.duration_since(timestamps);
if let Err(e) = muxer.lock().await.send_audio_frame(frame, timestamp) {
error!("Audio encoder: {e}");
}
}
})
.await;
for source in &mut erased_audio_sources {
let _ = (source.stop_fn)(source.inner.as_mut()).await;
}
muxer.lock().await.stop();
Ok(())
}
});
Ok(())
}
pub type DoneFut = Shared<BoxFuture<'static, Result<(), PipelineDoneError>>>;
pub struct OutputPipeline {
path: PathBuf,
pub first_timestamp_rx: oneshot::Receiver<Timestamp>,
stop_token: Option<DropGuard>,
video_info: Option<VideoInfo>,
done_fut: DoneFut,
pause_flag: Arc<AtomicBool>,
}
pub struct FinishedOutputPipeline {
pub path: PathBuf,
pub first_timestamp: Timestamp,
pub video_info: Option<VideoInfo>,
}
#[derive(Clone, Debug)]
pub struct PipelineDoneError(Arc<anyhow::Error>);
impl std::fmt::Display for PipelineDoneError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::error::Error for PipelineDoneError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.0.as_ref().source()
}
}
impl OutputPipeline {
pub fn path(&self) -> &PathBuf {
&self.path
}
pub async fn stop(mut self) -> anyhow::Result<FinishedOutputPipeline> {
drop(self.stop_token.take());
self.done_fut.await?;
Ok(FinishedOutputPipeline {
path: self.path,
first_timestamp: self.first_timestamp_rx.await?,
video_info: self.video_info,
})
}
pub fn pause(&self) {
self.pause_flag.store(true, atomic::Ordering::Relaxed);
}
pub fn resume(&self) {
self.pause_flag.store(false, atomic::Ordering::Relaxed);
}
pub fn video_info(&self) -> Option<VideoInfo> {
self.video_info
}
pub fn done_fut(&self) -> DoneFut {
self.done_fut.clone()
}
}
pub struct ChannelVideoSourceConfig<TVideoFrame> {
info: VideoInfo,
rx: flume::Receiver<TVideoFrame>,
}
impl<TVideoFrame> ChannelVideoSourceConfig<TVideoFrame> {
pub fn new(info: VideoInfo, rx: flume::Receiver<TVideoFrame>) -> Self {
Self { info, rx }
}
}
pub struct ChannelVideoSource<TVideoFrame>(VideoInfo, PhantomData<TVideoFrame>);
impl<TVideoFrame: VideoFrame> VideoSource for ChannelVideoSource<TVideoFrame> {
type Config = ChannelVideoSourceConfig<TVideoFrame>;
type Frame = TVideoFrame;
async fn setup(
config: Self::Config,
mut video_tx: mpsc::Sender<Self::Frame>,
_: &mut SetupCtx,
) -> anyhow::Result<Self>
where
Self: Sized,
{
tokio::spawn(async move {
while let Ok(frame) = config.rx.recv_async().await {
let _ = video_tx.send(frame).await;
}
});
Ok(Self(config.info, PhantomData))
}
fn video_info(&self) -> VideoInfo {
self.0
}
}
pub struct ChannelAudioSource {
info: AudioInfo,
}
pub struct ChannelAudioSourceConfig {
info: AudioInfo,
rx: mpsc::Receiver<AudioFrame>,
}
impl ChannelAudioSourceConfig {
pub fn new(info: AudioInfo, rx: mpsc::Receiver<AudioFrame>) -> Self {
Self { info, rx }
}
}
impl AudioSource for ChannelAudioSource {
type Config = ChannelAudioSourceConfig;
fn setup(
mut config: Self::Config,
mut tx: mpsc::Sender<AudioFrame>,
_: &mut SetupCtx,
) -> impl Future<Output = anyhow::Result<Self>> + 'static {
tokio::spawn(async move {
while let Some(frame) = config.rx.next().await {
let _ = tx.send(frame).await;
}
});
async move { Ok(ChannelAudioSource { info: config.info }) }
}
fn audio_info(&self) -> AudioInfo {
self.info
}
}
pub struct AudioFrame {
pub inner: ::ffmpeg::frame::Audio,
pub timestamp: Timestamp,
}
impl AudioFrame {
pub fn new(inner: ::ffmpeg::frame::Audio, timestamp: Timestamp) -> Self {
Self { inner, timestamp }
}
}
pub trait VideoSource: Send + 'static {
type Config;
type Frame: VideoFrame;
fn setup(
config: Self::Config,
video_tx: mpsc::Sender<Self::Frame>,
ctx: &mut SetupCtx,
) -> impl std::future::Future<Output = anyhow::Result<Self>> + Send
where
Self: Sized;
fn video_info(&self) -> VideoInfo;
fn start(&mut self) -> BoxFuture<'_, anyhow::Result<()>> {
future::ready(Ok(())).boxed()
}
fn stop(&mut self) -> BoxFuture<'_, anyhow::Result<()>> {
future::ready(Ok(())).boxed()
}
}
struct ErasedAudioSource {
inner: Box<dyn Any + Send>,
audio_info: AudioInfo,
start_fn: fn(&mut dyn Any) -> BoxFuture<'_, anyhow::Result<()>>,
stop_fn: fn(&mut dyn Any) -> BoxFuture<'_, anyhow::Result<()>>,
}
impl ErasedAudioSource {
pub fn new<TAudio: AudioSource>(source: TAudio) -> Self {
Self {
audio_info: source.audio_info(),
start_fn: |raw| {
raw.downcast_mut::<TAudio>()
.expect("Wrong type")
.start()
.boxed()
},
stop_fn: |raw| {
raw.downcast_mut::<TAudio>()
.expect("Wrong type")
.stop()
.boxed()
},
inner: Box::new(source),
}
}
}
pub trait AudioSource: Send + 'static {
type Config: Send;
fn setup(
config: Self::Config,
tx: mpsc::Sender<AudioFrame>,
ctx: &mut SetupCtx,
) -> impl Future<Output = anyhow::Result<Self>> + Send + 'static
where
Self: Sized;
fn audio_info(&self) -> AudioInfo;
fn start(&mut self) -> impl Future<Output = anyhow::Result<()>> + Send {
async { Ok(()) }
}
fn stop(&mut self) -> impl Future<Output = anyhow::Result<()>> + Send {
async { Ok(()) }
}
}
pub trait VideoFrame: Send + 'static {
fn timestamp(&self) -> Timestamp;
}
pub trait Muxer: Send + 'static {
type Config;
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>
where
Self: Sized;
fn stop(&mut self) {}
fn finish(&mut self, timestamp: Duration) -> anyhow::Result<()>;
}
pub trait AudioMuxer: Muxer {
fn send_audio_frame(&mut self, frame: AudioFrame, timestamp: Duration) -> anyhow::Result<()>;
}
pub trait VideoMuxer: Muxer {
type VideoFrame;
fn send_video_frame(
&mut self,
frame: Self::VideoFrame,
timestamp: Duration,
) -> anyhow::Result<()>;
}