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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use std::ops::Range;

use chrono::{DateTime, Utc};
use mime::Mime;
use serde::{Deserialize, Serialize};
use serde_with::{DefaultOnNull, json::JsonString};
use serde_with::serde_as;
use url::Url;

#[serde_as]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct StreamingData {
    // todo: remove the field adaptive_formats, and deserialize all formats into formats
    pub adaptive_formats: Vec<RawFormat>,
    #[serde_as(as = "JsonString")]
    pub expires_in_seconds: u64,
    #[serde(default)]
    pub formats: Vec<RawFormat>,
}

#[serde_as]
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct RawFormat {
    #[serde(rename = "type")]
    pub format_type: Option<FormatType>,
    #[serde(default)]
    #[serde_as(as = "Option<JsonString>")]
    pub approx_duration_ms: Option<u64>,
    pub audio_channels: Option<u8>,
    pub audio_quality: Option<AudioQuality>,
    #[serde(default)]
    #[serde_as(as = "Option<DefaultOnNull<JsonString>>")]
    pub audio_sample_rate: Option<u64>,
    pub average_bitrate: Option<u64>,
    pub bitrate: Option<u64>,
    pub color_info: Option<ColorInfo>,
    #[serde(default)]
    #[serde_as(as = "Option<JsonString>")]
    pub content_length: Option<u64>,
    #[serde(default)]
    pub fps: u8,
    pub height: Option<u64>,
    pub high_replication: Option<bool>,
    #[serde(default)]
    #[serde_as(as = "Option<crate::serde_impl::range::Range>")]
    pub index_range: Option<Range<u64>>,
    #[serde(default)]
    #[serde_as(as = "Option<crate::serde_impl::range::Range>")]
    pub init_range: Option<Range<u64>>,
    pub itag: u64,
    #[serde(with = "crate::serde_impl::unix_timestamp_micro_secs")]
    pub last_modified: DateTime<Utc>,
    pub loudness_db: Option<f64>,
    #[serde(with = "crate::serde_impl::mime_type")]
    pub mime_type: MimeType,
    pub projection_type: ProjectionType,
    pub quality: Quality,
    pub quality_label: Option<QualityLabel>,
    #[serde(flatten, deserialize_with = "crate::serde_impl::signature_cipher::deserialize")]
    pub signature_cipher: SignatureCipher,
    pub width: Option<u64>,
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)]
pub struct SignatureCipher {
    pub url: Url,
    pub s: Option<String>,
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)]
pub enum FormatType {
    #[serde(rename = "FORMAT_STREAM_TYPE_OTF")]
    Otf,
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "camelCase")]
pub struct ColorInfo {
    pub primaries: Option<ColorInfoPrimary>,
    pub transfer_characteristics: TransferCharacteristics,
    pub matrix_coefficients: Option<MatrixCoefficients>,
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)]
pub enum ColorInfoPrimary {
    #[serde(rename = "COLOR_PRIMARIES_BT709")]
    BT709
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)]
pub enum TransferCharacteristics {
    #[serde(rename = "COLOR_TRANSFER_CHARACTERISTICS_BT709")]
    BT709
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)]
pub enum MatrixCoefficients {
    #[serde(rename = "COLOR_MATRIX_COEFFICIENTS_BT709")]
    BT709
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct MimeType {
    pub mime: Mime,
    // todo: make codec an enum 
    pub codecs: Vec<String>,
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "UPPERCASE")]
pub enum ProjectionType {
    Rectangular,
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum AudioQuality {
    #[serde(rename = "AUDIO_QUALITY_LOW")]
    Low,
    #[serde(rename = "AUDIO_QUALITY_MEDIUM")]
    Medium,
    #[serde(rename = "AUDIO_QUALITY_HIGH")]
    High,
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[serde(rename_all = "lowercase")]
pub enum Quality {
    Tiny,
    Small,
    Medium,
    Large,
    Hd720,
    Hd1080,
    Hd1440,
    Hd2160,
}

#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub enum QualityLabel {
    #[serde(rename = "144p")]
    P144,
    #[serde(rename = "240p")]
    P240,
    #[serde(rename = "360p")]
    P360,
    #[serde(rename = "480p")]
    P480,
    #[serde(rename = "720p")]
    P720,
    #[serde(rename = "720p50")]
    P720Hz50,
    #[serde(rename = "720p60")]
    P720Hz60,
    #[serde(rename = "1080p")]
    P1080,
    #[serde(rename = "1080p50")]
    P1080Hz50,
    #[serde(rename = "1080p60")]
    P1080Hz60,
    #[serde(rename = "1440p")]
    P1440,
    #[serde(rename = "1440p60")]
    P1440Hz60,
    #[serde(rename = "2160p")]
    P2160,
    #[serde(rename = "2160p60")]
    P2160Hz60,
}