Skip to main content

oximedia_transcode/presets/
streaming.rs

1//! Streaming presets for HLS, DASH, and adaptive bitrate.
2
3use crate::{AbrLadder, AbrLadderBuilder, AbrStrategy, PresetConfig, QualityMode};
4
5/// HLS (HTTP Live Streaming) ABR ladder.
6#[must_use]
7pub fn hls_ladder() -> AbrLadder {
8    AbrLadder::hls_standard()
9}
10
11/// DASH (MPEG-DASH) ABR ladder.
12#[must_use]
13pub fn dash_ladder() -> AbrLadder {
14    AbrLadder::hls_standard() // Similar to HLS
15}
16
17/// Low-latency streaming preset (720p).
18#[must_use]
19pub fn low_latency_720p() -> PresetConfig {
20    PresetConfig {
21        video_codec: Some("h264".to_string()),
22        audio_codec: Some("aac".to_string()),
23        video_bitrate: Some(3_000_000), // 3 Mbps
24        audio_bitrate: Some(128_000),
25        width: Some(1280),
26        height: Some(720),
27        frame_rate: Some((30, 1)),
28        quality_mode: Some(QualityMode::Medium),
29        container: Some("mp4".to_string()),
30        audio_channel_layout: None,
31    }
32}
33
34/// Low-latency streaming preset (1080p).
35#[must_use]
36pub fn low_latency_1080p() -> PresetConfig {
37    PresetConfig {
38        video_codec: Some("h264".to_string()),
39        audio_codec: Some("aac".to_string()),
40        video_bitrate: Some(6_000_000), // 6 Mbps
41        audio_bitrate: Some(192_000),
42        width: Some(1920),
43        height: Some(1080),
44        frame_rate: Some((30, 1)),
45        quality_mode: Some(QualityMode::Medium),
46        container: Some("mp4".to_string()),
47        audio_channel_layout: None,
48    }
49}
50
51/// CMAF (Common Media Application Format) preset.
52#[must_use]
53pub fn cmaf() -> PresetConfig {
54    PresetConfig {
55        video_codec: Some("h264".to_string()),
56        audio_codec: Some("aac".to_string()),
57        video_bitrate: Some(5_000_000),
58        audio_bitrate: Some(192_000),
59        width: Some(1920),
60        height: Some(1080),
61        frame_rate: Some((30, 1)),
62        quality_mode: Some(QualityMode::High),
63        container: Some("mp4".to_string()),
64        audio_channel_layout: None,
65    }
66}
67
68/// Creates a custom ABR ladder for streaming.
69#[must_use]
70pub fn custom_ladder(
71    min_resolution: (u32, u32),
72    max_resolution: (u32, u32),
73    strategy: AbrStrategy,
74) -> AbrLadder {
75    AbrLadderBuilder::new(strategy)
76        .min_resolution(min_resolution.0, min_resolution.1)
77        .max_resolution(max_resolution.0, max_resolution.1)
78        .build()
79}
80
81/// Twitch streaming preset (1080p60).
82#[must_use]
83pub fn twitch_1080p60() -> PresetConfig {
84    PresetConfig {
85        video_codec: Some("h264".to_string()),
86        audio_codec: Some("aac".to_string()),
87        video_bitrate: Some(6_000_000), // 6 Mbps (Twitch recommendation)
88        audio_bitrate: Some(160_000),
89        width: Some(1920),
90        height: Some(1080),
91        frame_rate: Some((60, 1)),
92        quality_mode: Some(QualityMode::High),
93        container: Some("mp4".to_string()),
94        audio_channel_layout: None,
95    }
96}
97
98/// Twitch streaming preset (720p60).
99#[must_use]
100pub fn twitch_720p60() -> PresetConfig {
101    PresetConfig {
102        video_codec: Some("h264".to_string()),
103        audio_codec: Some("aac".to_string()),
104        video_bitrate: Some(4_500_000), // 4.5 Mbps
105        audio_bitrate: Some(160_000),
106        width: Some(1280),
107        height: Some(720),
108        frame_rate: Some((60, 1)),
109        quality_mode: Some(QualityMode::High),
110        container: Some("mp4".to_string()),
111        audio_channel_layout: None,
112    }
113}
114
115#[cfg(test)]
116mod tests {
117    use super::*;
118
119    #[test]
120    fn test_hls_ladder() {
121        let ladder = hls_ladder();
122        assert!(ladder.rung_count() > 0);
123        assert_eq!(ladder.strategy, AbrStrategy::AppleHls);
124    }
125
126    #[test]
127    fn test_low_latency_720p() {
128        let preset = low_latency_720p();
129        assert_eq!(preset.width, Some(1280));
130        assert_eq!(preset.height, Some(720));
131        assert_eq!(preset.quality_mode, Some(QualityMode::Medium));
132    }
133
134    #[test]
135    fn test_twitch_1080p60() {
136        let preset = twitch_1080p60();
137        assert_eq!(preset.width, Some(1920));
138        assert_eq!(preset.height, Some(1080));
139        assert_eq!(preset.frame_rate, Some((60, 1)));
140        assert_eq!(preset.video_bitrate, Some(6_000_000));
141    }
142
143    #[test]
144    fn test_custom_ladder() {
145        let ladder = custom_ladder((640, 360), (1920, 1080), AbrStrategy::Conservative);
146        assert_eq!(ladder.strategy, AbrStrategy::Conservative);
147    }
148}