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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use std::{
collections::{hash_map, HashMap},
sync::{atomic, Arc, Mutex},
};
use futures::{stream::FuturesUnordered, StreamExt};
use crate::{
coding::TrackNamespace,
message::{self, Message},
mlog,
serve::{ServeError, TracksReader},
};
use crate::watch::Queue;
use super::{
Announce, AnnounceRecv, Session, SessionError, Subscribed, SubscribedRecv, TrackStatusRequested,
};
// TODO remove Clone.
#[derive(Clone)]
pub struct Publisher {
webtransport: web_transport::Session,
/// When the announce method is used, a new entry is added to this HashMap to track outbound announcement
announces: Arc<Mutex<HashMap<TrackNamespace, AnnounceRecv>>>,
/// When a Subscribe is received and we have a previous announce for the namespace, then a new entry is
/// added to this HashMap to track the inbound subscription
subscribeds: Arc<Mutex<HashMap<u64, SubscribedRecv>>>,
/// When a Subscribe is received and we DO NOT have a previous announce for the namespace, then a new entry is
/// added to this Queue to track the inbound subscription
unknown_subscribed: Queue<Subscribed>,
/// When a TrackStatus is received and we DO NOT have a previous announce for the namespace, then a new entry is
/// added to this Queue to track the inbound track status request
unknown_track_status_requested: Queue<TrackStatusRequested>,
/// The queue we will write any outbound control messages we want to sent, the session run_send task
/// will process the queue and send the message on the control stream.
outgoing: Queue<Message>,
/// When we need a new Request Id for sending a request, we can get it from here. Note: The instance
/// of AtomicU64 is shared with the Subscriber, so the session uses unique request ids for all requests
/// generated. Note: If we initiated the QUIC connection then request id's start at 0 and increment by 2
/// for each request (even numbers). If we accepted an inbound QUIC connection then request id's start at 1 and
/// increment by 2 for each request (odd numbers).
next_requestid: Arc<atomic::AtomicU64>,
/// Optional mlog writer for logging transport events
mlog: Option<Arc<Mutex<mlog::MlogWriter>>>,
}
impl Publisher {
pub(crate) fn new(
outgoing: Queue<Message>,
webtransport: web_transport::Session,
next_requestid: Arc<atomic::AtomicU64>,
mlog: Option<Arc<Mutex<mlog::MlogWriter>>>,
) -> Self {
Self {
webtransport,
announces: Default::default(),
subscribeds: Default::default(),
unknown_subscribed: Default::default(),
unknown_track_status_requested: Default::default(),
outgoing,
next_requestid,
mlog,
}
}
pub async fn accept(
session: web_transport::Session,
) -> Result<(Session, Publisher), SessionError> {
let (session, publisher, _) = Session::accept(session, None).await?;
Ok((session, publisher.unwrap()))
}
pub async fn connect(
session: web_transport::Session,
) -> Result<(Session, Publisher), SessionError> {
let (session, publisher, _) = Session::connect(session, None).await?;
Ok((session, publisher))
}
/// Announce a namespace and serve tracks using the provided [serve::TracksReader].
/// The caller uses [serve::TracksWriter] for static tracks and [serve::TracksRequest] for dynamic tracks.
pub async fn announce(&mut self, tracks: TracksReader) -> Result<(), SessionError> {
// Check if annouce for this namespace already exists or not, and if not, then create a new Announce
let announce = match self
.announces
.lock()
.unwrap()
.entry(tracks.namespace.clone())
{
// Namespace already exists in HashMap (has already been announced) - return Duplicate error
hash_map::Entry::Occupied(_) => return Err(ServeError::Duplicate.into()),
// This is a new announce, send announce message to peer.
hash_map::Entry::Vacant(entry) => {
// Get the current next request id to use and increment the value for by 2 for the next request
let request_id = self.next_requestid.fetch_add(2, atomic::Ordering::Relaxed);
let (send, recv) =
Announce::new(self.clone(), request_id, tracks.namespace.clone());
entry.insert(recv);
send
}
};
let mut subscribe_tasks = FuturesUnordered::new();
let mut status_tasks = FuturesUnordered::new();
let mut subscribe_done = false;
let mut status_done = false;
// The code enters an infinite loop and waits for one of several events:
// - A new subscription arrives.
// - A new track status request arrives.
// - One of the spawned subscription-handling tasks completes.
// - One of the spawned status-handling tasks completes.
// Exit the loop when all input streams are done (None), and all tasks have completed
loop {
tokio::select! {
// Get next subscription to this announce
res = announce.subscribed(), if !subscribe_done => {
match res? {
Some(subscribed) => {
let tracks = tracks.clone();
subscribe_tasks.push(async move {
let info = subscribed.info.clone();
if let Err(err) = Self::serve_subscribe(subscribed, tracks).await {
log::warn!("failed serving subscribe: {:?}, error: {}", info, err)
}
});
},
None => subscribe_done = true,
}
},
res = announce.track_status_requested(), if !status_done => {
match res? {
Some(status) => {
let tracks = tracks.clone();
status_tasks.push(async move {
let request_msg = status.request_msg.clone();
if let Err(err) = Self::serve_track_status(status, tracks).await {
log::warn!("failed serving track status request: {:?}, error: {}", request_msg, err)
}
});
},
None => status_done = true,
}
},
Some(res) = subscribe_tasks.next() => res,
Some(res) = status_tasks.next() => res,
else => return Ok(())
}
}
}
pub async fn serve_subscribe(
subscribed: Subscribed,
mut tracks: TracksReader,
) -> Result<(), SessionError> {
if let Some(track) = tracks.subscribe(
subscribed.info.track_namespace.clone(),
&subscribed.info.track_name,
) {
subscribed.serve(track).await?;
} else {
let namespace = subscribed.info.track_namespace.clone();
let name = subscribed.info.track_name.clone();
subscribed.close(ServeError::not_found_ctx(format!(
"track '{}/{}' not found in tracks",
namespace, name
)))?;
}
Ok(())
}
pub async fn serve_track_status(
track_status_request: TrackStatusRequested,
mut tracks: TracksReader,
) -> Result<(), SessionError> {
let track = tracks
.subscribe(
track_status_request.request_msg.track_namespace.clone(),
&track_status_request.request_msg.track_name,
)
.ok_or_else(|| {
ServeError::not_found_ctx(format!(
"track '{}/{}' not found for track_status",
track_status_request.request_msg.track_namespace,
track_status_request.request_msg.track_name
))
})?;
track_status_request.respond_ok(&track)?;
Ok(())
}
// Returns subscriptions that do not map to an active announce.
pub async fn subscribed(&mut self) -> Option<Subscribed> {
self.unknown_subscribed.pop().await
}
// Returns track_status requests that do not map to an active announce.
pub async fn track_status_requested(&mut self) -> Option<TrackStatusRequested> {
self.unknown_track_status_requested.pop().await
}
pub(crate) fn recv_message(&mut self, msg: message::Subscriber) -> Result<(), SessionError> {
let res = match msg {
message::Subscriber::Subscribe(msg) => self.recv_subscribe(msg),
message::Subscriber::SubscribeUpdate(msg) => self.recv_subscribe_update(msg),
message::Subscriber::Unsubscribe(msg) => self.recv_unsubscribe(msg),
message::Subscriber::Fetch(_msg) => Err(SessionError::unimplemented("FETCH")),
message::Subscriber::FetchCancel(_msg) => {
Err(SessionError::unimplemented("FETCH_CANCEL"))
}
message::Subscriber::TrackStatus(msg) => self.recv_track_status(msg),
message::Subscriber::SubscribeNamespace(_msg) => {
Err(SessionError::unimplemented("SUBSCRIBE_NAMESPACE"))
}
message::Subscriber::UnsubscribeNamespace(_msg) => {
Err(SessionError::unimplemented("UNSUBSCRIBE_NAMESPACE"))
}
message::Subscriber::PublishNamespaceCancel(msg) => {
self.recv_publish_namespace_cancel(msg)
}
message::Subscriber::PublishNamespaceOk(msg) => self.recv_publish_namespace_ok(msg),
message::Subscriber::PublishNamespaceError(msg) => {
self.recv_publish_namespace_error(msg)
}
message::Subscriber::PublishOk(_msg) => Err(SessionError::unimplemented("PUBLISH_OK")),
message::Subscriber::PublishError(_msg) => {
Err(SessionError::unimplemented("PUBLISH_ERROR"))
}
};
if let Err(err) = res {
log::warn!("failed to process message: {}", err);
}
Ok(())
}
fn recv_publish_namespace_ok(
&mut self,
msg: message::PublishNamespaceOk,
) -> Result<(), SessionError> {
// We need to find the announce request using the request id, however the self.announces data structure
// is a HashMap indexed by Namespace (which is needed for handling PUBLISH_NAMESPACE_CANCEL). TODO - make more efficient.
// For now iterate through all self.annouces until we find the matching id.
let mut announces = self.announces.lock().unwrap();
let announce = announces.iter_mut().find(|(_k, v)| v.request_id == msg.id);
if let Some(announce) = announce {
announce.1.recv_ok()?;
}
Ok(())
}
fn recv_publish_namespace_error(
&mut self,
msg: message::PublishNamespaceError,
) -> Result<(), SessionError> {
// We need to find the announce request using the request id, however the self.announces data structure
// is a HashMap indexed by Namespace (which is needed for handling PUBLISH_NAMESPACE_CANCEL). TODO - make more efficient.
// For now iterate through all self.annouces until we find the matching id.
let mut announces = self.announces.lock().unwrap();
// Find the key first (immutable borrow only)
let key_opt = announces
.iter()
.find(|(_k, v)| v.request_id == msg.id)
.map(|(k, _)| k.clone());
// Remove from HashMap and take ownership
if let Some(key) = key_opt {
if let Some((_ns, v)) = announces.remove_entry(&key) {
// Step 3: call recv_error, consuming v
v.recv_error(ServeError::Closed(msg.error_code))?;
}
}
Ok(())
}
fn recv_publish_namespace_cancel(
&mut self,
msg: message::PublishNamespaceCancel,
) -> Result<(), SessionError> {
// TODO: If a publisher receives new subscriptions for that namespace after receiving an ANNOUNCE_CANCEL,
// it SHOULD close the session as a 'Protocol Violation'.
if let Some(announce) = self.announces.lock().unwrap().remove(&msg.track_namespace) {
announce.recv_error(ServeError::Cancel)?;
}
Ok(())
}
fn recv_subscribe(&mut self, msg: message::Subscribe) -> Result<(), SessionError> {
let namespace = msg.track_namespace.clone();
let subscribed = {
let mut subscribeds = self.subscribeds.lock().unwrap();
// See if entry exists for this request id already, if so error out
let entry = match subscribeds.entry(msg.id) {
hash_map::Entry::Occupied(_) => return Err(SessionError::Duplicate),
hash_map::Entry::Vacant(entry) => entry,
};
// Create new Subscribed entry and add to HashMap
let (send, recv) = Subscribed::new(self.clone(), msg, self.mlog.clone());
entry.insert(recv);
send
};
// If we have an announce, route the subscribe to it.
if let Some(announce) = self.announces.lock().unwrap().get_mut(&namespace) {
return announce.recv_subscribe(subscribed).map_err(Into::into);
}
// Otherwise, put it in the unknown queue.
// TODO Have some way to detect if the application is not reading from the unknown queue,
// then send SubscribeError.
if let Err(err) = self.unknown_subscribed.push(subscribed) {
// Default to closing with a not found error I guess.
err.close(ServeError::not_found_ctx(format!(
"unknown_subscribed queue full for namespace {:?}",
namespace
)))?;
}
Ok(())
}
fn recv_subscribe_update(
&mut self,
_msg: message::SubscribeUpdate,
) -> Result<(), SessionError> {
// TODO: Implement updating subscriptions.
Err(SessionError::unimplemented("SUBSCRIBE_UPDATE"))
}
fn recv_track_status(&mut self, msg: message::TrackStatus) -> Result<(), SessionError> {
let namespace = msg.track_namespace.clone();
// Create TrackStatusRequested to track this request
let track_status_requested = TrackStatusRequested::new(self.clone(), msg);
// If we have an announce, route the track_status to it.
if let Some(announce) = self.announces.lock().unwrap().get_mut(&namespace) {
return announce
.recv_track_status_requested(track_status_requested)
.map_err(Into::into);
}
// Otherwise, put it in the unknown_track_status queue.
// TODO Have some way to detect if the application is not reading from the unknown_track_status queue,
// then send TrackStatusError.
if let Err(mut err) = self
.unknown_track_status_requested
.push(track_status_requested)
{
// push only fails if the queue is dropped, send TrackStatusError, Internal error
err.respond_error(0, "Internal error")?;
}
Ok(())
}
fn recv_unsubscribe(&mut self, msg: message::Unsubscribe) -> Result<(), SessionError> {
if let Some(subscribed) = self.subscribeds.lock().unwrap().get_mut(&msg.id) {
subscribed.recv_unsubscribe()?;
}
Ok(())
}
/// Process a message before sending it, performing any necessary internal actions.
fn act_on_message_to_send<T: Into<message::Publisher>>(
&mut self,
msg: T,
) -> message::Publisher {
let msg = msg.into();
match &msg {
message::Publisher::PublishDone(m) => self.drop_subscribe(m.id),
message::Publisher::SubscribeError(m) => self.drop_subscribe(m.id),
message::Publisher::PublishNamespaceDone(m) => {
self.drop_publish_namespace(&m.track_namespace);
}
_ => {}
}
msg
}
/// Send a message without waiting for it to be sent.
pub(super) fn send_message<T: Into<message::Publisher> + Into<Message>>(&mut self, msg: T) {
let msg = self.act_on_message_to_send(msg);
self.outgoing.push(msg.into()).ok();
}
/// Send a message and wait until it is sent (or at least popped off the outgoing control message queue)
pub(super) async fn send_message_and_wait<T: Into<message::Publisher> + Into<Message>>(
&mut self,
msg: T,
) {
let msg = self.act_on_message_to_send(msg);
self.outgoing
.push_and_wait_until_popped(msg.into())
.await
.ok();
}
fn drop_subscribe(&mut self, id: u64) {
self.subscribeds.lock().unwrap().remove(&id);
}
fn drop_publish_namespace(&mut self, namespace: &TrackNamespace) {
self.announces.lock().unwrap().remove(namespace);
}
pub(super) async fn open_uni(&mut self) -> Result<web_transport::SendStream, SessionError> {
Ok(self.webtransport.open_uni().await?)
}
pub(super) async fn send_datagram(&mut self, data: bytes::Bytes) -> Result<(), SessionError> {
Ok(self.webtransport.send_datagram(data).await?)
}
}