teddycloud icon indicating copy to clipboard operation
teddycloud copied to clipboard

Live-Tonie: switch what a Tonie plays without lifting it (live:// channels)

Open jefvanhoyweghen opened this issue 2 weeks ago • 0 comments

Is your feature request related to a problem? Please describe.

What a Tonie plays is fixed by its content. To change it, you have to lift the figure, change the source in the web UI and place it again. With small children, a parent or a home automation often wants to choose what plays from a phone or a script while the figure stays on the box: switch to another story, skip a chapter, pause at bedtime, stop. Children also want to pick a story themselves, without being able to change any settings.

Describe the solution you'd like

This is already implemented and working. The backend changes, the web UI page and the Kids Kiosk described below exist and run on my own teddyCloud with a Toniebox. I'm opening this issue first, as asked for bigger features, so the approach can be discussed before I open the PRs.

A Live-Tonie: a tag whose source is a live channel (live://1 to live://8). The box streams the channel like any other stream, and teddyCloud decides at runtime what goes into it. Switching takes a few seconds, and the figure stays on the box. A live channel only plays audio that is already in the user's own teddyCloud (the content of their Tonies and their library files). It downloads nothing and does not contact the cloud.

How the implementation works

  • Source type. live://N is a new source scheme, next to lib:// and stream URLs. live_channel_of() parses it. Live tags are treated like streams (live: true, resumeBehavior: alwaysReset).
  • Channel state. A small module (live_channel.c) keeps the state of 8 channels in memory: the selected file, the source/tag it came from, paused, ended, and the listener count. A generation counter tells listeners to reopen when the selection changes.
  • Audio path. The stream encoder already reads PCM from an ffmpeg pipe. For live:// sources, toniefile.c instead gets a PCM reader from live_open(). Each listening box gets its own feeder task, which decodes the selected file with ffmpeg (skipping the 4 KiB TAF header) and writes s16le/48 kHz/stereo in real time, about one second ahead.
  • Never-ending stream. When nothing is selected, playback is paused or the title has ended, the channel plays silence. The stream never ends, so the box keeps its connection.
  • CBR for live sources. With VBR, Opus encodes silence in a few bytes per frame. The stream file then grows much slower than real time and the box reports a download error. Live sources are encoded as CBR.
  • Chapters. When a TAF is selected, the channel reads its chapter table (track_page_nums in the header, start times from the Ogg granule positions). Jumping to a chapter restarts the decoder from that chapter's block: ffmpeg gets the header block followed by the audio from that block on, the same layout as a complete TAF file. The feeder tracks the playback position.
  • Lifting stops the channel. When RTNL reports that a Live-Tonie was removed, its channel goes back to silence. Placing the figure again does not resume an old selection.

API

  • GET /api/live: lists the channels with what they play, paused/ended, the number of listening boxes, the position and length, and the chapters (start time and title from tonies.json when known).

  • POST /api/live/set?channel=N[&overlay=ID] with a form body:

    • action=play&tag=<ruid>: plays the content of one of the user's Tonies stored in teddyCloud
    • action=play&source=lib://…: plays a library file
    • action=pause|resume|stop
    • action=chapter&chapter=N: jumps to chapter N
    • action=next|prev: next chapter; prev restarts the current chapter, or goes to the previous one within its first 3 seconds

    Tags and paths are resolved and validated on the server, so only files inside the library and content folders can be selected.

Web UI

A page under Tonies → Live-Tonie in teddycloud_web. From it you can turn a Tonie into a Live-Tonie (assigns a free channel), pick one of your other Tonies or a library file to play on it, pause, resume and stop, jump to a chapter or skip to the next or previous one, see whether a box is listening, and turn it back into a normal Tonie. Backends without the endpoints answer 404, and the page then shows a notice.

Kids Kiosk

A standalone plugin for teddyCloud's plugin folder (data/www/plugins/KidsKiosk): a full-screen remote for a tablet or an old phone. The child taps one of the family's Tonies and the Live-Tonie plays it. While something plays, the screen shows pause and stop and, for Tonies with chapters, buttons to skip back and forward, with a dot per chapter to show how far the story is.

  • Stopping or switching to another story needs a short hold, so a stray touch changes nothing.
  • The settings (which box, which Live-Tonie, which Tonies the child sees) sit behind a PIN. The PIN only keeps children out. It is stored hashed in the browser and is not a security boundary.
  • Plain HTML, CSS and JavaScript, with no build step, no external scripts and no tracking. It only uses the /api/live endpoints and the existing tag and box lists.
  • German and English, following the language chosen in the web UI.

Proposed PRs

The code for all of these is written. I can open them as soon as the approach is agreed, or adapt it first to what you prefer.

  1. teddyCloud: live channels, chapters, /api/live endpoints, CBR for live sources, stop on removal
  2. teddycloud_web: the Live-Tonie page (depends on 1)
  3. Kids Kiosk plugin (depends on 1), if you want it in the project. Otherwise I can publish it separately.

Separately, I found a double free in toniefile.c while working on this. I would send that as its own small PR, independent of this feature.

Describe alternatives you've considered

  • Rewriting the tag's source and asking the box to reload. The box only fetches content again after lifting or a freshness check, so the switch is neither immediate nor reliable.
  • An external streaming server (e.g. Icecast) as a stream source. This works for radio, but every user would need extra infrastructure, and it can't play teddyCloud's own library or other tags' content without re-implementing teddyCloud's path handling.
  • One shared decoder per channel instead of one per box. Boxes on the same channel would be in sync, but this needs fan-out and buffering for boxes that join late. Per-box decoders are simpler. Two boxes on one channel hear the same audio, just not in sync.

Questions for the maintainers

  • Is live://N an acceptable scheme, or would you prefer this as a variant of the existing stream source?
  • Is 8 channels with state in memory fine (lost on restart), or should the selection persist?
  • Endpoint naming and shape: /api/live and /api/live/set with form bodies, matching the existing /api/… style. Is that OK?
  • Is it OK for the stop-on-removal to hook into the RTNL tag-removed event? Without RTNL, the channel keeps its selection.
  • Chapter jumps currently pipe the header block and the chapter data through a shell (tail/head) into ffmpeg. Would you prefer seeking inside teddyCloud and feeding ffmpeg from a C reader instead?
  • Would you like the Kids Kiosk in one of the teddyCloud repositories? If so, where should plugins like this live?
  • Is ffmpeg as a hard requirement for live channels acceptable? It's already required for stream sources.

Additional context

Tested on a Toniebox: switching between Tonies and library files while the Live-Tonie stays on the box, and pause/resume/stop.

jefvanhoyweghen avatar Nov 21 '26 12:11 jefvanhoyweghen