Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions rs/moq-hls/MIGRATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ How to update your code for each breaking change, newest first.

## Unreleased

### Denied broadcast references report authorization failure

A rendition whose referenced broadcast is outside the source origin's scope now
returns `Error::Mux(moq_mux::Error::Moq(moq_net::Error::Unauthorized))` from
playlist (ranged too), init and segment reads, and from the segment cursor,
instead of behaving like missing media.
Comment thread
kixelated marked this conversation as resolved.
Resolved
`Broadcaster::ready` and `Rendition::playable` settle on that refusal. Embedders
should answer it with an uncacheable 403; the HTTP server does so automatically.
Allowed renditions keep serving, and missing or disconnected media remains 404.

### A failed timeline fails the segment cursor

`export::segments::Consumer::next` returns `Err(Error::TimelineFailed { .. })` once a failed timeline's covered segments drain, instead of `Ok(None)`.
Expand Down
4 changes: 2 additions & 2 deletions rs/moq-hls/src/export/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ impl Broadcaster {
self.renditions.subscribe()
}

/// Resolve once at least one rendition has a playable media playlist. How long to wait is
/// the caller's policy: wrap this in a timeout (or select against it) as needed.
/// Resolve once at least one rendition has a playable media playlist or a settled failure.
/// How long to wait is the caller's policy: wrap this in a timeout as needed.
pub async fn ready(&self) {
self.renditions.ready().await;
}
Expand Down
131 changes: 115 additions & 16 deletions rs/moq-hls/src/export/rendition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,15 @@ impl Media {
handle.binding.clone()
}

/// The settled authorization refusal of this rendition's referenced broadcast.
fn refusal(&self) -> Option<crate::Error> {
let handle = self.handle.lock().expect("media lock poisoned");
match handle.binding.poll_broadcast(&kio::Waiter::noop()) {
Poll::Ready(Err(err @ moq_mux::Error::Moq(moq_net::Error::Unauthorized))) => Some(err.into()),
_ => None,
}
}

