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
//! Cheaply-cloneable identifiers for applications and streams.
use std::fmt;
use std::sync::Arc;
/// Shared implementation for cheaply-cloneable `Arc<str>` newtype identifiers.
macro_rules! arc_str_newtype {
($(#[$meta:meta])* $Name:ident) => {
$(#[$meta])*
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct $Name(Arc<str>);
impl serde::Serialize for $Name {
fn serialize<S: serde::Serializer>(&self, s: S) -> std::result::Result<S::Ok, S::Error> {
s.serialize_str(&self.0)
}
}
impl<'de> serde::Deserialize<'de> for $Name {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> std::result::Result<Self, D::Error> {
let s = String::deserialize(d)?;
Ok(Self(Arc::from(s.as_str())))
}
}
impl $Name {
/// Create a new identifier from any string-like value.
pub fn new(s: impl Into<String>) -> Self {
Self(Arc::from(s.into().as_str()))
}
/// Borrow the identifier as a `&str`.
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for $Name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl From<&str> for $Name {
fn from(s: &str) -> Self { Self(Arc::from(s)) }
}
impl From<String> for $Name {
fn from(s: String) -> Self { Self(Arc::from(s.as_str())) }
}
};
}
arc_str_newtype!(
/// Name of an application (e.g. `"live"`, `"vod"`).
/// Cheaply cloneable — backed by an `Arc<str>`.
AppName
);
arc_str_newtype!(
/// Unique identifier for a stream within an application.
/// Cheaply cloneable — backed by an `Arc<str>`.
StreamId
);
/// Composite key uniquely identifying a stream: `(app_name, stream_id)`.
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct StreamKey {
/// Application the stream belongs to.
pub app: AppName,
/// Stream identifier within the application.
pub stream_id: StreamId,
}
impl StreamKey {
/// Build a key from an application name and stream id.
pub fn new(app: impl Into<AppName>, stream_id: impl Into<StreamId>) -> Self {
Self {
app: app.into(),
stream_id: stream_id.into(),
}
}
/// The key for an adaptive-bitrate *layer* of this stream: `<stream>~<variant>`
/// in the same application.
///
/// This is the one convention shared across the media plane — the WebRTC
/// simulcast demux (one key per RID), an ABR transcoder (one per rendition
/// rung), and the WHEP layer selector that discovers and switches between them
/// all agree on it, so a transcoded rung and a simulcast layer are
/// interchangeable to a viewer.
pub fn layer(&self, variant: &str) -> StreamKey {
StreamKey::new(
self.app.as_str(),
format!("{}~{}", self.stream_id.as_str(), variant),
)
}
/// The base stream id of a layer key (everything before the first `~`), i.e.
/// the inverse of [`layer`](Self::layer). Returns the whole id when it carries
/// no `~` (already a base stream).
pub fn layer_base(&self) -> &str {
let s = self.stream_id.as_str();
s.split_once('~').map(|(base, _)| base).unwrap_or(s)
}
}
impl fmt::Display for StreamKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}/{}", self.app, self.stream_id)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn newtypes_are_cheap_clones_with_value_equality() {
let a = AppName::from("live");
let b = a.clone();
assert_eq!(a, b);
assert_eq!(a.as_str(), "live");
// Clones share the same Arc allocation (cheap, not a deep copy).
assert!(std::ptr::eq(a.as_str().as_ptr(), b.as_str().as_ptr()));
}
#[test]
fn layer_key_round_trips_with_base() {
let base = StreamKey::new("live", "show");
let high = base.layer("f");
assert_eq!(high.stream_id.as_str(), "show~f");
assert_eq!(high.app, base.app);
// The base id is recoverable from a layer key, and a plain key is its own
// base.
assert_eq!(high.layer_base(), "show");
assert_eq!(base.layer_base(), "show");
}
#[test]
fn stream_key_display_is_app_slash_stream() {
let key = StreamKey::new("live", "cam-1");
assert_eq!(key.to_string(), "live/cam-1");
}
#[test]
fn identifiers_roundtrip_through_serde() {
let key = StreamKey::new("vod", "movie");
let json = serde_json::to_string(&key).expect("serialize");
let back: StreamKey = serde_json::from_str(&json).expect("deserialize");
assert_eq!(key, back);
}
}