Skip to main content

rskit_media/stream/
track.rs

1//! Track information types for media containers.
2
3use std::time::Duration;
4
5use serde::{Deserialize, Serialize};
6
7use crate::{
8    audio::{ChannelLayout, SampleRate},
9    codec::{Codec, CodecLevel, CodecProfile},
10    color::{ColorRange, ColorSpace, PixelFormat},
11    spatial::{FrameRate, Resolution},
12    types::TrackKind,
13};
14
15/// A single track/stream within a media container.
16#[derive(Debug, Clone, Serialize, Deserialize)]
17pub struct Track {
18    /// Track index within the container.
19    pub index: usize,
20    /// Kind of track.
21    pub kind: TrackKind,
22    /// Codec used for this track.
23    pub codec: Option<Codec>,
24    /// Track bitrate in bits per second.
25    pub bitrate: Option<u64>,
26    /// Track language (BCP 47 tag).
27    pub language: Option<String>,
28    /// Whether this is the default track for its kind.
29    pub is_default: bool,
30    /// Track title.
31    pub title: Option<String>,
32    /// Track duration.
33    pub duration: Option<Duration>,
34    /// Video-specific info (populated if `kind == Video`).
35    pub video: Option<VideoTrackInfo>,
36    /// Audio-specific info (populated if `kind == Audio`).
37    pub audio: Option<AudioTrackInfo>,
38    /// Subtitle-specific info (populated if `kind == Subtitle`).
39    pub subtitle: Option<SubtitleTrackInfo>,
40}
41
42/// Video-specific track information.
43#[derive(Debug, Clone, Serialize, Deserialize)]
44pub struct VideoTrackInfo {
45    /// Resolution (width × height).
46    pub resolution: Resolution,
47    /// Frame rate.
48    pub frame_rate: Option<FrameRate>,
49    /// Pixel format.
50    pub pixel_format: Option<PixelFormat>,
51    /// Rotation in degrees (e.g., 90 for portrait video on mobile).
52    pub rotation: Option<i16>,
53    /// Color space.
54    pub color_space: Option<ColorSpace>,
55    /// Color range (limited / full).
56    pub color_range: Option<ColorRange>,
57    /// Bit depth per channel.
58    pub bit_depth: Option<u8>,
59    /// Codec profile (e.g., H264High, HevcMain10).
60    pub profile: Option<CodecProfile>,
61    /// Codec level (e.g., "4.1", "5.1").
62    pub level: Option<CodecLevel>,
63    /// HDR metadata (None for SDR content).
64    pub hdr: Option<HdrMetadata>,
65}
66
67/// HDR metadata attached to a video track.
68#[derive(Debug, Clone, Serialize, Deserialize)]
69pub struct HdrMetadata {
70    /// HDR format.
71    pub format: HdrFormat,
72    /// Mastering display metadata.
73    pub mastering_display: Option<MasteringDisplay>,
74    /// Content light level info.
75    pub content_light_level: Option<ContentLightLevel>,
76}
77
78/// HDR format variant.
79#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
80pub enum HdrFormat {
81    /// HDR10 (static metadata).
82    Hdr10,
83    /// HDR10+ (dynamic metadata).
84    Hdr10Plus,
85    /// Dolby Vision.
86    DolbyVision,
87    /// Hybrid Log-Gamma.
88    Hlg,
89    /// Generic PQ (SMPTE ST 2084) without specific format.
90    Pq,
91}
92
93/// SMPTE ST 2086 mastering display metadata.
94#[derive(Debug, Clone, Serialize, Deserialize)]
95pub struct MasteringDisplay {
96    /// Display primaries as CIE 1931 chromaticity coordinates (x, y) in the order: Green, Blue, Red.
97    pub primaries: Option<[(f64, f64); 3]>,
98    /// White point as CIE 1931 chromaticity (x, y).
99    pub white_point: Option<(f64, f64)>,
100    /// Min/max luminance in cd/m² (nits).
101    pub luminance: Option<(f64, f64)>,
102}
103
104/// Content light level info (CTA-861.3).
105#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
106pub struct ContentLightLevel {
107    /// Maximum Content Light Level in cd/m².
108    pub max_cll: u32,
109    /// Maximum Frame-Average Light Level in cd/m².
110    pub max_fall: u32,
111}
112
113/// Audio-specific track information.
114#[derive(Debug, Clone, Serialize, Deserialize)]
115pub struct AudioTrackInfo {
116    /// Sample rate.
117    pub sample_rate: SampleRate,
118    /// Channel layout.
119    pub channels: ChannelLayout,
120    /// Bit depth per sample.
121    pub bit_depth: Option<u8>,
122}
123
124/// Subtitle-specific track information.
125#[derive(Debug, Clone, Serialize, Deserialize)]
126pub struct SubtitleTrackInfo {
127    /// Subtitle format (e.g., "srt", "ass").
128    pub format: String,
129    /// Whether this is a forced subtitle track.
130    pub forced: bool,
131}