Skip to content

Mjpeg stream refactor - #409

Open
rwb27 wants to merge 7 commits into
mainfrom
mjpeg-stream-refactor
Open

rwb27 wants to merge 7 commits into
mainfrom
mjpeg-stream-refactor

Conversation

@rwb27

@rwb27 rwb27 commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

This PR strips out the custom pub/sub logic from MJPEGStream and replaces it with a couple of calls to MessageBroker. It goes further than #372 by actually using the MessageBroker object rather than just pinching a function from it. I think this results in a bigger improvement, from a smaller code change.

I hope this makes the code much more readable and much more testable.

The diff has more additions than deletions - but the deletions are (complicated) code while the additions are docstrings, comments, tests, and calls to much clearer code.

The pub/sub logic in MessageBroker is tested much more carefully than the old logic was, even though it was technically covered before.

I think I now understand how to write some better async tests for MJPEGStream, but I'll leave that for a future PR. This PR passes both the old tests, and the relevant bits of the improved tests in #372, which are included in this PR.

OFM-Feature-Branch: v3-mjpeg-stream-refactor

This MR contains the following

  • Add some utility functions to MessageBroker
  • Add a new "stream" message type
  • Remove the ringbuffer and subscription mechanism from MJPEGStream
  • Publish JPEG frames via the MessageBroker

Before merge:

  • Test autofocus with real hardware
  • Add more unit tests

Merge checklist:

  • All new/changed functions have up to date typehints and docstrings.
  • Any changes to the public API have been updated in docs/src/public_api.rst.
  • New or changed features have been added to (or updated in) the conceptual documentation.
  • Any new or updated dependencies have been added to the project configuration (e.g., pyproject.toml).
  • New functionality is fully tested.
  • Any decrease in test coverage has been justified.
  • Either the test-against-ofm-v3 job passes, or test-against-ofm-feature-branch passes.
  • New features have been used in a branch of the OpenFlexure Microscope, which is tested in test-against-ofm-feature-branch.
  • This code has been tested manually against simulated hardware (detail tests in "Before merge").
  • This code has been tested against real hardware (detail tests in "Before merge").

rwb27 added 3 commits October 9, 2026 00:09
I've added "stream" as a message type, and provided "next message" and "close streams by affordance" methods.

This should prepare the way for using MessageBroker for MJPEG streams.
The thing server interface now exposes the message broker.
This strips out the pub/sub logic from MJPEGStream in favour of using
MessageBroker, as is already done for property notifications.

The resulting class should be much simpler.

I've also inherited from BaseDescriptor to save duplication in
MJPEGStreamDescriptor.
@barecheck

barecheck Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Barecheck - Code coverage report

Total: 97.77%

Your code coverage diff: 0.34% ▴

Uncovered files and lines
FileLines
src/labthings_fastapi/message_broker.py176-177
src/labthings_fastapi/outputs/mjpeg_stream.py249, 306, 342

rwb27 added 4 commits October 9, 2026 00:32
This uses an event to speed up tests by eliminating `time.sleep` and adds some more checks on the data received.
The stream no longer has the notion of being "active" so we can't check for errors there.

This might be a feature we want to revive - I'm not totally sure.
This is tested by a better-isolated test that mocks the server and uses a real MessageBroker.

This branch has not been deployed

No deployments
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