# 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`](https://docs.python.org/3/library/asyncio-future.html#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.

* **Parameters:**
  * **high_water_mark** ([`float`](https://docs.python.org/3/builtins/functions.html#float), optional) – The total size of queued chunks at which the stream signals
    backpressure. [`None`](https://docs.python.org/3/builtins/constants.html#None) takes the default of the stream.
  * **size** ([`callable`](https://docs.python.org/3/builtins/functions.html#callable), optional) – Returns the size of a chunk. Each chunk counts as 1 if [`None`](https://docs.python.org/3/builtins/constants.html#None).

### *class* webrtc.streams.ReadableStream(underlying_source=None, strategy=None)

A source of chunks to read, iterable with `async for`.

See [MDN: ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/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`](exceptions.md#webrtc.exceptions.InvalidStateError).

* **Parameters:**
  * **underlying_source** (*optional*) – An object or a [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict) with optional `start(controller)`,
    `pull(controller)` and `cancel(reason)` callables. Each can be a plain or a coroutine function, and
    they get a [`ReadableStreamDefaultController`](#webrtc.streams.ReadableStreamDefaultController).
  * **strategy** ([`QueuingStrategy`](#webrtc.streams.QueuingStrategy), optional) – How the queue is measured. The default high water mark is 1.
* **Raises:**
  [**webrtc.InvalidRangeError**](exceptions.md#webrtc.exceptions.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 `from` in 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](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/from_static).

* **Parameters:**
  **async_iterable** ([*AsyncIterable*](https://docs.python.org/3/library/collections.abc.html#collections.abc.AsyncIterable) *[* *\_R* *]*  *|* [*Iterable*](https://docs.python.org/3/library/collections.abc.html#collections.abc.Iterable) *[* *\_R* *]*) – The iterable or asynchronous iterable.
* **Returns:**
  [`ReadableStream`](#webrtc.streams.ReadableStream) – The stream of its items.
* **Raises:**
  [**TypeError**](https://docs.python.org/3/builtins/exceptions.html#TypeError) – If the object isn’t iterable.
* **Return type:**
  [*ReadableStream*](#webrtc.streams.ReadableStream)[ *\_R*]

#### *property* locked *: [bool](https://docs.python.org/3/builtins/functions.html#bool)*

Whether a reader holds the lock of the stream.

See [MDN: ReadableStream/locked](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/locked).

* **Type:**
  [`bool`](https://docs.python.org/3/builtins/functions.html#bool)

#### get_reader(options=None)

Creates a reader, which locks the stream until [`ReadableStreamDefaultReader.release_lock()`](#webrtc.streams.ReadableStreamDefaultReader.release_lock).

See [MDN: ReadableStream/getReader](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/getReader).

* **Parameters:**
  **options** ([`ReadableStreamGetReaderOptions`](#webrtc.streams.ReadableStreamGetReaderOptions), optional) – The kind of reader. Only default readers
  exist.
* **Returns:**
  [`ReadableStreamDefaultReader`](#webrtc.streams.ReadableStreamDefaultReader) – The reader.
* **Raises:**
  [**TypeError**](https://docs.python.org/3/builtins/exceptions.html#TypeError) – If the stream is locked or the mode is `'byob'`.
* **Return type:**
  [*ReadableStreamDefaultReader*](#webrtc.streams.ReadableStreamDefaultReader)[ *\_T*]

#### cancel(reason=None)

Cancels the stream. Queued chunks are dropped and the `cancel` of the source is called.

See [MDN: ReadableStream/cancel](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/cancel).

* **Parameters:**
  **reason** (*optional*) – Passed to the `cancel` of the source.
* **Returns:**
  [`asyncio.Future`](https://docs.python.org/3/library/asyncio-future.html#asyncio.Future) – Done once the source is canceled. Fails with [`TypeError`](https://docs.python.org/3/builtins/exceptions.html#TypeError) if the stream is
  locked, or with the error of an errored stream or of the source’s `cancel`.
* **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_abort` is set.

See [MDN: ReadableStream/pipeTo](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/pipeTo).

* **Parameters:**
  * **destination** ([`WritableStream`](#webrtc.streams.WritableStream)) – The stream to write to.
  * **options** ([`StreamPipeOptions`](#webrtc.streams.StreamPipeOptions), optional) – What the pipe leaves untouched when it stops.
* **Returns:**
  [`asyncio.Future`](https://docs.python.org/3/library/asyncio-future.html#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 with [`TypeError`](https://docs.python.org/3/builtins/exceptions.html#TypeError) if 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](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/pipeThrough).

* **Parameters:**
  * **transform** ([`ReadableWritablePair`](#webrtc.streams.ReadableWritablePair)) – The pair, or any object with `writable` and `readable`
    streams, like [`TransformStream`](#webrtc.streams.TransformStream).
  * **options** ([`StreamPipeOptions`](#webrtc.streams.StreamPipeOptions), optional) – The options of the pipe, as for [`pipe_to()`](#webrtc.streams.ReadableStream.pipe_to).
* **Returns:**
  [`ReadableStream`](#webrtc.streams.ReadableStream) – The readable side of the transform.
* **Raises:**
  [**TypeError**](https://docs.python.org/3/builtins/exceptions.html#TypeError) – If this stream or the writable side is locked.
* **Return type:**
  [*ReadableStream*](#webrtc.streams.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.

See [MDN: ReadableStream/tee](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/tee).

* **Returns:**
  [`list`](https://docs.python.org/3/builtins/stdtypes.html#list) of [`ReadableStream`](#webrtc.streams.ReadableStream) – The two branches.
* **Raises:**
  [**TypeError**](https://docs.python.org/3/builtins/exceptions.html#TypeError) – If the stream is locked.
* **Return type:**
  [list](https://docs.python.org/3/builtins/stdtypes.html#list)[[*ReadableStream*](#webrtc.streams.ReadableStream)[ *\_T*]]

#### values(options=None)

Returns an asynchronous iterator over the chunks, which locks the stream. `async for` uses it too.

Stopping early cancels the stream when the iterator is finalized. Wrap it in [`contextlib.aclosing()`](https://docs.python.org/3/library/contextlib.html#contextlib.aclosing)
to cancel right away.

* **Parameters:**
  **options** ([`ReadableStreamIteratorOptions`](#webrtc.streams.ReadableStreamIteratorOptions), optional) – Whether stopping early leaves the stream open.
* **Returns:**
  [`collections.abc.AsyncIterator`](https://docs.python.org/3/library/collections.abc.html#collections.abc.AsyncIterator) – The chunks.
* **Raises:**
  [**TypeError**](https://docs.python.org/3/builtins/exceptions.html#TypeError) – If the stream is locked.
* **Return type:**
  [*AsyncIterator*](https://docs.python.org/3/library/collections.abc.html#collections.abc.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](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultController).

#### *property* desired_size *: [float](https://docs.python.org/3/builtins/functions.html#float) | [None](https://docs.python.org/3/builtins/constants.html#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 [`None`](https://docs.python.org/3/builtins/constants.html#None) once it errored.

See [MDN: ReadableStreamDefaultController/desiredSize](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultController/desiredSize).

* **Type:**
  [`float`](https://docs.python.org/3/builtins/functions.html#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](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultController/enqueue).

* **Parameters:**
  **chunk** ( *\_T* *|* *None*) – The chunk.
* **Raises:**
  * [**TypeError**](https://docs.python.org/3/builtins/exceptions.html#TypeError) – If the stream is closed or a close was requested.
  * [**webrtc.InvalidRangeError**](exceptions.md#webrtc.exceptions.InvalidRangeError) – If the size of the chunk isn’t a finite non-negative number. This error and
        any exception from the `size` function also error the stream.

#### close()

Closes the stream once the chunks already queued are read.

See [MDN: ReadableStreamDefaultController/close](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultController/close).

* **Raises:**
  [**TypeError**](https://docs.python.org/3/builtins/exceptions.html#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](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultController/error).

* **Parameters:**
  **e** (*optional*) – The error. A value that isn’t an exception is raised as a [`TypeError`](https://docs.python.org/3/builtins/exceptions.html#TypeError).

### *class* webrtc.streams.ReadableStreamDefaultReader(stream)

Reads the chunks of a stream, locking it until [`release_lock()`](#webrtc.streams.ReadableStreamDefaultReader.release_lock).

See [MDN: ReadableStreamDefaultReader](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader).

* **Parameters:**
  **stream** ([`ReadableStream`](#webrtc.streams.ReadableStream)) – The stream to lock.
* **Raises:**
  [**TypeError**](https://docs.python.org/3/builtins/exceptions.html#TypeError) – If the stream is locked.

#### *property* closed *: Future[[None](https://docs.python.org/3/builtins/constants.html#None)]*

Done once the stream closes. It fails if the stream errors or the lock is released.

See [MDN: ReadableStreamDefaultReader/closed](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader/closed).

* **Type:**
  [`asyncio.Future`](https://docs.python.org/3/library/asyncio-future.html#asyncio.Future)

#### read()

Reads the next chunk.

See [MDN: ReadableStreamDefaultReader/read](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader/read).

* **Returns:**
  [`asyncio.Future`](https://docs.python.org/3/library/asyncio-future.html#asyncio.Future) – A [`ReadableStreamReadResult`](#webrtc.streams.ReadableStreamReadResult), set once a chunk is available or the stream
  closes. It fails with the error of the stream, or with [`TypeError`](https://docs.python.org/3/builtins/exceptions.html#TypeError) once the reader is released.
* **Return type:**
  *Future*[[*ReadableStreamReadResult*](#webrtc.streams.ReadableStreamReadResult)[ *\_T*]]

#### cancel(reason=None)

Cancels the stream, as [`ReadableStream.cancel()`](#webrtc.streams.ReadableStream.cancel) does, while keeping the lock.

See [MDN: ReadableStreamDefaultReader/cancel](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader/cancel).

* **Parameters:**
  **reason** (*optional*) – Passed to the `cancel` of the source.
* **Returns:**
  [`asyncio.Future`](https://docs.python.org/3/library/asyncio-future.html#asyncio.Future) – Done once the source is canceled. Fails with [`TypeError`](https://docs.python.org/3/builtins/exceptions.html#TypeError) once the reader is
  released.
* **Return type:**
  *Future*[None]

#### release_lock()

Unlocks the stream. Pending reads and [`closed`](#webrtc.streams.ReadableStreamDefaultReader.closed) fail with [`TypeError`](https://docs.python.org/3/builtins/exceptions.html#TypeError).

It does nothing if the reader is already released.

See [MDN: ReadableStreamDefaultReader/releaseLock](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader/releaseLock).

### *class* webrtc.streams.ReadableStreamGetReaderOptions(mode=None)

The options of [`ReadableStream.get_reader()`](#webrtc.streams.ReadableStream.get_reader).

See [MDN: ReadableStream/getReader](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/getReader).

* **Parameters:**
  **mode** ([`webrtc.ReadableStreamReaderMode`](enums.md#webrtc.enums.ReadableStreamReaderMode), optional) – The kind of reader. [`None`](https://docs.python.org/3/builtins/constants.html#None) gives a default
  reader. `'byob'` is refused because there are no byte streams.
* **Raises:**
  [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError) – If the mode isn’t a member of [`webrtc.ReadableStreamReaderMode`](enums.md#webrtc.enums.ReadableStreamReaderMode).

### *class* webrtc.streams.ReadableStreamIteratorOptions(prevent_cancel=False)

The options of [`ReadableStream.values()`](#webrtc.streams.ReadableStream.values).

* **Parameters:**
  **prevent_cancel** ([`bool`](https://docs.python.org/3/builtins/functions.html#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()`](#webrtc.streams.ReadableStreamDefaultReader.read).

See [MDN: ReadableStreamDefaultReader/read](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader/read).

* **Parameters:**
  * **value** (*optional*) – The chunk read. It’s [`None`](https://docs.python.org/3/builtins/constants.html#None) when `done` is set.
  * **done** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool), optional) – [`True`](https://docs.python.org/3/builtins/constants.html#True) once the stream is closed and every chunk was read.

### *class* webrtc.streams.ReadableWritablePair(readable, writable)

A writable stream and the readable stream its output comes out of, for [`ReadableStream.pipe_through()`](#webrtc.streams.ReadableStream.pipe_through).

See [MDN: ReadableStream/pipeThrough](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/pipeThrough).

* **Parameters:**
  * **readable** ([`ReadableStream`](#webrtc.streams.ReadableStream)) – The output side.
  * **writable** ([`WritableStream`](#webrtc.streams.WritableStream)) – The input side.

### *class* webrtc.streams.StreamPipeOptions(prevent_close=False, prevent_abort=False, prevent_cancel=False)

The options of [`ReadableStream.pipe_to()`](#webrtc.streams.ReadableStream.pipe_to) and [`ReadableStream.pipe_through()`](#webrtc.streams.ReadableStream.pipe_through).

There’s no `signal`. To stop a pipe, cancel the future that [`ReadableStream.pipe_to()`](#webrtc.streams.ReadableStream.pipe_to) returns.

See [MDN: ReadableStream/pipeTo](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/pipeTo).

* **Parameters:**
  * **prevent_close** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool), optional) – Don’t close the destination when the source closes.
  * **prevent_abort** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool), optional) – Don’t abort the destination when the source errors.
  * **prevent_cancel** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool), optional) – Don’t cancel the source when the destination errors.

### *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](https://developer.mozilla.org/en-US/docs/Web/API/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`](exceptions.md#webrtc.exceptions.InvalidStateError).

* **Parameters:**
  * **transformer** (*optional*) – An object or a [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict) with optional `start(controller)`,
    `transform(chunk, controller)` and `flush(controller)` callables. Each can be a plain or a coroutine
    function, and they get a [`TransformStreamDefaultController`](#webrtc.streams.TransformStreamDefaultController). Without `transform`, chunks pass
    through unchanged.
  * **writable_strategy** ([`QueuingStrategy`](#webrtc.streams.QueuingStrategy), optional) – How the queue of the writable side is measured. The
    default high water mark is 1.
  * **readable_strategy** ([`QueuingStrategy`](#webrtc.streams.QueuingStrategy), optional) – How the queue of the readable side is measured. The
    default high water mark is 0.
* **Raises:**
  [**webrtc.InvalidRangeError**](exceptions.md#webrtc.exceptions.InvalidRangeError) – If the high water mark of a strategy is negative or NaN.

#### *property* readable *: [ReadableStream](#webrtc.streams.ReadableStream)[\_O]*

The side to read the output chunks from.

See [MDN: TransformStream/readable](https://developer.mozilla.org/en-US/docs/Web/API/TransformStream/readable).

* **Type:**
  [`ReadableStream`](#webrtc.streams.ReadableStream)

#### *property* writable *: [WritableStream](#webrtc.streams.WritableStream)[\_T]*

The side to write the input chunks to.

See [MDN: TransformStream/writable](https://developer.mozilla.org/en-US/docs/Web/API/TransformStream/writable).

* **Type:**
  [`WritableStream`](#webrtc.streams.WritableStream)

### *class* webrtc.streams.TransformStreamDefaultController(stream)

Given to a transformer to enqueue output chunks, error the stream or terminate it.

See [MDN: TransformStreamDefaultController](https://developer.mozilla.org/en-US/docs/Web/API/TransformStreamDefaultController).

#### *property* desired_size *: [float](https://docs.python.org/3/builtins/functions.html#float) | [None](https://docs.python.org/3/builtins/constants.html#None)*

The room left in the queue of the readable side.

It matches [`ReadableStreamDefaultController.desired_size`](#webrtc.streams.ReadableStreamDefaultController.desired_size) of that side.

See [MDN: TransformStreamDefaultController/desiredSize](https://developer.mozilla.org/en-US/docs/Web/API/TransformStreamDefaultController/desiredSize).

* **Type:**
  [`float`](https://docs.python.org/3/builtins/functions.html#float), optional

#### enqueue(chunk=None)

Adds an output chunk to the readable side.

See [MDN: TransformStreamDefaultController/enqueue](https://developer.mozilla.org/en-US/docs/Web/API/TransformStreamDefaultController/enqueue).

* **Parameters:**
  **chunk** ( *\_O* *|* *None*) – The chunk.
* **Raises:**
  * [**TypeError**](https://docs.python.org/3/builtins/exceptions.html#TypeError) – If the readable side is closed or closing.
  * [**webrtc.InvalidRangeError**](exceptions.md#webrtc.exceptions.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](https://developer.mozilla.org/en-US/docs/Web/API/TransformStreamDefaultController/error).

* **Parameters:**
  **reason** (*optional*) – The error. A value that isn’t an exception is raised as a [`TypeError`](https://docs.python.org/3/builtins/exceptions.html#TypeError).

#### terminate()

Ends the stream. Readers get the end of the stream and writers get a [`TypeError`](https://docs.python.org/3/builtins/exceptions.html#TypeError).

See [MDN: TransformStreamDefaultController/terminate](https://developer.mozilla.org/en-US/docs/Web/API/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](https://developer.mozilla.org/en-US/docs/Web/API/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`](exceptions.md#webrtc.exceptions.InvalidStateError).

* **Parameters:**
  * **underlying_sink** (*optional*) – An object or a [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict) with optional `start(controller)`,
    `write(chunk, controller)`, `close()` and `abort(reason)` callables. Each can be a plain or a
    coroutine function, and they get a [`WritableStreamDefaultController`](#webrtc.streams.WritableStreamDefaultController).
  * **strategy** ([`QueuingStrategy`](#webrtc.streams.QueuingStrategy), optional) – How the queue is measured. The default high water mark is 1.
* **Raises:**
  [**webrtc.InvalidRangeError**](exceptions.md#webrtc.exceptions.InvalidRangeError) – If the high water mark of the strategy is negative or NaN.

#### *property* locked *: [bool](https://docs.python.org/3/builtins/functions.html#bool)*

Whether a writer holds the lock of the stream.

See [MDN: WritableStream/locked](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream/locked).

* **Type:**
  [`bool`](https://docs.python.org/3/builtins/functions.html#bool)

#### get_writer()

Creates a writer, which locks the stream until [`WritableStreamDefaultWriter.release_lock()`](#webrtc.streams.WritableStreamDefaultWriter.release_lock).

See [MDN: WritableStream/getWriter](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream/getWriter).

* **Returns:**
  [`WritableStreamDefaultWriter`](#webrtc.streams.WritableStreamDefaultWriter) – The writer.
* **Raises:**
  [**TypeError**](https://docs.python.org/3/builtins/exceptions.html#TypeError) – If the stream is locked.
* **Return type:**
  [*WritableStreamDefaultWriter*](#webrtc.streams.WritableStreamDefaultWriter)[ *\_W_contra*]

#### close()

Closes the stream after the chunks already written, then calls the `close` of the sink.

See [MDN: WritableStream/close](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream/close).

* **Returns:**
  [`asyncio.Future`](https://docs.python.org/3/library/asyncio-future.html#asyncio.Future) – Done once the sink is closed. It fails with [`TypeError`](https://docs.python.org/3/builtins/exceptions.html#TypeError) if 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 `abort` of the sink is called.

It doesn’t wait for a write in progress. Pending writes fail with `reason` if it’s an exception, and
otherwise with an [`asyncio.CancelledError`](https://docs.python.org/3/library/asyncio-exceptions.html#asyncio.CancelledError) that holds it.

See [MDN: WritableStream/abort](https://developer.mozilla.org/en-US/docs/Web/API/WritableStream/abort).

* **Parameters:**
  **reason** (*optional*) – Passed to the `abort` of the sink.
* **Returns:**
  [`asyncio.Future`](https://docs.python.org/3/library/asyncio-future.html#asyncio.Future) – Done once the sink is aborted, or right away for a closed or errored stream. It
  fails with [`TypeError`](https://docs.python.org/3/builtins/exceptions.html#TypeError) if the stream is locked, or with the sink’s `abort` error.
* **Return type:**
  *Future*[None]

### *class* webrtc.streams.WritableStreamDefaultController(stream, sink, strategy)

Given to an underlying sink to error its stream.

See [MDN: WritableStreamDefaultController](https://developer.mozilla.org/en-US/docs/Web/API/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](https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultController/error).

* **Parameters:**
  **e** (*optional*) – The error. A value that isn’t an exception is raised as a [`TypeError`](https://docs.python.org/3/builtins/exceptions.html#TypeError).

### *class* webrtc.streams.WritableStreamDefaultWriter(stream)

Writes chunks to a stream, locking it until [`release_lock()`](#webrtc.streams.WritableStreamDefaultWriter.release_lock).

See [MDN: WritableStreamDefaultWriter](https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultWriter).

* **Parameters:**
  **stream** ([`WritableStream`](#webrtc.streams.WritableStream)) – The stream to lock.
* **Raises:**
  [**TypeError**](https://docs.python.org/3/builtins/exceptions.html#TypeError) – If the stream is locked.

#### *property* closed *: Future[[None](https://docs.python.org/3/builtins/constants.html#None)]*

Done once the stream closes. It fails if the stream errors or the lock is released.

See [MDN: WritableStreamDefaultWriter/closed](https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultWriter/closed).

* **Type:**
  [`asyncio.Future`](https://docs.python.org/3/library/asyncio-future.html#asyncio.Future)

#### *property* ready *: Future[[None](https://docs.python.org/3/builtins/constants.html#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](https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultWriter/ready).

* **Type:**
  [`asyncio.Future`](https://docs.python.org/3/library/asyncio-future.html#asyncio.Future)

#### *property* desired_size *: [float](https://docs.python.org/3/builtins/functions.html#float) | [None](https://docs.python.org/3/builtins/constants.html#None)*

The room left in the queue before backpressure.

It’s 0 once the stream is closed and [`None`](https://docs.python.org/3/builtins/constants.html#None) once it’s errored or erroring.

See [MDN: WritableStreamDefaultWriter/desiredSize](https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultWriter/desiredSize).

* **Raises:**
  [**TypeError**](https://docs.python.org/3/builtins/exceptions.html#TypeError) – If the writer is released.
* **Type:**
  [`float`](https://docs.python.org/3/builtins/functions.html#float), optional

#### write(chunk=None)

Queues a chunk for the `write` of the sink.

See [MDN: WritableStreamDefaultWriter/write](https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultWriter/write).

* **Parameters:**
  **chunk** ( *\_W_contra* *|* *None*) – The chunk.
* **Returns:**
  [`asyncio.Future`](https://docs.python.org/3/library/asyncio-future.html#asyncio.Future) – Done once the sink wrote the chunk. It fails with the error of the sink or the
  stream, or with [`TypeError`](https://docs.python.org/3/builtins/exceptions.html#TypeError) if the stream is closing or the writer is released.
* **Return type:**
  *Future*[None]

#### close()

Closes the stream, as [`WritableStream.close()`](#webrtc.streams.WritableStream.close) does, while keeping the lock.

See [MDN: WritableStreamDefaultWriter/close](https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultWriter/close).

* **Returns:**
  [`asyncio.Future`](https://docs.python.org/3/library/asyncio-future.html#asyncio.Future) – Done once the sink is closed. It fails with [`TypeError`](https://docs.python.org/3/builtins/exceptions.html#TypeError) once the writer is
  released.
* **Return type:**
  *Future*[None]

#### abort(reason=None)

Aborts the stream, as [`WritableStream.abort()`](#webrtc.streams.WritableStream.abort) does, while keeping the lock.

See [MDN: WritableStreamDefaultWriter/abort](https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultWriter/abort).

* **Parameters:**
  **reason** (*optional*) – Passed to the `abort` of the sink.
* **Returns:**
  [`asyncio.Future`](https://docs.python.org/3/library/asyncio-future.html#asyncio.Future) – Done once the sink is aborted. It fails with [`TypeError`](https://docs.python.org/3/builtins/exceptions.html#TypeError) once the writer is
  released.
* **Return type:**
  *Future*[None]

#### release_lock()

Unlocks the stream. [`ready`](#webrtc.streams.WritableStreamDefaultWriter.ready) and [`closed`](#webrtc.streams.WritableStreamDefaultWriter.closed) fail with [`TypeError`](https://docs.python.org/3/builtins/exceptions.html#TypeError).

Writes already queued go on. It does nothing if the writer is already released.

See [MDN: WritableStreamDefaultWriter/releaseLock](https://developer.mozilla.org/en-US/docs/Web/API/WritableStreamDefaultWriter/releaseLock).
