aboutsummaryrefslogtreecommitdiff
path: root/common/src/stream.rs
blob: ca09999b0ed401804a35e8dc6535afa96287d866 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#[cfg(feature = "rocket")]
use rocket::{FromForm, FromFormField, UriDisplayQuery};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Deserialize, Serialize)]
#[cfg_attr(feature = "rocket", derive(FromForm, UriDisplayQuery))]
pub struct StreamSpec {
    pub tracks: Vec<usize>,
    pub format: StreamFormat,
    pub webm: Option<bool>,
    pub abr: Option<usize>,
    pub vbr: Option<usize>,
    pub index: Option<usize>,
}

#[rustfmt::skip]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
#[cfg_attr(feature = "rocket", derive(FromFormField, UriDisplayQuery))]
pub enum StreamFormat {
    #[cfg_attr(feature = "rocket", field(value = "original"))] Original,
    #[cfg_attr(feature = "rocket", field(value = "matroska"))] Matroska,
    #[cfg_attr(feature = "rocket", field(value = "hls"))] Hls,
    #[cfg_attr(feature = "rocket", field(value = "jhls"))] Jhls,
    #[cfg_attr(feature = "rocket", field(value = "hlsseg"))] Segment,
}

impl Default for StreamSpec {
    fn default() -> Self {
        Self {
            tracks: Vec::new(),
            format: StreamFormat::Matroska,
            webm: Some(true),
            abr: None,
            vbr: None,
            index: None,
        }
    }
}

impl StreamSpec {
    pub fn to_query(&self) -> String {
        use std::fmt::Write;
        let mut u = String::new();
        writeln!(u, "format={}", self.format.ident()).unwrap();

        if !self.tracks.is_empty() {
            writeln!(
                u,
                "&tracks={}",
                self.tracks
                    .iter()
                    .map(|s| s.to_string())
                    .collect::<Vec<_>>()
                    .join(",")
            )
            .unwrap();
        }
        if let Some(abr) = self.abr {
            writeln!(u, "&abr={abr}").unwrap();
        }
        if let Some(vbr) = self.vbr {
            writeln!(u, "&vbr={vbr}").unwrap();
        }
        if let Some(index) = self.index {
            writeln!(u, "&index={index}").unwrap();
        }
        if let Some(webm) = self.webm {
            writeln!(u, "&webmm={webm}").unwrap();
        }
        u
    }
}

impl StreamFormat {
    pub fn ident(&self) -> &'static str {
        match self {
            StreamFormat::Original => "original",
            StreamFormat::Matroska => "matroska",
            StreamFormat::Hls => "hls",
            StreamFormat::Jhls => "jhls",
            StreamFormat::Segment => "hlsseg",
        }
    }
}