avb-stream 0.2.1

AVTP audio streams: AAF and AM824 talkers and listeners paced on gPTP time, CRF media clocks, with audio through cpal
Documentation
# avb

Rust crates for AVB and Milan: discovering, describing and connecting
IEEE 1722.1 entities, reserving streams, raw Ethernet on Linux, macOS and
Windows, and sending and receiving AVTP audio on gPTP time. They are the
networking under [triib](https://github.com/scrambletoolsllc/triib), an
ATDECC controller, and [plaa](https://github.com/scrambletools/plaa), a
media player, and are meant for other apps with
very different constraints too: a blocking command line tool, an async
daemon, a GUI, or firmware on a microcontroller.

| Crate | What it does |
|---|---|
| `atdecc` | IEEE 1722.1-2021 with Milan 1.3, MAAP and AVB Lite's vendor unique messages: frame codecs, controller state machines (discovery, AECP commands, enumeration, ACMP, unsolicited notifications), entity state machines (AEM responder, Milan listener binding, ACMP talker) and the entity model as data |
| `avb-lite` | The AVB Lite profile: CVU SRP's declarations, admission and escalation; gptp weighs the fallback |
| `avb-mrp` | IEEE 802.1Q MRP (applicant, registrar, LeaveAll, periodic timers), MSRP and MVRP, and a port's participant shared between programs on one computer; with the `bridge` feature, a bridge's MSRP and MVRP: attribute propagation between ports, stream admission, and the forwarding and idle slope changes that follow |
| `avb-net` | Raw Ethernet for AVB: `raw-ether`'s interfaces, frames, multicast membership, hardware time stamps, the PTP hardware clock, link settings and captures, under the paths AVB crates use, and whole frames for AVTP streams |
| `avb-stream` | AVTP audio: AAF and AM824 talkers and listeners paced on gPTP time, CRF media clock streams, and audio to and from the computer's devices through cpal |

## Rules for the protocol crates

`atdecc`, `avb-mrp` and `avb-net` keep to these:

1. **Sans-I/O core.** No sockets, threads, clock or runtime in the protocol
   code. The caller feeds received frames and the current time
   (`handle_frame(now, bytes)`, `handle_timeout(now)`) and drains frames
   to send, events and the next deadline (`poll_transmit`, `poll_event`,
   `poll_timeout`). Commands return an ID and complete as an event. This
   fits a blocking loop, tokio, embassy or a firmware main loop alike.
2. **`no_std`.** Codecs need neither std nor alloc: fixed-size PDUs decode
   into plain values, variable ones as views over the received bytes, and
   both encode into the caller's buffer. Reserved values are kept, so a
   frame decodes and encodes back unchanged. State machines need `alloc`
   only. The `std` feature (default) adds the `avb-net` transports and a
   small blocking driver for simple apps.
3. **No dependencies beyond each other.** `avb-net` depends only on
   `raw-ether`, in gptp's repository, which holds the platform code
   (`libc` on Linux and macOS, `windows-sys` on Windows), and on `libc`
   for AVTP streams on Linux. No serde, no logging framework, no async
   runtime.
4. **Never panic on input.** Malformed frames are errors.
5. **Checked in CI**: each crate tested on its own, and built with
   `--no-default-features` for `riscv32imac-unknown-none-elf` (the
   ESP32-C6).

`avb-stream` is for computers: it needs std, threads and an audio device.

## Using them

They are on crates.io:

```toml
[dependencies]
atdecc = "0.1"
```

To change them together with an app that uses them, point the app at a
local checkout from its own git-ignored `.cargo/config.toml`:

```toml
[patch.crates-io]
atdecc = { path = "../avb/crates/atdecc" }
avb-lite = { path = "../avb/crates/avb-lite" }
avb-mrp = { path = "../avb/crates/avb-mrp" }
avb-net = { path = "../avb/crates/avb-net" }
avb-stream = { path = "../avb/crates/avb-stream" }
gptp = { path = "../gptp/crates/gptp" }
raw-ether = { path = "../gptp/crates/raw-ether" }
```

## Platform needs

- **Linux:** raw Ethernet needs `CAP_NET_RAW`, but only to open a
  socket. Rather than granting it to the app, which then runs as a
  process others may not look into (the desktop portals refuse it file
  dialogs and screen sharing; debuggers and crash reports pass it by),
  ship a tiny helper whose `main` calls `avb_net::broker::serve()`, grant
  the capability to that alone (`sudo setcap cap_net_raw+ep <helper>`),
  and at start `install` a `Broker::start(<helper>)`: every raw socket
  `avb-net` and `avb-stream` open then comes from the helper, passed over
  a Unix socket. `raw-ether`'s `netraw` example is such a helper. gPTP
  comes from gptpd, in gptp's repository; the stream crates read the
  grandmaster's time from the record it keeps for the interface.
- **macOS:** raw Ethernet needs read and write access to `/dev/bpf*`.
- **Windows:** raw Ethernet needs [Npcap]https://npcap.com.

## Building

```sh
cargo test --workspace
cargo test -p avb-mrp --features bridge
cargo build -p avb-net -p atdecc -p avb-mrp --no-default-features --target riscv32imac-unknown-none-elf
cargo build -p avb-mrp --no-default-features --features bridge --target riscv32imac-unknown-none-elf
```

On Linux, `avb-stream` builds against ALSA's headers (`libasound2-dev`,
`alsa-lib`).

## License

MIT or Apache-2.0, at your option.