-
Notifications
You must be signed in to change notification settings - Fork 4
Test MJPEGStream #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Test MJPEGStream #131
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3964d74
WIP: Add a test for the MJPEG Stream
rwb27 f9d0cae
Allow MJPEGStream to stop cleanly
rwb27 3791931
Make mjpeg stream test runnable
rwb27 febdac5
Add pillow to dev-requirements.txt
rwb27 ca41f6c
Update docstring for `notify_new_frame`
julianstirling File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ dev = [ | |
| "mypy>=1.6.1, <2", | ||
| "ruff>=0.1.3", | ||
| "types-jsonschema", | ||
| "Pillow", | ||
| ] | ||
|
|
||
| [project.urls] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import io | ||
| import threading | ||
| import time | ||
| from PIL import Image | ||
| from fastapi.testclient import TestClient | ||
| import labthings_fastapi as lt | ||
|
|
||
|
|
||
| class Telly(lt.Thing): | ||
| _stream_thread: threading.Thread | ||
| _streaming: bool = False | ||
| framerate: float = 1000 | ||
| frame_limit: int = 3 | ||
|
|
||
| stream = lt.outputs.MJPEGStreamDescriptor() | ||
|
|
||
| def __enter__(self): | ||
| self._streaming = True | ||
| self._stream_thread = threading.Thread(target=self._make_images) | ||
| self._stream_thread.start() | ||
|
|
||
| def __exit__(self, exc_t, exc_v, exc_tb): | ||
| self._streaming = False | ||
| self._stream_thread.join() | ||
|
|
||
| def _make_images(self): | ||
| """Stream a series of solid colours""" | ||
| colours = ["#F00", "#0F0", "#00F"] | ||
| jpegs = [] | ||
| for c in colours: | ||
| image = Image.new("RGB", (10, 10), c) | ||
| dest = io.BytesIO() | ||
| image.save(dest, "jpeg") | ||
| jpegs.append(dest.getvalue()) | ||
|
|
||
| i = 0 | ||
| while self._streaming and (i < self.frame_limit or self.frame_limit < 0): | ||
| self.stream.add_frame( | ||
| jpegs[i % len(jpegs)], self._labthings_blocking_portal | ||
| ) | ||
| time.sleep(1 / self.framerate) | ||
| i = i + 1 | ||
| self.stream.stop(self._labthings_blocking_portal) | ||
| self._streaming = False | ||
|
|
||
|
|
||
| def test_mjpeg_stream(): | ||
| """Verify the MJPEG stream contains at least one frame marker. | ||
|
|
||
| A limitation of the TestClient is that it can't actually stream. | ||
| This means that all of the frames sent by our test Thing will | ||
| arrive in a single packet. | ||
|
|
||
| For now, we just check it starts with the frame separator, | ||
| but it might be possible in the future to check there are three | ||
| images there. | ||
| """ | ||
| server = lt.ThingServer() | ||
| telly = Telly() | ||
| server.add_thing(telly, "telly") | ||
| with TestClient(server.app) as client: | ||
| with client.stream("GET", "/telly/stream") as stream: | ||
| stream.raise_for_status() | ||
| received = 0 | ||
| for b in stream.iter_bytes(): | ||
| received += 1 | ||
| assert b.startswith(b"--frame") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import uvicorn | ||
|
|
||
| server = lt.ThingServer() | ||
| telly = Telly() | ||
| telly.framerate = 6 | ||
| telly.frame_limit = -1 | ||
| server.add_thing(telly, "telly") | ||
| uvicorn.run(server.app, port=5000) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.