fn poll_track(
&self,
binding: &moq_mux::Binding,
Expand Down Expand Up @@ -550,9 +559,12 @@ impl Rendition {
kio::wait(|waiter| self.poll_playable(waiter)).await;
}

/// Poll until this rendition has a renderable media playlist, or a timeline failed.
/// Poll until this rendition is playable, its media is denied, or a timeline failed.
pub(crate) fn poll_playable(&self, waiter: &kio::Waiter) -> Poll<()> {
self.poll_media(waiter);
if self.media.refusal().is_some() {
return Poll::Ready(());
}
if self.spans.poll_failed(waiter).is_ready() {
return Poll::Ready(());
}
Expand All @@ -574,6 +586,9 @@ impl Rendition {
/// or this rendition's own timeline failed, rather than ending a truncated playlist with
/// `EXT-X-ENDLIST`. A server should answer that with an uncacheable error.
///
/// A denied referenced broadcast propagates the underlying authorization error; it is
/// not missing media. The HTTP server answers it with 403 and no-store.
///
/// The playlist names its init segment by a hash of the bytes, so it is also `None` until
/// the init is known. An inline-parameter-set codec (no catalog `description`) only learns
/// it from media: await [`init`](Self::init) first.
Expand Down Expand Up @@ -625,6 +640,8 @@ impl Rendition {
/// A failed timeline fails it with [`TimelineFailed`](crate::Error::TimelineFailed), as in
/// [`media_playlist`](Self::media_playlist).
pub async fn playlist(&self, range: Option<super::Range>, query: Option<&str>) -> Result<Option<String>> {
// A denied reference is refused before anything else, so no other error masks it.
self.authorize()?;
// Refuse an unsupported range up front rather than wait for media it can never list.
if range.is_some() {
self.check_range()?;
Expand Down Expand Up @@ -669,9 +686,12 @@ impl Rendition {
Ok(self.snapshot_from_window(window, finished, generation))
}

/// Why every listing of `window` refuses: the reference timeline failed, or this rendition's
/// own timeline failed before covering every row it lists or will list.
/// Why listings refuse: the referenced media is denied, or a timeline failed before
/// covering every row it lists or will list.
fn failure(&self, window: &segments::Window) -> Option<crate::Error> {
if let Some(err) = self.media.refusal() {
return Some(err);
}
if let Some(err) = &window.failed {
return Some(err.clone());
}
Expand All @@ -681,24 +701,33 @@ impl Rendition {
(!failure.late || window.segments.iter().any(|row| self.is_failed(row))).then_some(failure.err)
}

/// Fail if a timeline this rendition lists failed (see [`failure`](Self::failure)).
/// Return bound media or its authorization refusal, leaving timeline state alone.
pub(crate) fn authorize(&self) -> Result<Arc<moq_mux::Binding>> {
let binding = self.media.sync(&self.live);
match self.media.refusal() {
Some(err) => Err(err),
None => Ok(binding),
}
}

/// Refuse denied media or a failed timeline (see [`failure`](Self::failure)).
pub(crate) fn check(&self) -> Result<()> {
self.authorize()?;
if let Some(err) = self.live.failure() {
return Err(err);
}
// Only a failed own timeline needs the window to judge.
if self.spans.failure().is_none() {
return Ok(());
}
self.media.sync(&self.live);
match self.failure(&self.live.window()) {
Some(err) => Err(err),
None => Ok(()),
}
}

/// Whether only this rendition's own timeline failed, so the broadcast-wide listings leave it
/// out while its siblings stay available.
/// Whether this rendition failed independently of the reference timeline, so listings
/// leave it out while its siblings stay available.
pub(crate) fn failed_alone(&self) -> bool {
self.live.failure().is_none() && self.check().is_err()
}
Expand Down Expand Up @@ -957,7 +986,7 @@ impl Rendition {
allow(dead_code, reason = "only the `server` feature uses it")
)]
pub(crate) async fn segment_at(&self, tag: &str, time: u64) -> Result<Option<Bytes>> {
self.media.sync(&self.live);
self.authorize()?;
let Some(segment) = self.live.segment_number_at(tag, time, self.timescale()) else {
return Ok(None);
};
Expand Down Expand Up @@ -1016,8 +1045,17 @@ impl Rendition {
async fn track(
&self,
binding: &moq_mux::Binding,
) -> Option<(moq_net::broadcast::Consumer, moq_net::track::Consumer)> {
kio::wait(|waiter| self.media.poll_track(binding, waiter)).await
) -> Result<Option<(moq_net::broadcast::Consumer, moq_net::track::Consumer)>> {
let track = kio::wait(|waiter| self.media.poll_track(binding, waiter)).await;
// The bind can be refused while this read waits. Preserve that exact instance's
// authorization error; other failed or disconnected references stay missing.
if track.is_none()
&& let Poll::Ready(Err(err @ moq_mux::Error::Moq(moq_net::Error::Unauthorized))) =
binding.poll_broadcast(&kio::Waiter::noop())
{
return Err(err.into());
}
Ok(track)
}

/// The media track's name in its broadcast, which can differ from the rendition's ID.
Expand Down Expand Up @@ -1070,7 +1108,7 @@ impl Rendition {
}

async fn load_init(&self) -> Result<Option<Arc<Init>>> {
let binding = self.media.sync(&self.live);
let binding = self.authorize()?;
// A transmuxed segment can cache the init while another bootstrap holds `building`,
// blocked on an unfinished group.
if let Some(init) = self.run().init {
Expand All @@ -1094,7 +1132,7 @@ impl Rendition {
let Some(sequence) = latest.or_else(|| self.live.latest_keyframe_group(&reference)) else {
return Ok(None);
};
let Some((broadcast, track)) = self.track(&binding).await else {
let Some((broadcast, track)) = self.track(&binding).await? else {
return Ok(None);
};
let Ok(mut group) = fetch(&track, hang::timeline::Position::group(sequence), None, &broadcast).await?
Expand All @@ -1121,9 +1159,10 @@ impl Rendition {
/// short groups) and encodes them as a single CMAF fragment. `None` when the segment isn't in
/// the playlist window, is a gap or not yet resolved for this rendition, or its groups already
/// left the relay cache. After End or Restart, only the bound instance's retained media
/// drains; a replacement's media is never read under these rows.
/// drains; a replacement's media is never read under these rows. A denied referenced
/// broadcast returns its authorization error even when the requested row is absent.
pub async fn segment(&self, tag: &str, segment: u64) -> Result<Option<Bytes>> {
self.media.sync(&self.live);
self.authorize()?;
let Some(row) = self.live.row(tag, segment) else {
return Ok(None);
};
Expand All @@ -1132,7 +1171,7 @@ impl Rendition {

/// Fetch and transmux `row`'s frames on this rendition; see [`segment`](Self::segment).
pub(crate) async fn fetch(&self, row: &segments::Row) -> Result<Option<Bytes>> {
let binding = self.media.sync(&self.live);
let binding = self.authorize()?;
// Read the run before resolving the row (see `restart`), so an init decoded from its
// groups is never cached for a run that started while they were being fetched.
let run = self.run();
Expand All @@ -1155,7 +1194,7 @@ impl Rendition {
return Ok(None);
}

let Some((broadcast, track)) = self.track(&binding).await else {
let Some((broadcast, track)) = self.track(&binding).await? else {
return Ok(None);
};

Expand Down Expand Up @@ -1413,6 +1452,66 @@ mod tests {
producer
}

#[tokio::test]
async fn pending_reads_preserve_a_later_authorization_refusal() {
let origin = moq_tokio::origin::spawn();
let broadcast = origin.create_broadcast("room/live").unwrap();
let dynamic = origin.dynamic_route("room/secret", Default::default()).unwrap();
let source = moq_mux::Source::new(origin.consume(), broadcast.consume());
let mut config = VideoConfig::new(hang::catalog::VideoCodec::VP8);
config.broadcast = Some(moq_net::path::Relative::new("./secret").to_owned());
let rendition = Arc::new(Rendition::video("video".into(), &config, &source, Archive::default(), None).unwrap());
let request = dynamic.requested_broadcast().await.unwrap();
let other = origin.dynamic_route("room/hidden", Default::default()).unwrap();
config.broadcast = Some(moq_net::path::Relative::new("./hidden").to_owned());
let resolving = Arc::new(Rendition::video("video".into(), &config, &source, Archive::default(), None).unwrap());
let other_request = other.requested_broadcast().await.unwrap();
let row = |reference: &str| segments::Row {
index: 0,
segment: 0,
period: moq_net::Timestamp::ZERO,
reference: Arc::new((Kind::Video, reference.into(), reference.into())),
frames: hang::timeline::Position::group(0)..hang::timeline::Position::group(1),
keyframe: true,
duration: Duration::from_secs(1),
pts: moq_net::Timestamp::ZERO,
end: Duration::from_secs(1),
discontinuity: 0,
};
let own = row("video");
let segment = rendition.fetch(&own);
tokio::pin!(segment);
// The cursor waits on a window with no rows, which the refusal neither fills nor ends.
let mut cursor = rendition.segments();
let next = cursor.next();
tokio::pin!(next);
// This cursor waits for its own timeline to resolve another rendition's row, which the
// refusal never does either.
resolving.live.push(row("other"), None);
let mut resolving_cursor = resolving.segments();
let resolving_next = resolving_cursor.next();
tokio::pin!(resolving_next);
tokio::select! {
biased;
_ = &mut segment => panic!("the reference request has not been answered"),
_ = &mut next => panic!("the reference request has not been answered"),
_ = &mut resolving_next => panic!("the reference request has not been answered"),
_ = std::future::ready(()) => {}
}
request.reject(moq_net::Error::Unauthorized);
other_request.reject(moq_net::Error::Unauthorized);
for read in [
segment.await,
next.await.map(|_| None),
resolving_next.await.map(|_| None),
] {
assert!(matches!(
read,
Err(crate::Error::Mux(moq_mux::Error::Moq(moq_net::Error::Unauthorized)))
));
}
}

// End may land while capture's query installs a dynamically served track. The handle must
// stay on that instance rather than repeat a lookup End refuses, or its retained media is lost.
#[test]
Expand Down
16 changes: 14 additions & 2 deletions rs/moq-hls/src/export/segments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,8 @@ impl Consumer {
///
/// Once a timeline fails (the reference's, or this rendition's own), the cursor drains the
/// segments it still covers, then fails every call with
/// [`TimelineFailed`](crate::Error::TimelineFailed), as [`Rendition::playlist`] does.
/// [`TimelineFailed`](crate::Error::TimelineFailed), as [`Rendition::playlist`] does. A
/// denied referenced broadcast fails every call with its authorization error.
///
/// Waits for the next segment to resolve on this rendition, then FETCHes and transmuxes its
/// frames. A segment whose groups already left the relay cache (or that is a gap for this
Expand All @@ -542,7 +543,13 @@ impl Consumer {
let Some((row, resets)) = kio::wait(|waiter| self.poll_next(waiter)).await? else {
return Ok(None);
};
kio::wait(|waiter| self.rendition.poll_resolved(waiter, &row)).await;
kio::wait(|waiter| {
// The refusal settles the binding, not this rendition's timeline, so watch both.
self.rendition.poll_media(waiter);
self.rendition.authorize()?;
self.rendition.poll_resolved(waiter, &row).map(Ok::<_, crate::Error>)
})
.await?;
// This rendition's timeline failed before covering the row, so no later row resolves.
if let Some(err) = self.rendition.own_failure()
&& self.rendition.is_failed(&row)
Expand All @@ -565,6 +572,11 @@ impl Consumer {

fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll<Result<Option<(Row, u64)>>> {
self.poll_feed(waiter);
// A refused binding never held media, so there is nothing to drain. `poll_feed` registered
// the wake for a refusal that arrives while the window waits for rows.
if let Err(err) = self.rendition.authorize() {
return Poll::Ready(Err(err));
}
Comment thread
kixelated marked this conversation as resolved.
Resolved
let poll = self
.state
.poll(waiter, |state| match state.next_after(self.after.as_ref()) {
Expand Down
Loading
Loading