Aller au contenu

ADR-0073: Cameras in video — the enlarged camera plays Home Assistant's own MJPEG stream, no add-on, off by default

Page en anglais

Cette page n'existe qu'en anglais.

Status: Proposed (2026-11-09; draft PR, not tried on a tablet nor with a live camera when written: every figure below marked « not measured » is an estimate or a reading of the sources). Update the same day: tried on the author's Tab5 with one ESPHome camera; the first measurements, and what limited the rate, are in « Update » at the end. The sections above are left as written. Date: 2026-11-09

Context

A tester (discussion #278) pointed at a project that shows his cameras live on the same tablet through a Home Assistant add-on. The author's rule for this firmware: no add-on and no server on the Home Assistant side, nothing that goes beyond what the ESP32-P4 and its hardware JPEG decoder carry, no sound. ADR-0049 had rejected /api/camera_proxy_stream because, with online_image, a never-ending response would have held the main loop; since 2026-10-10 the images are downloaded and decoded by a task off the main loop (ADR-0049 « Off the main loop »), which removes that objection.

What Home Assistant 2026.11.0 does (source read, tag 2026.11.0):

  • /api/camera_proxy_stream/<entity_id>?token=<token> checks the same token as the still image, once, at connection: a stream already open is not cut when the token rotates (every 5 minutes). It ignores width and height; its only parameter, interval (0.5 s at least), forces the still-image loop.
  • What it sends depends on the camera integration (Camera.handle_async_mjpeg_stream): by default (Reolink, UniFi Protect, generic, …) the still image in a loop, two images per second at most, at the camera's full size, an identical image not re-sent; ffmpeg, ONVIF, TP-Link (the SD stream), Ring, Amcrest… a real MJPEG made by ffmpeg at the camera's size and rate (HA declares boundary=ffserver while ffmpeg writes --ffmpeg: measured with ffmpeg 9.0.2, Content-type / Content-length headers); ESPHome cameras push their frames; the mjpeg platform copies the camera's own stream, its own boundary, sometimes without Content-Length. No fallback on HA's side: a WebRTC-only camera answers 200 with no image.
  • ESP-IDF 5.5.5 (esp_http_client.c): esp_http_client_fetch_headers() returns 0 for a response without length and switches to chunked; esp_http_client_read() loops until the buffer it was given is full or the timeout runs out, and returns -ESP_ERR_HTTP_EAGAIN when nothing arrived in time; with esp_http_client_set_timeout_ms(client, 0) a read does not wait at all.

Decision

  • One stream, the camera shown enlarged, off by default. Blueprint input cameras_video (boolean, unchecked): when checked, each online camera with an image gets a 5th field 1 (« nom|image|pièce|depuis|1 »; nothing is added when unchecked, the answer is the one of before byte for byte; an older firmware reads four fields and ignores the fifth). Reason for the default: nothing could be seen on a tablet when this was written, and the only camera of the author's Home Assistant was offline.
  • URL: the one the blueprint already gave. camera_flux_url() (tab5_parse, pure) swaps /api/camera_proxy/ for /api/camera_proxy_stream/ in the image path (token kept, base address as for the still image), and refuses an image path that is not a camera proxy or already carries width= / height= (the stream ignores them): that camera then stays on still images. No Home Assistant entity in the firmware, no HA action (ADR-0025 holds), no change to the service variables (contrat/contrat.yaml unchanged).
  • The loader reads the stream (Tab5/ecran/tab5_cameras_charge.cpp, the same task, the same state machine): camera_charge_lancer_flux(url) opens the stream (or keeps the open one: the URL is compared up to the ?, so a token that rotated does not reconnect) and answers with the most recent image received: it reads, without waiting, everything that already arrived, sorts the images out in the receive buffer, throws the older ones away without decoding them, and only waits (up to 12 s, once) when no complete image is there. No read asks for more than 32 KiB, and an image that is ready is returned after 250 ms of reading even if data keeps coming (a saturated link): esp_http_client_read() otherwise returns only once its buffer is full. A flow faster than the screen therefore never builds a delay; the hardware decoder (baseline JPEG, 1280 × 736 at most, as for the still images) decodes one image per request, into the same double buffers.
  • Pure parsing (Tab5/socle/tab5_flux_mjpeg.h, header only, tested on a PC): flux::extraire() finds the next complete JPEG in a buffer. It never trusts the boundary Home Assistant announces: a -- line, the headers up to the blank line, Content-Length (case-insensitive) or, without it, the first \r\n-- that follows an end-of-JPEG marker (FF D9). A body that is not a JPEG, an image over 1 MiB, an over-long line: refused (the stream is closed).
  • Screen (Tab5/ecran/tab5_cameras.cpp): only the camera shown enlarged plays (never the mosaic, never the neighbours: while it plays the task and the connection are the stream's, so no neighbour prefetch). The next image is asked as soon as the previous one is painted, at least 50 ms apart (kVideoMs; the screen cannot follow faster: a full image costs about 60 ms of drawing), polled every 10 ms. The first image repaints the whole popup; the following ones only change the image and the time (a full repaint rewrites every chip's style and invalidates the room column each time, LVGL not comparing). The stream is closed when the camera is no longer the shown one, the view is the mosaic, the video is paused or the popup closes (Home Assistant then stops its ffmpeg).
  • Fallback to still images, automatic. Any failure of the stream (status other than 200, not an image stream, nothing in 12 s, image too large, progressive JPEG, closed by Home Assistant) closes it, does not count as a failed still image (the camera does not turn « Hors ligne »), and the camera is on still images right away; the video is tried again after 1 minute, then 5, then 30 (kVideoEchecsDelaisMs), and reset by a frame of the stream (a still image does not count), a reopening of the popup or the camera coming back online. A camera at full HD in its main stream therefore ends on still images, which Home Assistant scales for the tablet.
  • Not a new activity timer: the existing 45 s return to the home page still applies (the frames do not count as a touch); a long watch needs a touch, or a later decision.

Rejected

  • A Home Assistant add-on or a restream server (the other project's way): against the rule, one more thing to install and keep.
  • Several streams (the mosaic playing): one connection and one decoder per tile, 4 × the Wi-Fi and the PSRAM, for 476 × 266 tiles; the mosaic keeps its still images.
  • Asking HA for a smaller or slower stream: impossible from the URL (width, height ignored); only the integration's own settings (ffmpeg arguments) can, on the Home Assistant side.
  • WebRTC / HLS / H.264: no decoder for them on this firmware, and WebRTC needs signalling through Home Assistant's websocket.
  • The ESP32-P4 pixel-processing accelerator to scale a full-HD image: kept out; an image over 1280 × 736 falls back to still images.

Consequences and what is not measured

  • Gain depends on the camera's integration: real video for ESPHome, mjpeg, and ffmpeg / ONVIF / TP-Link in SD; at most 2 images per second, at full size, for the default integrations (a still image every 5 s before); no gain, and a still-image fallback, for a full-HD main stream.
  • Memory: two decoded buffers (1.04 MB each at 960 × 544, 0.47 MB at 640 × 368) as with still images, plus the receive buffer (128 KiB, grown by doubling to 2 MiB + 8 KiB for an image kept and one arriving), all given back at closing.
  • Per-image log line every 30 decoded images (tab5.cameras, flux : N images en … ms (… par seconde), … jetées, lecture … ms et décodage … ms par image): the first measurement to read after a try.
  • Not measured (no tablet, no live camera): real frame rate and delay, worst main-loop time (217 ms with still images; 80 to 150 ms expected at 5 to 10 images/s), Wi-Fi throughput over the P4-C6 link, behaviour behind a reverse proxy that buffers a response (https), CPU cost of the task's 5 ms read polling while it waits for an image.

Update (2026-11-09, same day): first measurements — the camera sets the rate

Tried on the author's Tab5 (ST7121 revision) with the enlarged camera set to video, one camera: an M5Stack AtomS3R-CAM (ESP32-S3, GC0308 sensor, 640 × 480, ESPHome 2026.9.0), which has no hardware JPEG encoder. The author saw the picture and found it correct. What was measured, and with what:

  • On the tablet (log line flux : read for 70 s, before any change to the camera): 1.7 then 1.5 images per second, 0 images dropped, 3 to 4 ms of decoding per image, 514 to 591 ms of reading per image, « pile libre 9816 » in the log. The hardware decoder and the stack are not the limit; the rate follows the source.
  • Where the time goes (read-only): Home Assistant's stream read from a PC gave 1.51 images per second (median gap 576 ms), the camera read directly through its native API about 1.45 on its first 8 images, the tablet 1.5 to 1.7: the same rate along the whole chain. On the camera, the JPEG is made in software in the main loop (esp32_camera), one frame buffer, and the next image only starts once the previous one has been fully sent; its loop time was 315 ms of a cycle of about 660 ms.
  • Changes tried on the camera (same picture, 30 s per measure, PC → Home Assistant's stream; the camera is also used by an automation of the author's, so each trial was backed up and could be reversed): a TCP send buffer of 32 768 bytes (CONFIG_LWIP_TCP_SND_BUF_DEFAULT, 5 760 by default) and two frame buffers (frame_buffer_count: 2): 2.79 images per second, then 2.55 and 2.57 when put back (against 1.51 and 1.43 before); median gap 356 ms, JPEG about 46 KB. The loop time is then almost the whole cycle, so what is left is the software JPEG encoding. With compiler_optimization: PERF in addition: 2.76, no gain, flash 51.2 → 55.8 %: dropped. With jpeg_quality: 30 in addition: 2.95 (+6 %), JPEG about 35 KB, a softer picture: dropped. Free internal memory unchanged (205 KB), free PSRAM 7.77 → 7.15 MB (the second buffer), internal temperature 56 → 59 °C during the trials.
  • Smaller pictures (the two settings above kept, resolution changed, 30 s per measure, two measures each): CIF (400 × 296, JPEG about 24 KB) 6.0 and 6.3 images per second; QVGA (320 × 240, JPEG about 18 KB) 9.3 and 8.9 (the camera's own cap is max_framerate: 10 fps), with a loop time of 118 ms in one reading. The camera was put back at 640 × 480 after the trials (sharper picture, 2.5 to 2.8 images per second); sharpness against rate is a matter of taste and is not settled here. No picture from the trials is published.

Not measured, still: the tablet again after the change on the camera (the Tab5 was no longer running this firmware when the camera was changed); the delay between the scene and the screen; the worst main-loop time while playing; the fallback when the camera drops; the https reverse proxy; the cost of LVGL scaling a 640 × 480 picture; the effect on the author's automation that also uses the camera (the camera was unavailable in Home Assistant for about 45 s at each sending). Figures come from one camera on one network; they say what limited this camera, not what will limit another.

The user-facing version, with the two settings and the table, is in the Cameras page.