-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream_information_store.rs
172 lines (143 loc) · 4.83 KB
/
stream_information_store.rs
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
172
use std::{borrow::Borrow, collections::HashMap, hash::Hash};
use bit_cursor::nsw_types::u7;
use crate::{
rtp_parser::MediaType,
util::{LiveStateReader, LiveStateWriter},
};
// TODO: this should map to a 'PayloadType' struct which contains other information
#[derive(Default)]
pub struct PayloadTypes(HashMap<u7, MediaType>);
impl PayloadTypes {
pub fn insert(&mut self, k: u7, v: MediaType) -> Option<MediaType> {
self.0.insert(k, v)
}
pub fn get<Q>(&self, k: &Q) -> Option<&MediaType>
where
u7: Borrow<Q>,
Q: Hash + Eq,
{
self.0.get(k)
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
#[derive(Default)]
pub struct HeaderExtensionIds(HashMap<String, u8>);
impl HeaderExtensionIds {
pub fn insert(&mut self, k: String, v: u8) -> Option<u8> {
self.0.insert(k, v)
}
pub fn get<Q>(&self, k: &Q) -> Option<&u8>
where
String: Borrow<Q>,
Q: Hash + Eq,
{
self.0.get(k)
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
pub struct StreamInformationStore {
payload_types: LiveStateWriter<PayloadTypes>,
header_extension_ids: LiveStateWriter<HeaderExtensionIds>,
// For parties who are interested in only a single mapping
header_extension_id_writers: HashMap<String, LiveStateWriter<Option<u8>>>,
}
impl StreamInformationStore {
pub fn new() -> Self {
let payload_types = LiveStateWriter::new(PayloadTypes::default());
let header_extension_ids = LiveStateWriter::new(HeaderExtensionIds::default());
StreamInformationStore {
payload_types,
header_extension_ids,
header_extension_id_writers: HashMap::default(),
}
}
pub fn add_payload_type(&mut self, pt: u7, media_type: MediaType) {
self.payload_types
.modify(|pts| _ = pts.insert(pt, media_type));
}
pub fn add_payload_types(&mut self, new_pts: HashMap<u7, MediaType>) {
self.payload_types.modify(|pts| pts.0.extend(new_pts));
}
pub fn subscribe_to_pt_changes(&self) -> LiveStateReader<PayloadTypes> {
self.payload_types.reader()
}
pub fn add_header_extension(&mut self, uri: String, id: u8) {
let uri_insert: String = uri.clone();
self.header_extension_ids
.modify(|hes| _ = hes.insert(uri_insert, id));
if let Some(w) = self.header_extension_id_writers.get(&uri) {
w.set(Some(id))
}
}
pub fn add_header_extensions(&mut self, new_hes: HashMap<String, u8>) {
for (uri, id) in &new_hes {
if let Some(w) = self.header_extension_id_writers.get(uri) {
w.set(Some(*id))
}
}
self.header_extension_ids
.modify(|hes| hes.0.extend(new_hes));
}
pub fn subscribe_to_header_extension_id_changes(&self) -> LiveStateReader<HeaderExtensionIds> {
self.header_extension_ids.reader()
}
pub fn subscribe_to_header_extension_id_change<T>(
&mut self,
uri: T,
) -> LiveStateReader<Option<u8>>
where
String: Borrow<T>,
T: Hash + Eq + Into<String>,
{
let writer = if let Some(id) = self.header_extension_ids.value().get(&uri) {
LiveStateWriter::new(Some(*id))
} else {
LiveStateWriter::new(None)
};
let reader = writer.reader();
self.header_extension_id_writers.insert(uri.into(), writer);
reader
}
}
impl Default for StreamInformationStore {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_stream_information_store() {
let mut store = StreamInformationStore::new();
let he_reader = store.subscribe_to_header_extension_id_changes();
let pt_reader = store.subscribe_to_pt_changes();
assert!(he_reader.value().is_empty());
assert!(pt_reader.value().is_empty());
store.add_header_extension(String::from("uri"), 10);
assert_eq!(
he_reader.value().get(&String::from("uri")),
Some(10).as_ref()
);
assert!(pt_reader.value().is_empty());
store.add_payload_type(u7::new(100), MediaType::Video);
assert_eq!(
pt_reader.value().get(&u7::new(100)),
Some(MediaType::Video).as_ref()
);
}
#[test]
fn test_subscribe_to_single_header_ext() {
let mut store = StreamInformationStore::new();
let reader = store.subscribe_to_header_extension_id_change(String::from("foo"));
assert_eq!(*reader.value(), None);
store.add_header_extension(String::from("foo"), 10);
assert_eq!(*reader.value(), Some(10));
let reader2 = store.subscribe_to_header_extension_id_change(String::from("foo"));
assert_eq!(*reader2.value(), Some(10));
}
}