1use crate::{
2 AudioProperties, Captions, ClipBlendMode, ClipComposition, ClipEffect, ClipId, Easing, Matte,
3 MediaKind, ShapeMask, TimeMap, TrackAudio, TrackId, VisualProperties,
4};
5use alpha2_media::SourceId;
6use alpha2_time::TimeRange;
7use serde::{Deserialize, Serialize};
8
9#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
11#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
12#[serde(deny_unknown_fields)]
13pub struct Timeline {
14 #[serde(deserialize_with = "crate::bounded::seq::<_, _, { crate::MAX_TRACKS }>")]
16 #[cfg_attr(feature = "schema", schemars(length(max = crate::MAX_TRACKS)))]
17 pub tracks: Vec<Track>,
18}
19
20impl Timeline {
21 #[must_use]
24 pub fn extent(&self) -> Option<TimeRange> {
25 let mut ranges = self.tracks.iter().flat_map(|track| {
26 track.clips.iter().map(|clip| clip.timeline_range).chain(
27 track
28 .captions
29 .iter()
30 .flat_map(|captions| captions.cues.iter().map(|cue| cue.timeline_range)),
31 )
32 });
33 let first = ranges.next()?;
34 let (start, end) = ranges.fold((first.start(), first.end()), |(start, end), range| {
35 (start.min(range.start()), end.max(range.end()))
36 });
37 TimeRange::new(start, end).ok()
38 }
39
40 #[must_use]
43 pub fn any_solo(&self) -> bool {
44 self.tracks.iter().any(|track| track.audio.solo)
45 }
46
47 #[must_use]
50 pub fn matte_sources(&self) -> std::collections::HashSet<&ClipId> {
51 self.tracks
52 .iter()
53 .flat_map(|track| &track.clips)
54 .filter_map(|clip| clip.matte.as_ref().map(|matte| &matte.source))
55 .collect()
56 }
57}
58
59#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
61#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
62#[serde(deny_unknown_fields)]
63#[non_exhaustive]
64pub struct Track {
65 pub id: TrackId,
67 pub name: String,
69 pub kind: TrackKind,
71 #[serde(default)]
74 #[serde(deserialize_with = "crate::bounded::seq::<_, _, { crate::MAX_CLIPS_PER_TRACK }>")]
75 #[cfg_attr(feature = "schema", schemars(length(max = crate::MAX_CLIPS_PER_TRACK)))]
76 pub clips: Vec<Clip>,
77 #[serde(default)]
79 #[serde(
80 deserialize_with = "crate::bounded::seq::<_, _, { crate::MAX_TRANSITIONS_PER_TRACK }>"
81 )]
82 #[cfg_attr(feature = "schema", schemars(length(max = crate::MAX_TRANSITIONS_PER_TRACK)))]
83 pub transitions: Vec<ClipTransition>,
84 #[serde(default)]
86 pub audio: TrackAudio,
87 #[serde(default)]
89 pub captions: Option<Captions>,
90}
91
92impl Track {
93 #[must_use]
95 pub fn new(id: TrackId, name: impl Into<String>, kind: TrackKind) -> Self {
96 Self {
97 id,
98 name: name.into(),
99 kind,
100 clips: Vec::new(),
101 transitions: Vec::new(),
102 audio: TrackAudio::default(),
103 captions: (kind == TrackKind::Caption).then(Captions::default),
104 }
105 }
106}
107
108#[derive(Copy, Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
110#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
111#[serde(rename_all = "snake_case")]
112#[non_exhaustive]
113pub enum TrackKind {
114 Video,
116 Audio,
118 Caption,
120}
121
122impl TrackKind {
123 pub(crate) const fn accepts(self, source: MediaKind) -> bool {
124 matches!(
125 (self, source),
126 (
127 Self::Video,
128 MediaKind::Video
129 | MediaKind::Image
130 | MediaKind::Generated
131 | MediaKind::Composition
132 | MediaKind::Adjustment
133 ) | (Self::Audio, MediaKind::Audio)
134 )
135 }
136}
137
138#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
140#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
141#[serde(deny_unknown_fields)]
142#[non_exhaustive]
143pub struct Clip {
144 pub id: ClipId,
146 pub source_id: SourceId,
148 pub timeline_range: TimeRange,
150 pub source_range: TimeRange,
152 #[serde(default)]
154 pub visual: VisualProperties,
155 #[serde(default)]
158 pub composition: Option<ClipComposition>,
159 #[serde(default)]
161 pub blend_mode: ClipBlendMode,
162 #[serde(default)]
165 #[serde(deserialize_with = "crate::bounded::seq::<_, _, { crate::MAX_CLIP_MASKS }>")]
166 #[cfg_attr(feature = "schema", schemars(length(max = crate::MAX_CLIP_MASKS)))]
167 pub masks: Vec<ShapeMask>,
168 #[serde(default)]
170 pub matte: Option<Matte>,
171 #[serde(default)]
173 pub time_map: TimeMap,
174 #[serde(default)]
176 pub audio: AudioProperties,
177 #[serde(default, skip_serializing_if = "CacheIntent::is_auto")]
180 pub cache: CacheIntent,
181}
182
183#[derive(Copy, Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
185#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
186#[serde(rename_all = "snake_case")]
187pub enum CacheIntent {
188 #[default]
190 Auto,
191 Pin,
193 Bypass,
195}
196
197impl CacheIntent {
198 #[must_use]
200 pub const fn is_auto(&self) -> bool {
201 matches!(self, Self::Auto)
202 }
203}
204
205impl Clip {
206 #[must_use]
209 pub fn stretches_audio(&self) -> bool {
210 self.audio.preserve_pitch
211 && matches!(self.time_map, TimeMap::Forward | TimeMap::Reverse)
212 && self.timeline_range.duration() != self.source_range.duration()
213 }
214
215 #[must_use]
217 pub fn new(
218 id: ClipId,
219 source_id: SourceId,
220 timeline_range: TimeRange,
221 source_range: TimeRange,
222 ) -> Self {
223 Self {
224 id,
225 source_id,
226 timeline_range,
227 source_range,
228 visual: VisualProperties::default(),
229 composition: None,
230 blend_mode: ClipBlendMode::default(),
231 masks: Vec::new(),
232 matte: None,
233 time_map: TimeMap::Forward,
234 audio: AudioProperties::default(),
235 cache: CacheIntent::Auto,
236 }
237 }
238}
239
240#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
242#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
243#[serde(deny_unknown_fields)]
244pub struct ClipTransition {
245 pub from_clip: ClipId,
247 pub to_clip: ClipId,
249 pub timeline_range: TimeRange,
251 pub easing: Easing,
254 pub effect: ClipEffect,
256}