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 | MediaKind::Image | MediaKind::Generated | MediaKind::Composition
129 ) | (Self::Audio, MediaKind::Audio)
130 )
131 }
132}
133
134#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
136#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
137#[serde(deny_unknown_fields)]
138#[non_exhaustive]
139pub struct Clip {
140 pub id: ClipId,
142 pub source_id: SourceId,
144 pub timeline_range: TimeRange,
146 pub source_range: TimeRange,
148 #[serde(default)]
150 pub visual: VisualProperties,
151 #[serde(default)]
154 pub composition: Option<ClipComposition>,
155 #[serde(default)]
157 pub blend_mode: ClipBlendMode,
158 #[serde(default)]
161 #[serde(deserialize_with = "crate::bounded::seq::<_, _, { crate::MAX_CLIP_MASKS }>")]
162 #[cfg_attr(feature = "schema", schemars(length(max = crate::MAX_CLIP_MASKS)))]
163 pub masks: Vec<ShapeMask>,
164 #[serde(default)]
166 pub matte: Option<Matte>,
167 #[serde(default)]
169 pub time_map: TimeMap,
170 #[serde(default)]
172 pub audio: AudioProperties,
173 #[serde(default, skip_serializing_if = "CacheIntent::is_auto")]
176 pub cache: CacheIntent,
177}
178
179#[derive(Copy, Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
181#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
182#[serde(rename_all = "snake_case")]
183pub enum CacheIntent {
184 #[default]
186 Auto,
187 Pin,
189 Bypass,
191}
192
193impl CacheIntent {
194 #[must_use]
196 pub const fn is_auto(&self) -> bool {
197 matches!(self, Self::Auto)
198 }
199}
200
201impl Clip {
202 #[must_use]
205 pub fn stretches_audio(&self) -> bool {
206 self.audio.preserve_pitch
207 && matches!(self.time_map, TimeMap::Forward | TimeMap::Reverse)
208 && self.timeline_range.duration() != self.source_range.duration()
209 }
210
211 #[must_use]
213 pub fn new(
214 id: ClipId,
215 source_id: SourceId,
216 timeline_range: TimeRange,
217 source_range: TimeRange,
218 ) -> Self {
219 Self {
220 id,
221 source_id,
222 timeline_range,
223 source_range,
224 visual: VisualProperties::default(),
225 composition: None,
226 blend_mode: ClipBlendMode::default(),
227 masks: Vec::new(),
228 matte: None,
229 time_map: TimeMap::Forward,
230 audio: AudioProperties::default(),
231 cache: CacheIntent::Auto,
232 }
233 }
234}
235
236#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
238#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
239#[serde(deny_unknown_fields)]
240pub struct ClipTransition {
241 pub from_clip: ClipId,
243 pub to_clip: ClipId,
245 pub timeline_range: TimeRange,
247 pub easing: Easing,
250 pub effect: ClipEffect,
252}