Streams
The subset of WHATWG Streams that media processing uses, with readable, writable and transform streams of objects.
Where the specification returns a promise, these methods return an asyncio.Future. The operation starts when
the method is called, before the future is awaited. Byte streams and BYOB readers arenโt implemented. Streams are
created and used from a running asyncio event loop.
- class webrtc.streams.QueuingStrategy(high_water_mark=None, size=None)
How a stream measures its queue, given to the constructor of a stream.
- class webrtc.streams.ReadableStream(underlying_source=None, strategy=None)
A source of chunks to read, iterable with
async for.See MDN: ReadableStream.
It belongs to the first event loop using it until that loop closes: from another loop, its methods and those of its readers, writers and controllers raise
webrtc.InvalidStateError.- Parameters:
underlying_source (optional) โ An object or a
dictwith optionalstart(controller),pull(controller)andcancel(reason)callables. Each can be a plain or a coroutine function, and they get aReadableStreamDefaultController.strategy (
QueuingStrategy, optional) โ How the queue is measured. The default high water mark is 1.
- Raises:
webrtc.InvalidRangeError โ If the high water mark of the strategy is negative or NaN.
- static from_(async_iterable)
Creates a stream that reads the items of an iterable, asynchronous or not.
Named
fromin the specification. Items are fetched one at a time, as theyโre read. Canceling the stream closes the iterator if itโs a generator.See MDN: ReadableStream/from_static.
- Parameters:
async_iterable (AsyncIterable[_R] | Iterable[_R]) โ The iterable or asynchronous iterable.
- Returns:
ReadableStreamโ The stream of its items.- Raises:
TypeError โ If the object isnโt iterable.
- Return type:
ReadableStream[_R]
- property locked: bool
Whether a reader holds the lock of the stream.
See MDN: ReadableStream/locked.
- Type:
- get_reader(options=None)
Creates a reader, which locks the stream until
ReadableStreamDefaultReader.release_lock().See MDN: ReadableStream/getReader.
- Parameters:
options (
ReadableStreamGetReaderOptions, optional) โ The kind of reader. Only default readers exist.- Returns:
ReadableStreamDefaultReaderโ The reader.- Raises:
TypeError โ If the stream is locked or the mode is
'byob'.- Return type:
- cancel(reason=None)
Cancels the stream. Queued chunks are dropped and the
cancelof the source is called.See MDN: ReadableStream/cancel.
- Parameters:
reason (optional) โ Passed to the
cancelof the source.- Returns:
asyncio.Futureโ Done once the source is canceled. Fails withTypeErrorif the stream is locked, or with the error of an errored stream or of the sourceโscancel.- Return type:
Future[None]
- pipe_to(destination, options=None)
Reads every chunk and writes it to a writable stream, respecting its backpressure.
Both streams are locked while the pipe runs. Thereโs no abort signal, so cancel the returned future to stop the pipe. The destination is then aborted unless
StreamPipeOptions.prevent_abortis set.See MDN: ReadableStream/pipeTo.
- Parameters:
destination (
WritableStream) โ The stream to write to.options (
StreamPipeOptions, optional) โ What the pipe leaves untouched when it stops.
- Returns:
asyncio.Futureโ Done once every chunk is written and the destination is closed, unless closing is prevented. It fails with the error that stopped the pipe, or withTypeErrorif either stream is locked.- Return type:
Future[None]
- pipe_through(transform, options=None)
Sends the chunks of the stream through a transform and returns the readable side of the transform.
See MDN: ReadableStream/pipeThrough.
- Parameters:
transform (
ReadableWritablePair) โ The pair, or any object withwritableandreadablestreams, likeTransformStream.options (
StreamPipeOptions, optional) โ The options of the pipe, as forpipe_to().
- Returns:
ReadableStreamโ The readable side of the transform.- Raises:
TypeError โ If this stream or the writable side is locked.
- Return type:
ReadableStream[_O]
- tee()
Splits the stream into two branches that each read every chunk, and locks the stream.
Chunks arenโt copied, so both branches get the same objects. The stream is canceled once both branches are, and both branches error if it errors.
- Returns:
listofReadableStreamโ The two branches.- Raises:
TypeError โ If the stream is locked.
- Return type:
list[ReadableStream[_T]]
- values(options=None)
Returns an asynchronous iterator over the chunks, which locks the stream.
async foruses it too.Stopping early cancels the stream when the iterator is finalized. Wrap it in
contextlib.aclosing()to cancel right away.- Parameters:
options (
ReadableStreamIteratorOptions, optional) โ Whether stopping early leaves the stream open.- Returns:
collections.abc.AsyncIteratorโ The chunks.- Raises:
TypeError โ If the stream is locked.
- Return type:
AsyncIterator[_T]
- class webrtc.streams.ReadableStreamDefaultController(stream, source, strategy)
Given to an underlying source to enqueue chunks into its stream, close it or error it.
See MDN: ReadableStreamDefaultController.
- property desired_size: float | None
The room left in the queue before backpressure.
Itโs negative when the queue is over-full, 0 once the stream is closed and
Noneonce it errored.See MDN: ReadableStreamDefaultController/desiredSize.
- Type:
float, optional
- enqueue(chunk=None)
Adds a chunk to the stream. If a read is pending, the chunk goes straight to it.
See MDN: ReadableStreamDefaultController/enqueue.
- Parameters:
chunk (_T | None) โ The chunk.
- Raises:
TypeError โ If the stream is closed or a close was requested.
webrtc.InvalidRangeError โ If the size of the chunk isnโt a finite non-negative number. This error and any exception from the
sizefunction also error the stream.
- close()
Closes the stream once the chunks already queued are read.
See MDN: ReadableStreamDefaultController/close.
- Raises:
TypeError โ If the stream is closed or a close was requested.
- error(e=None)
Errors the stream and drops its queue. Pending and later reads fail with the error.
Does nothing if the stream isnโt readable.
See MDN: ReadableStreamDefaultController/error.
- Parameters:
e (optional) โ The error. A value that isnโt an exception is raised as a
TypeError.
- class webrtc.streams.ReadableStreamDefaultReader(stream)
Reads the chunks of a stream, locking it until
release_lock().See MDN: ReadableStreamDefaultReader.
- Parameters:
stream (
ReadableStream) โ The stream to lock.- Raises:
TypeError โ If the stream is locked.
- property closed: Future[None]
Done once the stream closes. It fails if the stream errors or the lock is released.
See MDN: ReadableStreamDefaultReader/closed.
- Type:
- read()
Reads the next chunk.
See MDN: ReadableStreamDefaultReader/read.
- Returns:
asyncio.Futureโ AReadableStreamReadResult, set once a chunk is available or the stream closes. It fails with the error of the stream, or withTypeErroronce the reader is released.- Return type:
Future[ReadableStreamReadResult[_T]]
- cancel(reason=None)
Cancels the stream, as
ReadableStream.cancel()does, while keeping the lock.See MDN: ReadableStreamDefaultReader/cancel.
- Parameters:
reason (optional) โ Passed to the
cancelof the source.- Returns:
asyncio.Futureโ Done once the source is canceled. Fails withTypeErroronce the reader is released.- Return type:
Future[None]
- release_lock()
Unlocks the stream. Pending reads and
closedfail withTypeError.It does nothing if the reader is already released.
See MDN: ReadableStreamDefaultReader/releaseLock.
- class webrtc.streams.ReadableStreamGetReaderOptions(mode=None)
The options of
ReadableStream.get_reader().See MDN: ReadableStream/getReader.
- Parameters:
mode (
webrtc.ReadableStreamReaderMode, optional) โ The kind of reader.Nonegives a default reader.'byob'is refused because there are no byte streams.- Raises:
ValueError โ If the mode isnโt a member of
webrtc.ReadableStreamReaderMode.
- class webrtc.streams.ReadableStreamIteratorOptions(prevent_cancel=False)
The options of
ReadableStream.values().- Parameters:
prevent_cancel (
bool, optional) โ Keep the stream open when the iteration stops before the end.
- class webrtc.streams.ReadableStreamReadResult(value=None, done=False)
The result of
ReadableStreamDefaultReader.read().
- class webrtc.streams.ReadableWritablePair(readable, writable)
A writable stream and the readable stream its output comes out of, for
ReadableStream.pipe_through().See MDN: ReadableStream/pipeThrough.
- Parameters:
readable (
ReadableStream) โ The output side.writable (
WritableStream) โ The input side.
- class webrtc.streams.StreamPipeOptions(prevent_close=False, prevent_abort=False, prevent_cancel=False)
The options of
ReadableStream.pipe_to()andReadableStream.pipe_through().Thereโs no
signal. To stop a pipe, cancel the future thatReadableStream.pipe_to()returns.
- class webrtc.streams.TransformStream(transformer=None, writable_strategy=None, readable_strategy=None)
A writable side and a readable side. Chunks written to one are transformed and read from the other.
See MDN: TransformStream.
It belongs to the first event loop using it until that loop closes: from another loop, its methods and those of its readers, writers and controllers raise
webrtc.InvalidStateError.- Parameters:
transformer (optional) โ An object or a
dictwith optionalstart(controller),transform(chunk, controller)andflush(controller)callables. Each can be a plain or a coroutine function, and they get aTransformStreamDefaultController. Withouttransform, chunks pass through unchanged.writable_strategy (
QueuingStrategy, optional) โ How the queue of the writable side is measured. The default high water mark is 1.readable_strategy (
QueuingStrategy, optional) โ How the queue of the readable side is measured. The default high water mark is 0.
- Raises:
webrtc.InvalidRangeError โ If the high water mark of a strategy is negative or NaN.
- property readable: ReadableStream[_O]
The side to read the output chunks from.
See MDN: TransformStream/readable.
- Type:
- property writable: WritableStream[_T]
The side to write the input chunks to.
See MDN: TransformStream/writable.
- Type:
- class webrtc.streams.TransformStreamDefaultController(stream)
Given to a transformer to enqueue output chunks, error the stream or terminate it.
See MDN: TransformStreamDefaultController.
- property desired_size: float | None
The room left in the queue of the readable side.
It matches
ReadableStreamDefaultController.desired_sizeof that side.See MDN: TransformStreamDefaultController/desiredSize.
- Type:
float, optional
- enqueue(chunk=None)
Adds an output chunk to the readable side.
See MDN: TransformStreamDefaultController/enqueue.
- Parameters:
chunk (_O | None) โ The chunk.
- Raises:
TypeError โ If the readable side is closed or closing.
webrtc.InvalidRangeError โ If the size of the chunk isnโt a finite non-negative number.
- error(reason=None)
Errors both sides of the stream.
See MDN: TransformStreamDefaultController/error.
- Parameters:
reason (optional) โ The error. A value that isnโt an exception is raised as a
TypeError.
- terminate()
Ends the stream. Readers get the end of the stream and writers get a
TypeError.See MDN: TransformStreamDefaultController/terminate.
- class webrtc.streams.WritableStream(underlying_sink=None, strategy=None)
A destination to write chunks to, one at a time, in order.
See MDN: WritableStream.
It belongs to the first event loop using it until that loop closes: from another loop, its methods and those of its readers, writers and controllers raise
webrtc.InvalidStateError.- Parameters:
underlying_sink (optional) โ An object or a
dictwith optionalstart(controller),write(chunk, controller),close()andabort(reason)callables. Each can be a plain or a coroutine function, and they get aWritableStreamDefaultController.strategy (
QueuingStrategy, optional) โ How the queue is measured. The default high water mark is 1.
- Raises:
webrtc.InvalidRangeError โ If the high water mark of the strategy is negative or NaN.
- property locked: bool
Whether a writer holds the lock of the stream.
See MDN: WritableStream/locked.
- Type:
- get_writer()
Creates a writer, which locks the stream until
WritableStreamDefaultWriter.release_lock().See MDN: WritableStream/getWriter.
- Returns:
WritableStreamDefaultWriterโ The writer.- Raises:
TypeError โ If the stream is locked.
- Return type:
WritableStreamDefaultWriter[_W_contra]
- close()
Closes the stream after the chunks already written, then calls the
closeof the sink.See MDN: WritableStream/close.
- Returns:
asyncio.Futureโ Done once the sink is closed. It fails withTypeErrorif the stream is locked, closed or closing, or with the error that stops the stream.- Return type:
Future[None]
- abort(reason=None)
Aborts the stream. Queued writes fail, the stream errors and the
abortof the sink is called.It doesnโt wait for a write in progress. Pending writes fail with
reasonif itโs an exception, and otherwise with anasyncio.CancelledErrorthat holds it.See MDN: WritableStream/abort.
- Parameters:
reason (optional) โ Passed to the
abortof the sink.- Returns:
asyncio.Futureโ Done once the sink is aborted, or right away for a closed or errored stream. It fails withTypeErrorif the stream is locked, or with the sinkโsaborterror.- Return type:
Future[None]
- class webrtc.streams.WritableStreamDefaultController(stream, sink, strategy)
Given to an underlying sink to error its stream.
See MDN: WritableStreamDefaultController.
- error(e=None)
Errors the stream, so queued and later writes fail with the error.
It does nothing unless the stream is writable.
See MDN: WritableStreamDefaultController/error.
- Parameters:
e (optional) โ The error. A value that isnโt an exception is raised as a
TypeError.
- class webrtc.streams.WritableStreamDefaultWriter(stream)
Writes chunks to a stream, locking it until
release_lock().See MDN: WritableStreamDefaultWriter.
- Parameters:
stream (
WritableStream) โ The stream to lock.- Raises:
TypeError โ If the stream is locked.
- property closed: Future[None]
Done once the stream closes. It fails if the stream errors or the lock is released.
See MDN: WritableStreamDefaultWriter/closed.
- Type:
- property ready: Future[None]
Done while the queue is below its high water mark.
Under backpressure itโs replaced by a pending future. It fails if the stream errors or the lock is released.
See MDN: WritableStreamDefaultWriter/ready.
- Type:
- property desired_size: float | None
The room left in the queue before backpressure.
Itโs 0 once the stream is closed and
Noneonce itโs errored or erroring.
- write(chunk=None)
Queues a chunk for the
writeof the sink.See MDN: WritableStreamDefaultWriter/write.
- Parameters:
chunk (_W_contra | None) โ The chunk.
- Returns:
asyncio.Futureโ Done once the sink wrote the chunk. It fails with the error of the sink or the stream, or withTypeErrorif the stream is closing or the writer is released.- Return type:
Future[None]
- close()
Closes the stream, as
WritableStream.close()does, while keeping the lock.See MDN: WritableStreamDefaultWriter/close.
- Returns:
asyncio.Futureโ Done once the sink is closed. It fails withTypeErroronce the writer is released.- Return type:
Future[None]
- abort(reason=None)
Aborts the stream, as
WritableStream.abort()does, while keeping the lock.See MDN: WritableStreamDefaultWriter/abort.
- Parameters:
reason (optional) โ Passed to the
abortof the sink.- Returns:
asyncio.Futureโ Done once the sink is aborted. It fails withTypeErroronce the writer is released.- Return type:
Future[None]
- release_lock()
Unlocks the stream.
readyandclosedfail withTypeError.Writes already queued go on. It does nothing if the writer is already released.
See MDN: WritableStreamDefaultWriter/releaseLock.