livestream: read frames with readexactly to avoid truncation#1232
livestream: read frames with readexactly to avoid truncation#1232jasoncarreira wants to merge 1 commit into
Conversation
|
Thanks a lot, this looks good! You mentioned that you are working on HA integration as well, have you seen my work-in-progress PR which was blocked due to performance issues and the recent changes to the blink API? I would like to avoid double-work. |
|
@jasoncarreira this looks good- just fix the failing test (probably the test is expecting a certain call and your change modified that flow) and I'll merge. |
StreamReader.read(n) returns up to n bytes, so a header or payload split across TCP segments was misread as a short read and aborted the stream. readexactly() blocks for the full frame and raises IncompleteReadError only on a genuine EOF. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
d69332a to
58fc442
Compare
|
Thanks @mback2k! No double-work to worry about — I'm not doing a competing HA-core PR. My Blink live-view work was just a local A few findings from reverse-engineering the immis stream that may help the perf/stability issues:
I've got several Blink cameras + a doorbell on HA — happy to test #160708 and give performance feedback. |
From previous experiences with other HA core integrations the core developers will probably ask the library to take care of that, for example via a mutex lock or something similar inside the library API instance. |
Description
BlinkLiveStream.recv()reads the 9-byte immis frame header and then thepayload with
StreamReader.read(n):StreamReader.read(n)returns up tonbytes — it resolves as soon as anydata is buffered. When a header or payload is split across TCP segments (very
common for the larger video payloads),
read()returns a short buffer, thelen(data) < nguard treats it as EOF, and the stream is torn down mid-frameeven though the connection is perfectly healthy.
This replaces both reads with
readexactly(), which waits for the full frameand raises
IncompleteReadErroronly on a genuine EOF:How I found it
While getting live view working through Home Assistant I built a standalone
relay that parses the immis framing from a buffer (accumulate-then-slice) and it
streamed hundreds of H.264 frames reliably. A faithful port that used
read(n)instead would intermittently abort with "Insufficient data". The difference is
exactly this partial-read handling.
Note: this is independent of the recent OAuth/2FA (202) and liveview-endpoint
work in #1227/#1228/#1229/#1231 — it's a latent framing bug that surfaces once a
live view actually connects and starts delivering video.
Checklist
read(n)truncates)tox(no test currently exercisesrecv()framing; happy to add one if desired)