Skip to main content

alpha2_model/
source.rs

1use crate::{Generator, MediaFingerprint, ProviderSource};
2use alpha2_media::{ColorMetadata, SourceId};
3use serde::{Deserialize, Serialize};
4
5/// A host-resolved media resource.
6#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
7#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
8#[serde(deny_unknown_fields)]
9#[non_exhaustive]
10pub struct MediaSource {
11    /// Stable source identity.
12    pub id: SourceId,
13    /// Opaque URI or host locator; the engine does not open it. Required except for
14    /// [`MediaKind::Generated`], [`MediaKind::Composition`] and [`MediaKind::Adjustment`] sources,
15    /// which have none.
16    #[serde(default)]
17    pub locator: String,
18    /// Media payload domain.
19    pub kind: MediaKind,
20    /// The color a [`MediaKind::Video`] source's pictures are in, overriding frame and stream
21    /// tags on every host. Recorded at import or declared to correct missing or wrong tags;
22    /// `None` leaves the tags in charge. Stills are always read as sRGB code values.
23    #[serde(default)]
24    pub color_metadata: Option<ColorMetadata>,
25    /// Whether the media has an audio stream. This is a fact of the file, recorded the same by
26    /// every host whether or not it can decode the stream; a host that cannot plays the source
27    /// silent.
28    ///
29    /// [`MediaKind::Audio`] always does. A [`MediaKind::Video`] source must declare it for its
30    /// clips on a visual track to contribute to the mix.
31    #[serde(default)]
32    pub has_audio: bool,
33    /// What a [`MediaKind::Generated`] source draws; present exactly for that kind.
34    #[serde(default)]
35    pub generator: Option<Generator>,
36    /// The composition a [`MediaKind::Composition`] source shows; present exactly for that kind.
37    #[serde(default, skip_serializing_if = "Option::is_none")]
38    pub composition: Option<crate::CompositionId>,
39    /// What the host found at the locator when the project recorded it, so another host can tell
40    /// whether media it finds is the same. Generated sources have none.
41    #[serde(default, skip_serializing_if = "Option::is_none")]
42    pub fingerprint: Option<MediaFingerprint>,
43    /// The extension provider and settings that supply this source's media when the host's own
44    /// decoders do not read it.
45    #[serde(default, skip_serializing_if = "Option::is_none")]
46    pub provider: Option<ProviderSource>,
47}
48
49impl MediaSource {
50    /// Creates a silent-by-default source without explicit color metadata.
51    #[must_use]
52    pub fn new(id: SourceId, locator: impl Into<String>, kind: MediaKind) -> Self {
53        Self {
54            id,
55            locator: locator.into(),
56            kind,
57            color_metadata: None,
58            has_audio: false,
59            generator: None,
60            composition: None,
61            fingerprint: None,
62            provider: None,
63        }
64    }
65
66    /// Creates a source the engine draws itself.
67    #[must_use]
68    pub const fn generated(id: SourceId, generator: Generator) -> Self {
69        Self {
70            id,
71            locator: String::new(),
72            kind: MediaKind::Generated,
73            color_metadata: None,
74            has_audio: false,
75            generator: Some(generator),
76            composition: None,
77            fingerprint: None,
78            provider: None,
79        }
80    }
81
82    /// Creates a source that shows composition `composition`.
83    #[must_use]
84    pub const fn composition(id: SourceId, composition: crate::CompositionId) -> Self {
85        Self {
86            id,
87            locator: String::new(),
88            kind: MediaKind::Composition,
89            color_metadata: None,
90            has_audio: false,
91            generator: None,
92            composition: Some(composition),
93            fingerprint: None,
94            provider: None,
95        }
96    }
97
98    /// Creates an adjustment source: its clips have no picture of their own and apply their
99    /// composition to what is composited beneath them.
100    #[must_use]
101    pub const fn adjustment(id: SourceId) -> Self {
102        Self {
103            id,
104            locator: String::new(),
105            kind: MediaKind::Adjustment,
106            color_metadata: None,
107            has_audio: false,
108            generator: None,
109            composition: None,
110            fingerprint: None,
111            provider: None,
112        }
113    }
114
115    /// Returns whether a host supplies this source's media; the engine draws generated and
116    /// composition sources itself, and an adjustment source has no media.
117    #[must_use]
118    pub const fn is_host_supplied(&self) -> bool {
119        !matches!(
120            self.kind,
121            MediaKind::Generated | MediaKind::Composition | MediaKind::Adjustment
122        )
123    }
124
125    /// Returns whether this source can contribute samples to the audio mix.
126    #[must_use]
127    pub const fn is_audible(&self) -> bool {
128        matches!(self.kind, MediaKind::Audio) || self.has_audio
129    }
130
131    /// Returns declared color metadata or the SDR BT.709 limited-range fallback.
132    #[must_use]
133    pub fn effective_color_metadata(&self) -> ColorMetadata {
134        self.color_metadata.unwrap_or_default()
135    }
136}
137
138/// The payload domain of a source.
139#[derive(Copy, Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
140#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
141#[serde(rename_all = "snake_case")]
142pub enum MediaKind {
143    /// A video stream, potentially with an audio stream.
144    Video,
145    /// An audio stream.
146    Audio,
147    /// A still visual image.
148    Image,
149    /// A picture the engine draws from the source's [`Generator`]; the host supplies nothing.
150    Generated,
151    /// A composition of the project, drawn with the source time as its own time. The host
152    /// supplies only the media its nodes read.
153    Composition,
154    /// No picture of its own: a clip of it on a video track applies its composition, masks,
155    /// opacity and blend mode to everything composited beneath it, its `clip` nodes reading that
156    /// backdrop. The source has only an ID and this kind.
157    Adjustment,
158}