downlink

ESP32-S3 Icecast player that streams Ogg Opus to a PCM5102A I2S DAC

readme · slastra/downlink

ESP32-S3 Icecast player that streams Ogg Opus to a PCM5102A I2S DAC.

ESP-IDF firmware that plays an Icecast Ogg Opus stream (RUMP's Opus codec) through a PCM5102A I2S DAC into an amp. The listening end of RUMP: RUMP is the uplink, this is the downlink.

Features

  • Joins live Icecast mounts mid-stream and reconnects on its own
  • Buffers in audio time, not bytes, and corrects for clock drift between source and board
  • Captive portal for WiFi and the stream URL
  • Remote management over MQTT, outbound only, so it works behind any NAT
  • Announcements over ducked music
  • OTA updates from GitHub Releases, with rollback
  • Crash reports that decode to source lines

Hardware

  • ESP32-S3-WROOM-1U N16R8 dev board, the same one as tspl-station (YD-ESP32-S3 layout): 16 MB flash, 8 MB octal PSRAM, two USB-C ports.
  • PCM5102A breakout (the common purple GY-PCM5102 board).
  • Line out to an amp.

Wiring

PCM5102A pin ESP32-S3 Notes
VIN 5V The board's LDOs take 3.3–5 V. 5 V gives the analog rail headroom.
GND GND
BCK GPIO12 bit clock, 64 fs (3.072 MHz at 48 kHz)
DIN GPIO13 serial data
LCK / LRCK GPIO14 word select, 48 kHz
SCK GND No MCLK. The DAC's PLL runs off BCK.
FMT GND I2S (Philips) format
XSMT 3V3 soft-mute off (high = play)
FLT GND normal-latency filter
DEMP GND de-emphasis off
LOUT / ROUT / AGND amp input or the board's 3.5 mm jack

On the purple board, the four config pins also have solder pads on the back: H1 = FLT, H2 = DEMP, H3 = XSMT, H4 = FMT. Bridge each to L or H to match the table instead of wiring them. XSMT must be high. A floating or low XSMT gives silence with everything else looking healthy.

All three I2S GPIOs can be changed in menuconfig. Stay off these pins:

  • 0 (BOOT, used for the portal)
  • 3, 45, 46 (straps)
  • 19, 20 (USB)
  • 26–37 (flash and octal PSRAM)
  • 43, 44 (UART0 console)
  • 48 (WS2812)

ESP32-S3 SuperMini

The SuperMini (4 MB flash, 2 MB quad PSRAM, one USB-C) works too, with I2S on GPIO9/10/11. docs/supermini.md has its wiring and build command.

USB ports (N16R8)

Flash and monitor through the UART bridge port (UART/COM, /dev/ttyUSB0 or ttyACM0 depending on the bridge chip). The console is on UART0, same as tspl-station. The native USB port is unused.

Install

Requirements

ESP-IDF v6.1.

Build and flash

. ~/Projects/Esp/esp-idf-v6.1/export.sh      # bash; from fish: bash -c '. … && idf.py …'
cd ~/Projects/Esp/downlink
cp sdkconfig.defaults.local.example sdkconfig.defaults.local   # WiFi, stream URL, broker; gitignored
idf.py set-target esp32s3
idf.py build
idf.py -p /dev/ttyUSB0 flash monitor

sdkconfig.defaults is only applied when sdkconfig doesn't exist. After editing either defaults file, rm sdkconfig (or use idf.py menuconfig).

Image: about 1.2 MB against 3 MB OTA slots (1.9 MB on the SuperMini). dependencies.lock is committed and the registry components are pinned, so a later build uses the same versions.

Usage

RUMP side

Set RUMP's codec to Opus (Server tab). Vorbis is its default. Both arrive as application/ogg, so the stream task reads the codec magic from the first page and refuses Vorbis before a byte reaches the decoder:

E stream: stream is Ogg Vorbis, not Opus; this build decodes Ogg Opus only (set RUMP's codec to Opus)

It then retries at the backoff ceiling (30 s), so switching RUMP to Opus recovers on its own.

WiFi and the captive portal

components/netlink and components/provision come from tspl-station. netlink stores up to 8 networks in NVS and picks the best in range by priority, then RSSI. It re-knocks on band-steering rejections, roams below −70 dBm, and backs off up to 30 s.

The portal opens an open AP downlink-<id> at http://192.168.4.1/ when:

  • no known network has been seen for 90 s, or
  • BOOT is held for 3 s (hold again to close it).

The page lists nearby networks and takes the password and stream URL. Leaving the password blank for a saved network keeps its stored password. Under Advanced are the priority and device id. Saving reboots the board.

Unattended, it closes after 5 minutes and netlink gets a full 90 s to find a known network before it can reopen. That way a router reboot at a remote site costs a few minutes, not an outage until someone power-cycles the board (tspl-station's 30-minute portal and immediate reopen did that).

Remote management

A board publishes a retained status to downlink/<device id>/status and takes JSON commands on downlink/<device id>/cmd:

mosquitto_sub -h <broker> -u … -P … -t 'downlink/#' -v
mosquitto_pub -h <broker> -u … -P … -t downlink/downlink/cmd -m '{"cmd":"url","url":"http://host:8000/mount"}'

Status LED

The onboard WS2812 (GPIO48 on both boards) shows the first state that applies. Brightness is DL_LED_BRIGHTNESS (15%).

LED State
magenta pulse / solid portal open / a phone is on it
red no WiFi
fast blue pulse downloading an update
white pulse announcing
slow red pulse the mount isn't Opus (switch RUMP's codec)
yellow pulse WiFi up, stream not connected (retrying)
blue connected, buffering
green playing

Configuration

idf.py menuconfig, menu downlink:

Option Default
DL_DEVICE_ID downlink DHCP hostname, portal AP downlink-<id>, MQTT topic segment; give each board its own
DL_BOARD n16r8 supermini in that variant; reported in the status
DL_STREAM_URL http://icecast.example:8000/stream placeholder; set yours in sdkconfig.defaults.local
DL_I2S_BCK_GPIO / WS / DOUT 12 / 14 / 13 9 / 11 / 10 on the SuperMini
DL_VOLUME 70 digital gain, 0.5 dB per step below 100 (70 = −15 dB, 0 = mute)
DL_RING_KB 512 compressed ring in PSRAM: 30 s of loud 128 kbps audio, far more of quiet
DL_PREBUFFER_MS 2000 audio buffered before playing, and after an underrun
DL_BUFFER_MS 4000 buffer depth held against clock drift
DL_BUFFER_MAX_MS 12000 above this, catch up (decode and discard) to DL_BUFFER_MS
DL_STALL_S 10 no bytes for this long means reconnect
DL_MAX_BACKOFF_S 30 reconnect backoff ceiling

Stream URL, volume, WiFi (NETLINK_SSID0/PASS0, under Network link) and the broker (UPLINK_*, under Uplink) are all seeds: copied to NVS on first boot, after which NVS is the truth and the portal or MQTT commands change them. So an update to an image built without your local file (a future public OTA release) keeps a board's settings. To re-seed a board from the build, erase NVS (idf.py erase-flash, then flash).

Not yet

  • MP3 via esphome/micro-mp3, with the decoder chosen by Content-Type.
  • MQTT over TLS (8883). Plain 1883 sends the broker password in the clear, which matters once the board is on someone else's network.

How it works

Icecast ──HTTP──▶ stream task ──▶ 512 KB ring ──▶ player task ──▶ I2S ──▶ PCM5102A ──▶ amp
                  (core 0)        (PSRAM)         (core 1)
                  ICY strip,                      OggOpusDecoder,
                  Ogg page sniff                  volume

The DAC takes raw PCM. The ESP decodes, so the codec only has to match what the Icecast mount serves. This build decodes Ogg Opus only. MP3 support can come later from esphome/micro-mp3, chosen by Content-Type.

The player writes a 10 ms block to the DAC every 10 ms, so a rebuffer or a reconnect never stalls the output. Buffer depth is read from the Ogg granule positions, which is what makes drift correction and backlog catch-up work on a variable-bitrate stream. docs/architecture.md covers reconnects, mid-stream joins, drift, self-rescue, metadata and the serial log.

Development

Host tests for the plain-C parts (the Ogg page walker, the mixer, and the buffer-depth controller over simulated days):

make -C test/host

tools/fake-icecast serves an Ogg Opus file the way Icecast serves a live mount: header pages, then a byte-sized burst from a real-time "live" position, then real-time pacing, with an optional forced disconnect. Point a board at it with the MQTT url command to test buffering against VBR swings and joins during silence, without a real source:

tools/fake-icecast programme.opus --drop-at 450    # http://<host>:8765/stream

License

MIT © 2026 Shaun Lastra. See LICENSE.