use bincode::{Decode, Encode}; /* This file is part of jellything (https://codeberg.org/metamuffin/jellything) which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2023 metamuffin */ #[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, pub format: StreamFormat, pub webm: Option, pub profile: Option, pub index: Option, } #[rustfmt::skip] #[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, Hash, Encode, Decode)] #[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 = "hlsmaster"))] HlsMaster, #[cfg_attr(feature = "rocket", field(value = "hlsvariant"))] HlsVariant, #[cfg_attr(feature = "rocket", field(value = "jhlsi"))] JhlsIndex, #[cfg_attr(feature = "rocket", field(value = "snippet"))] Snippet, #[cfg_attr(feature = "rocket", field(value = "webvtt"))] Webvtt, } impl Default for StreamSpec { fn default() -> Self { Self { tracks: Vec::new(), format: StreamFormat::Matroska, webm: Some(true), profile: None, index: None, } } } impl StreamSpec { pub fn to_query(&self) -> String { use std::fmt::Write; let mut u = String::new(); write!(u, "format={}", self.format.ident()).unwrap(); if !self.tracks.is_empty() { write!( u, "&tracks={}", self.tracks .iter() .map(|s| s.to_string()) .collect::>() .join(",") ) .unwrap(); } if let Some(profile) = self.profile { write!(u, "&profile={profile}").unwrap(); } if let Some(index) = self.index { write!(u, "&index={index}").unwrap(); } if let Some(webm) = self.webm { write!(u, "&webm={webm}").unwrap(); } u } } impl StreamFormat { pub fn ident(&self) -> &'static str { match self { StreamFormat::Original => "original", StreamFormat::Matroska => "matroska", StreamFormat::HlsMaster => "hlsmaster", StreamFormat::HlsVariant => "hlsvariant", StreamFormat::JhlsIndex => "jhlsi", StreamFormat::Snippet => "snippet", StreamFormat::Webvtt => "webvtt", } } }