aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/src/stream.rs39
1 files changed, 38 insertions, 1 deletions
diff --git a/common/src/stream.rs b/common/src/stream.rs
index 0e8f810..9a00ce0 100644
--- a/common/src/stream.rs
+++ b/common/src/stream.rs
@@ -4,7 +4,7 @@
Copyright (C) 2025 metamuffin <metamuffin.org>
*/
use serde::{Deserialize, Serialize};
-use std::fmt::Display;
+use std::{collections::BTreeMap, fmt::Display, str::FromStr};
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum StreamSpec {
@@ -106,6 +106,31 @@ impl StreamSpec {
} => format!("?fragment&segment={segment}&track={track}&index={index}&container={container}&format={format}"),
}
}
+ pub fn from_query_kv(query: &BTreeMap<String, String>) -> Result<Self, &'static str> {
+ let get_num = |k: &'static str| {
+ query
+ .get(k)
+ .ok_or(k)
+ .and_then(|a| a.parse().map_err(|_| "invalid number"))
+ };
+ let get_container = || {
+ query
+ .get("container")
+ .ok_or("container")
+ .and_then(|s| s.parse().map_err(|()| "unknown container"))
+ };
+ if query.contains_key("fragment") {
+ Ok(Self::Fragment {
+ segment: get_num("segment")?,
+ track: get_num("track")? as usize,
+ index: get_num("index")?,
+ container: get_container()?,
+ format: get_num("format")? as usize,
+ })
+ } else {
+ Err("invalid stream spec")
+ }
+ }
}
impl Display for StreamContainer {
@@ -118,3 +143,15 @@ impl Display for StreamContainer {
})
}
}
+impl FromStr for StreamContainer {
+ type Err = ();
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ Ok(match s {
+ "webm" => StreamContainer::WebM,
+ "matroska" => StreamContainer::Matroska,
+ "webvtt" => StreamContainer::WebVTT,
+ "jvtt" => StreamContainer::JVTT,
+ _ => return Err(()),
+ })
+ }
+}