-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy patheditor_instance.rs
More file actions
714 lines (634 loc) · 26.7 KB
/
editor_instance.rs
File metadata and controls
714 lines (634 loc) · 26.7 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
use crate::editor;
use crate::playback::{self, PlaybackHandle, PlaybackStartError};
use cap_audio::AudioData;
use cap_project::StudioRecordingMeta;
use cap_project::{
CursorEvents, ProjectConfiguration, RecordingMeta, RecordingMetaInner, TimelineConfiguration,
TimelineSegment, XY,
};
use cap_rendering::{
ProjectRecordingsMeta, ProjectUniforms, RecordingSegmentDecoders, RenderVideoConstants,
SegmentVideoPaths, Video, ZoomFocusInterpolator, get_duration,
spring_mass_damper::SpringMassDamperSimulationConfig,
};
use std::{
path::{Path, PathBuf},
sync::{
Arc,
atomic::{AtomicBool, Ordering},
},
};
use tokio::sync::{Mutex, watch};
use tokio_util::sync::CancellationToken;
use tracing::warn;
fn get_video_duration_fallback(path: &Path) -> Option<f64> {
tracing::debug!("get_video_duration_fallback called for: {:?}", path);
let input = match ffmpeg::format::input(path) {
Ok(i) => i,
Err(e) => {
tracing::warn!("get_video_duration_fallback: failed to open input: {}", e);
return None;
}
};
let container_duration = input.duration();
tracing::debug!(
"get_video_duration_fallback: container_duration (raw i64) = {}",
container_duration
);
if container_duration > 0 {
let secs = container_duration as f64 / 1_000_000.0;
tracing::debug!(
"get_video_duration_fallback: returning container duration {} seconds",
secs
);
return Some(secs);
}
let stream = input.streams().best(ffmpeg::media::Type::Video)?;
let stream_duration = stream.duration();
let time_base = stream.time_base();
tracing::debug!(
"get_video_duration_fallback: stream_duration = {}, time_base = {}/{}",
stream_duration,
time_base.numerator(),
time_base.denominator()
);
if stream_duration > 0 && time_base.denominator() > 0 {
let secs =
stream_duration as f64 * time_base.numerator() as f64 / time_base.denominator() as f64;
tracing::debug!(
"get_video_duration_fallback: returning stream duration {} seconds",
secs
);
Some(secs)
} else {
tracing::warn!("get_video_duration_fallback: no valid duration found");
None
}
}
pub struct EditorInstance {
pub project_path: PathBuf,
pub recordings: Arc<ProjectRecordingsMeta>,
pub renderer: Arc<editor::RendererHandle>,
pub render_constants: Arc<RenderVideoConstants>,
playback_active: watch::Sender<bool>,
playback_active_rx: watch::Receiver<bool>,
pub state: Arc<Mutex<EditorState>>,
on_state_change: Box<dyn Fn(&EditorState) + Send + Sync + 'static>,
pub preview_tx: watch::Sender<Option<PreviewFrameInstruction>>,
pub project_config: (
watch::Sender<ProjectConfiguration>,
watch::Receiver<ProjectConfiguration>,
),
// ws_shutdown_token: CancellationToken,
pub segment_medias: Arc<Vec<SegmentMedia>>,
meta: RecordingMeta,
pub export_preview_active: AtomicBool,
pub export_active: AtomicBool,
}
impl EditorInstance {
pub async fn new(
project_path: PathBuf,
on_state_change: impl Fn(&EditorState) + Send + Sync + 'static,
frame_cb: Box<dyn FnMut(editor::EditorFrameOutput) + Send>,
) -> Result<Arc<Self>, String> {
if !project_path.exists() {
return Err(format!("Video path {} not found!", project_path.display()));
}
let recording_meta = cap_project::RecordingMeta::load_for_project(&project_path)
.map_err(|e| format!("Failed to load recording meta: {e}"))?;
let RecordingMetaInner::Studio(meta) = &recording_meta.inner else {
return Err("Cannot edit non-studio recordings".to_string());
};
let segment_count = match meta.as_ref() {
StudioRecordingMeta::SingleSegment { .. } => 1,
StudioRecordingMeta::MultipleSegments { inner } => inner.segments.len(),
};
if segment_count == 0 {
return Err(
"Recording has no segments. It may need to be recovered first.".to_string(),
);
}
let mut project = recording_meta.project_config();
if project.timeline.is_none() {
warn!("Project config has no timeline, creating one from recording segments");
let timeline_segments = match meta.as_ref() {
StudioRecordingMeta::SingleSegment { segment } => {
let display_path = recording_meta.path(&segment.display.path);
let duration = match Video::new(&display_path, 0.0) {
Ok(v) => v.duration,
Err(e) => {
warn!(
"Failed to load video for duration calculation: {} (path: {}), trying fallback",
e,
display_path.display()
);
match get_video_duration_fallback(&display_path) {
Some(d) => d,
None => {
warn!("Fallback also failed, using default duration 5.0s");
5.0
}
}
}
};
vec![TimelineSegment {
recording_clip: 0,
start: 0.0,
end: duration,
timescale: 1.0,
}]
}
StudioRecordingMeta::MultipleSegments { inner } => inner
.segments
.iter()
.enumerate()
.filter_map(|(i, segment)| {
let display_path = recording_meta.path(&segment.display.path);
tracing::debug!("Attempting to get duration for segment {}: {:?}", i, display_path);
let duration = match Video::new(&display_path, 0.0) {
Ok(v) => {
tracing::debug!("Video::new succeeded, duration: {}", v.duration);
v.duration
}
Err(e) => {
warn!(
"Failed to load video for duration calculation: {} (path: {}), trying fallback",
e,
display_path.display()
);
match get_video_duration_fallback(&display_path) {
Some(d) => {
tracing::debug!("Fallback succeeded, duration: {}", d);
d
}
None => {
warn!("Fallback also failed, using default duration 5.0s");
5.0
}
}
}
};
tracing::debug!("Final duration for segment {}: {}", i, duration);
if duration <= 0.0 {
return None;
}
Some(TimelineSegment {
recording_clip: i as u32,
start: 0.0,
end: duration,
timescale: 1.0,
})
})
.collect(),
};
if !timeline_segments.is_empty() {
project.timeline = Some(TimelineConfiguration {
segments: timeline_segments,
zoom_segments: Vec::new(),
scene_segments: Vec::new(),
mask_segments: Vec::new(),
text_segments: Vec::new(),
});
if let Err(e) = project.write(&recording_meta.project_path) {
warn!("Failed to save auto-generated timeline: {}", e);
}
}
}
if project.clips.is_empty() {
let calibration_store = load_calibration_store(&recording_meta.project_path);
match meta.as_ref() {
StudioRecordingMeta::MultipleSegments { inner } => {
project.clips = inner
.segments
.iter()
.enumerate()
.map(|(i, segment)| {
let calibration_offset = get_calibration_offset(
segment.camera_device_id(),
segment.mic_device_id(),
&calibration_store,
);
cap_project::ClipConfiguration {
index: i as u32,
offsets: segment
.calculate_audio_offsets_with_calibration(calibration_offset),
}
})
.collect();
}
StudioRecordingMeta::SingleSegment { .. } => {
project.clips = vec![cap_project::ClipConfiguration {
index: 0,
offsets: cap_project::ClipOffsets::default(),
}];
}
}
if let Err(e) = project.write(&recording_meta.project_path) {
warn!("Failed to save auto-generated clip offsets: {}", e);
}
}
let recordings = Arc::new(ProjectRecordingsMeta::new(
&recording_meta.project_path,
meta.as_ref(),
)?);
let segments = create_segments(&recording_meta, meta.as_ref(), false).await?;
let render_constants = Arc::new(
RenderVideoConstants::new(
&recordings.segments,
recording_meta.clone(),
(**meta).clone(),
)
.await
.map_err(|e| format!("Failed to create render constants: {e}"))?,
);
let renderer = Arc::new(editor::Renderer::spawn(
render_constants.clone(),
frame_cb,
&recording_meta,
meta,
)?);
let (preview_tx, preview_rx) = watch::channel(None);
let (playback_active_tx, playback_active_rx) = watch::channel(false);
let this = Arc::new(Self {
project_path,
recordings,
renderer,
render_constants,
state: Arc::new(Mutex::new(EditorState {
playhead_position: 0,
playback_task: None,
preview_task: None,
})),
on_state_change: Box::new(on_state_change),
preview_tx,
project_config: watch::channel(project),
segment_medias: Arc::new(segments),
meta: recording_meta,
playback_active: playback_active_tx,
playback_active_rx,
export_preview_active: AtomicBool::new(false),
export_active: AtomicBool::new(false),
});
this.state.lock().await.preview_task =
Some(this.clone().spawn_preview_renderer(preview_rx));
Ok(this)
}
pub fn meta(&self) -> &RecordingMeta {
&self.meta
}
pub async fn dispose(&self) {
let mut state = self.state.lock().await;
if let Some(handle) = state.playback_task.take() {
handle.stop();
}
if let Some(task) = state.preview_task.take() {
task.abort();
if let Err(e) = task.await {
tracing::warn!("preview task abort await failed: {e}");
}
}
self.renderer.stop().await;
tokio::task::yield_now().await;
drop(state);
}
pub async fn modify_and_emit_state(&self, modify: impl Fn(&mut EditorState)) {
let mut state = self.state.lock().await;
modify(&mut state);
(self.on_state_change)(&state);
}
pub async fn start_playback(self: &Arc<Self>, fps: u32, resolution_base: XY<u32>) {
let (mut handle, prev) = {
let mut state = self.state.lock().await;
let start_frame_number = state.playhead_position;
let playback_handle = match (playback::Playback {
segment_medias: self.segment_medias.clone(),
renderer: self.renderer.clone(),
render_constants: self.render_constants.clone(),
start_frame_number,
project: self.project_config.0.subscribe(),
})
.start(fps, resolution_base)
.await
{
Ok(handle) => handle,
Err(PlaybackStartError::InvalidFps) => {
warn!(fps, "Skipping playback start due to invalid FPS");
return;
}
};
if let Err(e) = self.playback_active.send(true) {
tracing::warn!(%e, "failed to send playback_active=true");
}
let prev = state.playback_task.replace(playback_handle.clone());
(playback_handle, prev)
};
let this = self.clone();
tokio::spawn(async move {
loop {
let event = *handle.receive_event().await;
match event {
playback::PlaybackEvent::Start => {}
playback::PlaybackEvent::Frame(frame_number) => {
this.modify_and_emit_state(|state| {
state.playhead_position = frame_number;
})
.await;
}
playback::PlaybackEvent::Stop => {
if let Err(e) = this.playback_active.send(false) {
tracing::warn!(%e, "failed to send playback_active=false");
}
return;
}
}
}
});
if let Some(prev) = prev {
prev.stop();
}
}
fn spawn_preview_renderer(
self: Arc<Self>,
mut preview_rx: watch::Receiver<Option<(u32, u32, XY<u32>)>>,
) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
let mut prefetch_cancel_token: Option<CancellationToken> = None;
loop {
preview_rx.changed().await.unwrap();
loop {
let Some((frame_number, fps, resolution_base)) =
*preview_rx.borrow_and_update()
else {
break;
};
if let Some(token) = prefetch_cancel_token.take() {
token.cancel();
}
if *self.playback_active_rx.borrow() {
break;
}
if self.export_active.load(Ordering::Acquire) {
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
break;
}
let project = self.project_config.1.borrow().clone();
let Some((segment_time, segment)) =
project.get_segment_time(frame_number as f64 / fps as f64)
else {
warn!(
"Preview renderer: no segment found for frame {}",
frame_number
);
break;
};
let segment_medias = &self.segment_medias[segment.recording_clip as usize];
let clip_config = project
.clips
.iter()
.find(|v| v.index == segment.recording_clip);
let clip_offsets = clip_config.map(|v| v.offsets).unwrap_or_default();
let new_cancel_token = CancellationToken::new();
prefetch_cancel_token = Some(new_cancel_token.clone());
let playback_is_active = *self.playback_active_rx.borrow();
let export_preview_is_active =
self.export_preview_active.load(Ordering::Acquire);
let export_is_active = self.export_active.load(Ordering::Acquire);
if !playback_is_active && !export_preview_is_active && !export_is_active {
let prefetch_frames_count = 15u32;
let hide_camera = project.camera.hide;
let playback_rx = self.playback_active_rx.clone();
for offset in 1..=prefetch_frames_count {
let prefetch_frame = frame_number + offset;
if let Some((prefetch_segment_time, prefetch_segment)) =
project.get_segment_time(prefetch_frame as f64 / fps as f64)
&& let Some(prefetch_segment_media) = self
.segment_medias
.get(prefetch_segment.recording_clip as usize)
{
let prefetch_clip_offsets = project
.clips
.iter()
.find(|v| v.index == prefetch_segment.recording_clip)
.map(|v| v.offsets)
.unwrap_or_default();
let decoders = prefetch_segment_media.decoders.clone();
let cancel_token = new_cancel_token.clone();
let playback_rx = playback_rx.clone();
tokio::spawn(async move {
if cancel_token.is_cancelled() || *playback_rx.borrow() {
return;
}
let _ = decoders
.get_frames(
prefetch_segment_time as f32,
!hide_camera,
prefetch_clip_offsets,
)
.await;
});
}
}
}
tokio::select! {
biased;
_ = preview_rx.changed() => {
continue;
}
segment_frames_opt = segment_medias.decoders.get_frames_initial(
segment_time as f32,
!project.camera.hide,
clip_offsets,
) => {
if preview_rx.has_changed().unwrap_or(false) {
continue;
}
if let Some(segment_frames) = segment_frames_opt {
let total_duration = project
.timeline
.as_ref()
.map(|t| t.duration())
.unwrap_or(0.0);
let cursor_smoothing = (!project.cursor.raw).then_some(
SpringMassDamperSimulationConfig {
tension: project.cursor.tension,
mass: project.cursor.mass,
friction: project.cursor.friction,
},
);
let zoom_focus_interpolator = ZoomFocusInterpolator::new_arc(
segment_medias.cursor.clone(),
cursor_smoothing,
project.screen_movement_spring,
total_duration,
);
let uniforms = ProjectUniforms::new(
&self.render_constants,
&project,
frame_number,
fps,
resolution_base,
&segment_medias.cursor,
&segment_frames,
total_duration,
&zoom_focus_interpolator,
);
self.renderer
.render_frame(segment_frames, uniforms, segment_medias.cursor.clone());
} else {
warn!("Preview renderer: no frames returned for frame {}", frame_number);
}
}
}
break;
}
}
})
}
fn get_studio_meta(&self) -> &StudioRecordingMeta {
match &self.meta.inner {
RecordingMetaInner::Studio(meta) => meta.as_ref(),
_ => panic!("Not a studio recording"),
}
}
pub fn get_total_frames(&self, fps: u32) -> u32 {
let duration = get_duration(
&self.recordings,
&self.meta,
self.get_studio_meta(),
&self.project_config.1.borrow(),
);
(fps as f64 * duration).ceil() as u32
}
}
impl Drop for EditorInstance {
fn drop(&mut self) {}
}
type PreviewFrameInstruction = (u32, u32, XY<u32>);
pub struct EditorState {
pub playhead_position: u32,
pub playback_task: Option<PlaybackHandle>,
pub preview_task: Option<tokio::task::JoinHandle<()>>,
}
pub struct SegmentMedia {
pub audio: Option<Arc<AudioData>>,
pub system_audio: Option<Arc<AudioData>>,
pub cursor: Arc<CursorEvents>,
pub decoders: RecordingSegmentDecoders,
}
pub async fn create_segments(
recording_meta: &RecordingMeta,
meta: &StudioRecordingMeta,
force_ffmpeg: bool,
) -> Result<Vec<SegmentMedia>, String> {
match &meta {
cap_project::StudioRecordingMeta::SingleSegment { segment: s } => {
let audio = s
.audio
.as_ref()
.map(|audio_meta| {
AudioData::from_file(recording_meta.path(&audio_meta.path))
.map_err(|e| format!("SingleSegment Audio / {e}"))
})
.transpose()?
.map(Arc::new);
let cursor = Arc::new(
s.cursor
.as_ref()
.map(|cursor_path| {
let full_path = recording_meta.path(cursor_path);
match CursorEvents::load_from_file(&full_path) {
Ok(events) => events,
Err(e) => {
warn!(
"Failed to load cursor events from {}: {}",
full_path.display(),
e
);
CursorEvents::default()
}
}
})
.unwrap_or_default(),
);
let decoders = RecordingSegmentDecoders::new(
recording_meta,
meta,
SegmentVideoPaths {
display: recording_meta.path(&s.display.path),
camera: s.camera.as_ref().map(|c| recording_meta.path(&c.path)),
},
0,
force_ffmpeg,
)
.await
.map_err(|e| format!("SingleSegment / {e}"))?;
Ok(vec![SegmentMedia {
audio,
system_audio: None,
cursor,
decoders,
}])
}
cap_project::StudioRecordingMeta::MultipleSegments { inner, .. } => {
let mut segments = vec![];
for (i, s) in inner.segments.iter().enumerate() {
let audio = s
.mic
.as_ref()
.map(|audio| {
AudioData::from_file(recording_meta.path(&audio.path))
.map_err(|e| format!("MultipleSegments {i} Audio / {e}"))
})
.transpose()?
.map(Arc::new);
let system_audio = s
.system_audio
.as_ref()
.map(|audio| {
AudioData::from_file(recording_meta.path(&audio.path))
.map_err(|e| format!("MultipleSegments {i} System Audio / {e}"))
})
.transpose()?
.map(Arc::new);
let cursor = Arc::new(s.cursor_events(recording_meta));
let decoders = RecordingSegmentDecoders::new(
recording_meta,
meta,
SegmentVideoPaths {
display: recording_meta.path(&s.display.path),
camera: s.camera.as_ref().map(|c| recording_meta.path(&c.path)),
},
i,
force_ffmpeg,
)
.await
.map_err(|e| format!("MultipleSegments {i} / {e}"))?;
segments.push(SegmentMedia {
audio,
system_audio,
cursor,
decoders,
});
}
Ok(segments)
}
}
}
fn load_calibration_store(project_path: &std::path::Path) -> cap_audio::CalibrationStore {
let calibration_dir = project_path
.parent()
.and_then(|p| p.parent())
.map(|p| p.to_path_buf())
.unwrap_or_else(|| project_path.to_path_buf());
cap_audio::CalibrationStore::load(&calibration_dir)
}
fn get_calibration_offset(
camera_id: Option<&str>,
mic_id: Option<&str>,
store: &cap_audio::CalibrationStore,
) -> Option<f32> {
match (camera_id, mic_id) {
(Some(cam), Some(mic)) => store.get_offset(cam, mic).map(|o| o as f32),
_ => None,
}
}