Skip to content

Add raw MJPEG frames and caller-owned stream WebSockets - #54

Merged
chiconws merged 2 commits into
devfrom
feature/stream-bytes-and-open-ws
Oct 13, 2026
Merged

chiconws merged 2 commits into
devfrom
feature/stream-bytes-and-open-ws

Conversation

@chiconws

Copy link
Copy Markdown
Collaborator

Summary

The Home Assistant integration reads MJPEG frames and opens the WebRTC signaling WebSocket by reaching into the client's private session, HTTP session and SSL config, and by building the session cookie itself. This adds the two public methods it needs to stop doing that.

Changes

  • mjpeg_stream_bytes() yields each MJPEG frame as raw JPEG bytes. mjpeg_stream() is now this generator plus the decoding it already did.
  • open_ws(path, **kwargs) opens a new WebSocket on path, relative to the API base, such as stream/h264/webrtc. Each call returns its own connection and the caller closes it. kwargs go to aiohttp's ws_connect, for example heartbeat and timeout.
  • get_ws() shares the connection code. Building the ws/wss URL, the isolated cookie jar, the session cookie and the SSL config moved into one helper used by both, as did the handling of 401 and 403 on the handshake.

Notes

  • open_ws rejects headers and ssl with TypeError. The client owns both.
  • A WebSocket from open_ws is not closed by logout() or by leaving the client's async with. The caller must close it before the client exits.
  • A 401 on the handshake clears the session and raises NanoKVMNotAuthenticatedError; a 403 raises NanoKVMPermissionError with the requested path.
  • A caller that stops at the first frame of mjpeg_stream_bytes() should wrap it in contextlib.aclosing so the HTTP connection closes right away.

Behaviour changes

None. Both methods are additions, and mjpeg_stream() and the HID WebSocket behave as before.

Testing

404 unit tests pass, 2 of them new. Also run against a NanoKVM PCIe: mjpeg_stream_bytes() returned a JPEG frame and open_ws("stream/h264") connected while the HID WebSocket stayed open. On a NanoKVM Pro, open_ws("stream/h264/webrtc") opened two separate connections over HTTPS, again without affecting the HID WebSocket. MJPEG was not opened on the Pro, because that switches its video mode.

@chiconws
chiconws merged commit 73d8f3d into dev Oct 13, 2026
2 checks passed
@chiconws
chiconws deleted the feature/stream-bytes-and-open-ws branch October 13, 2026 00:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant