labthings_fastapi.outputs.mjpeg_stream
MJPEG Stream support.
This module defines a descriptor that allows Thing subclasses to expose an
MJPEG stream. See MJPEGStreamDescriptor.
Classes
A single entry in a ringbuffer. |
|
A StreamingResponse that streams an MJPEG stream. |
Module Contents
- class labthings_fastapi.outputs.mjpeg_stream.RingbufferEntry
A single entry in a ringbuffer.
This structure comprises one frame as a JPEG, plus a timestamp and a buffer index. Each time a frame is added to the stream, it is tagged with a timestamp and index, with the index increasing by 1 each time.
- timestamp: datetime.datetime
The time the frame was added to the ringbuffer.
- class labthings_fastapi.outputs.mjpeg_stream.MJPEGStreamResponse(gen: AsyncGenerator[bytes, None], status_code: int = 200)
Bases:
fastapi.responses.StreamingResponseA StreamingResponse that streams an MJPEG stream.
This response uses an async generator that yields
bytesobjects, each of which is a JPEG file. We add the –frame markers and mime types that mark it as an MJPEG stream. This is sufficient to enable it to work in animgtag, with thesrcset to the MJPEG stream’s endpoint.Set up StreamingResponse that streams an MJPEG stream.
This response is initialised with an async generator that yields
bytesobjects, each of which is a JPEG file. We add the –frame markers and mime types that mark it as an MJPEG stream. This is sufficient to enable it to work in animgtag, with thesrcset to the MJPEG stream’s endpoint.It expects an async generator that supplies individual JPEGs to be streamed, such as the one provided by
MJPEGStream.NB the
status_codeargument is used by FastAPI to set the status code of the response in OpenAPI.- Parameters:
gen – an async generator, yielding
bytesobjects each of which is one image, in JPEG format.status_code – The status code associated with the response, by default a 200 code is returned.
- media_type = 'multipart/x-mixed-replace; boundary=frame'
The media_type used to describe the endpoint in FastAPI.
- frame_async_generator
- async mjpeg_async_generator() AsyncGenerator[bytes, None]
Return a generator yielding an MJPEG stream.
This async generator wraps each incoming JPEG frame with the
--frameseparator and content type header. It is the basis of the response sent over HTTP (see__init__).- Yield:
JPEG frames, each with a
--framemarker prepended.