Skip to content

Commit

Permalink
feat: add moq-sub to subscribe to media from moq relays
Browse files Browse the repository at this point in the history
  • Loading branch information
Frando committed Feb 9, 2024
1 parent b4b389c commit 2b2f2cc
Show file tree
Hide file tree
Showing 10 changed files with 438 additions and 16 deletions.
48 changes: 36 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[workspace]
members = ["moq-transport", "moq-relay", "moq-pub", "moq-api", "moq-clock"]
members = ["moq-transport", "moq-relay", "moq-pub", "moq-api", "moq-clock", "moq-sub"]
resolver = "2"
21 changes: 21 additions & 0 deletions dev/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ You should have it installed already if you're a video nerd, otherwise:
brew install ffmpeg
```

### moq-sub

You can use `moq-sub` to subscribe to media streams from a MoQ relay and pipe them to the standard output. By piping the
command to a video player, e.g. `ffplay` or `mpv`, you can play a MoQ broadcast natively.

Currently, `moq-sub` simply dumps all received segments of the first video and the first audio track directly to `stdout`.

### moq-api

`moq-api` uses a redis instance to store active origins for clustering.
Expand All @@ -68,6 +75,7 @@ We run the redis instance via a container automatically as part of `dev/api`.
./dev/cert
./dev/relay
./dev/pub
./dev/sub
```

They will each print out a URL you can use to publish/watch broadcasts.
Expand Down Expand Up @@ -98,13 +106,26 @@ By default, the broadcast name is `dev` but you can overwrite it with the `NAME`

> Watch URL: https://quic.video/watch/dev?server=localhost:4443
By default, the audio track is exlcluded, because the web player does not yet support audio playback. To include audio,
set the `AUDIO=1` env var.

If you're debugging encoding issues, you can use this script to dump the file to disk instead, defaulting to
`dev/output.mp4`.

```bash
./dev/pub-file
```

### moq-sub

The following command subscribes to a stream from a MoQ relay and plays it with `ffplay`.
By default, the URL is `https://localhost:4433/dev`, so it will play the stream published with `dev/pub` to the relay
started with `dev/relay`. You can change the broadcast name by setting the `NAME` env var.

```bash
./dev/sub
```

### moq-api

The following commands runs an API server, listening for HTTP requests on `http://localhost:4442` by default.
Expand Down
10 changes: 7 additions & 3 deletions dev/pub
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ URL="${URL:-"https://$ADDR/$NAME"}"
# Default to a source video
INPUT="${INPUT:-dev/source.mp4}"

# Only inlcude audio track if AUDIO=1 is set
# TODO enable audio by default again once fixed.
if [[ ! -v AUDIO ]]; then
NO_AUDIO=1
fi

# Print out the watch URL
echo "Watch URL: https://quic.video/watch/$NAME?server=$ADDR"

# Run ffmpeg and pipe the output to moq-pub
# TODO enable audio again once fixed.
ffmpeg -hide_banner -v quiet \
-stream_loop -1 -re \
-i "$INPUT" \
-c copy \
-an \
-c copy ${NO_AUDIO:+-an} \
-f mp4 -movflags cmaf+separate_moof+delay_moov+skip_trailer \
-frag_duration 1 \
- | cargo run --bin moq-pub -- "$URL" "$@"
25 changes: 25 additions & 0 deletions dev/sub
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash
set -euo pipefail

# Change directory to the root of the project
cd "$(dirname "$0")/.."

# Use debug logging by default
export RUST_LOG="${RUST_LOG:-debug}"

# Connect to localhost by default.
HOST="${HOST:-localhost}"
PORT="${PORT:-4443}"
ADDR="${ADDR:-$HOST:$PORT}"

# Generate a random 16 character name by default.
#NAME="${NAME:-$(head /dev/urandom | LC_ALL=C tr -dc 'a-zA-Z0-9' | head -c 16)}"

# JK use the name "dev" instead
# TODO use that random name if the host is not localhost
NAME="${NAME:-dev}"

# Combine the host and name into a URL.
URL="${URL:-"https://$ADDR/$NAME"}"

cargo run --bin moq-sub -- "$URL" "$@" | ffplay -
49 changes: 49 additions & 0 deletions moq-sub/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[package]
name = "moq-sub"
description = "Media over QUIC"
authors = []
repository = "https://github.com/kixelated/moq-rs"
license = "MIT OR Apache-2.0"

version = "0.1.0"
edition = "2021"

keywords = ["quic", "http3", "webtransport", "media", "live"]
categories = ["multimedia", "network-programming", "web-programming"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
# iroh = ["iroh-net"]

[dependencies]
moq-transport = { path = "../moq-transport" }

# QUIC
quinn = "0.10"
webtransport-quinn = "0.6.1"
url = "2"

# Crypto
rustls = { version = "0.21", features = ["dangerous_configuration"] }
rustls-native-certs = "0.6"
rustls-pemfile = "1"

# Async stuff
tokio = { version = "1", features = ["full"] }

# CLI, logging, error handling
clap = { version = "4", features = ["derive"] }
log = { version = "0.4", features = ["std"] }
env_logger = "0.9"
mp4 = "0.13"
anyhow = { version = "1", features = ["backtrace"] }
serde_json = "1"
rfc6381-codec = "0.1"
tracing = "0.1"
tracing-subscriber = "0.3"

[build-dependencies]
clap = { version = "4", features = ["derive"] }
clap_mangen = "0.2"
url = "2"
10 changes: 10 additions & 0 deletions moq-sub/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# moq-sub

A command line tool for subscribing to media via Media over QUIC (MoQ).

Takes an URL to MoQ relay with a broadcast name in the path part of the URL. It will connect to the relay, subscribe to
the broadcast, and dump the media segments of the first video and first audio track to STDOUT.

```
moq-sub https://localhost:4443/dev | ffplay -
```
1 change: 1 addition & 0 deletions moq-sub/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod media;
Loading

0 comments on commit 2b2f2cc

Please sign in to comment.