livekit 1.2.1__py3-none-win_arm64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- livekit/rtc/__init__.py +264 -0
- livekit/rtc/_ffi_client.py +321 -0
- livekit/rtc/_proto/__init__.py +0 -0
- livekit/rtc/_proto/audio_frame_pb2.py +159 -0
- livekit/rtc/_proto/audio_frame_pb2.pyi +1560 -0
- livekit/rtc/_proto/capture_pb2.py +70 -0
- livekit/rtc/_proto/capture_pb2.pyi +559 -0
- livekit/rtc/_proto/data_stream_pb2.py +171 -0
- livekit/rtc/_proto/data_stream_pb2.pyi +1498 -0
- livekit/rtc/_proto/data_track_pb2.py +120 -0
- livekit/rtc/_proto/data_track_pb2.pyi +1073 -0
- livekit/rtc/_proto/e2ee_pb2.py +81 -0
- livekit/rtc/_proto/e2ee_pb2.pyi +591 -0
- livekit/rtc/_proto/ffi_pb2.py +55 -0
- livekit/rtc/_proto/ffi_pb2.pyi +1246 -0
- livekit/rtc/_proto/handle_pb2.py +27 -0
- livekit/rtc/_proto/handle_pb2.pyi +51 -0
- livekit/rtc/_proto/participant_pb2.py +45 -0
- livekit/rtc/_proto/participant_pb2.pyi +313 -0
- livekit/rtc/_proto/room_pb2.py +311 -0
- livekit/rtc/_proto/room_pb2.pyi +3136 -0
- livekit/rtc/_proto/rpc_pb2.py +47 -0
- livekit/rtc/_proto/rpc_pb2.pyi +261 -0
- livekit/rtc/_proto/stats_pb2.py +123 -0
- livekit/rtc/_proto/stats_pb2.pyi +1514 -0
- livekit/rtc/_proto/track_pb2.py +76 -0
- livekit/rtc/_proto/track_pb2.pyi +544 -0
- livekit/rtc/_proto/track_publication_pb2.py +39 -0
- livekit/rtc/_proto/track_publication_pb2.pyi +145 -0
- livekit/rtc/_proto/video_frame_pb2.py +97 -0
- livekit/rtc/_proto/video_frame_pb2.pyi +838 -0
- livekit/rtc/_utils.py +187 -0
- livekit/rtc/apm.py +119 -0
- livekit/rtc/audio_filter.py +23 -0
- livekit/rtc/audio_frame.py +242 -0
- livekit/rtc/audio_mixer.py +247 -0
- livekit/rtc/audio_resampler.py +174 -0
- livekit/rtc/audio_source.py +191 -0
- livekit/rtc/audio_stream.py +354 -0
- livekit/rtc/data_stream.py +640 -0
- livekit/rtc/data_track.py +296 -0
- livekit/rtc/e2ee.py +320 -0
- livekit/rtc/encoded_video.py +127 -0
- livekit/rtc/event_emitter.py +215 -0
- livekit/rtc/frame_processor.py +37 -0
- livekit/rtc/jupyter.py +68 -0
- livekit/rtc/log.py +3 -0
- livekit/rtc/media_devices.py +596 -0
- livekit/rtc/participant.py +1171 -0
- livekit/rtc/platform_audio.py +426 -0
- livekit/rtc/py.typed +0 -0
- livekit/rtc/resources/LICENSE.md +4055 -0
- livekit/rtc/resources/__init__.py +1 -0
- livekit/rtc/resources/jupyter-html/index.html +101 -0
- livekit/rtc/resources/livekit_ffi.dll +0 -0
- livekit/rtc/resources/livekit_ffi.h +42 -0
- livekit/rtc/room.py +1304 -0
- livekit/rtc/rpc.py +244 -0
- livekit/rtc/synchronizer.py +202 -0
- livekit/rtc/track.py +264 -0
- livekit/rtc/track_publication.py +145 -0
- livekit/rtc/transcription.py +19 -0
- livekit/rtc/utils.py +138 -0
- livekit/rtc/version.py +1 -0
- livekit/rtc/video_frame.py +347 -0
- livekit/rtc/video_source.py +74 -0
- livekit/rtc/video_stream.py +191 -0
- livekit-1.2.1.dist-info/METADATA +80 -0
- livekit-1.2.1.dist-info/RECORD +71 -0
- livekit-1.2.1.dist-info/WHEEL +5 -0
- livekit-1.2.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,640 @@
|
|
|
1
|
+
# Copyright 2023 LiveKit, Inc.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import asyncio
|
|
18
|
+
import datetime
|
|
19
|
+
from collections.abc import Callable
|
|
20
|
+
from dataclasses import dataclass
|
|
21
|
+
from typing import AsyncIterator, Optional, Dict, List
|
|
22
|
+
from ._proto import data_stream_pb2 as proto_data_stream
|
|
23
|
+
from ._proto import ffi_pb2 as proto_ffi
|
|
24
|
+
from ._ffi_client import FfiClient, FfiHandle
|
|
25
|
+
from typing import TYPE_CHECKING
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
if TYPE_CHECKING:
|
|
29
|
+
from .participant import LocalParticipant
|
|
30
|
+
|
|
31
|
+
STREAM_CHUNK_SIZE = 15_000
|
|
32
|
+
"""Deprecated: chunking now happens inside the FFI; kept for compatibility."""
|
|
33
|
+
|
|
34
|
+
_DISCONNECT_ERROR = "Disconnected while receiving"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class StreamError(ConnectionError):
|
|
38
|
+
"""Raised when a data stream operation fails or an incoming stream
|
|
39
|
+
terminates abnormally (e.g. aborted by the sender or exceeding the
|
|
40
|
+
room's maximum payload length)."""
|
|
41
|
+
|
|
42
|
+
def __init__(self, description: str) -> None:
|
|
43
|
+
super().__init__(description)
|
|
44
|
+
self.description = description
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@dataclass
|
|
48
|
+
class BaseStreamInfo:
|
|
49
|
+
stream_id: str
|
|
50
|
+
mime_type: str
|
|
51
|
+
topic: str
|
|
52
|
+
timestamp: int
|
|
53
|
+
size: Optional[int]
|
|
54
|
+
attributes: Optional[Dict[str, str]] # Optional for the attributes dictionary
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@dataclass
|
|
58
|
+
class TextStreamInfo(BaseStreamInfo):
|
|
59
|
+
attachments: List[str]
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@dataclass
|
|
63
|
+
class ByteStreamInfo(BaseStreamInfo):
|
|
64
|
+
name: str
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _text_stream_info_from_proto(info: proto_data_stream.TextStreamInfo) -> TextStreamInfo:
|
|
68
|
+
return TextStreamInfo(
|
|
69
|
+
stream_id=info.stream_id,
|
|
70
|
+
mime_type=info.mime_type,
|
|
71
|
+
topic=info.topic,
|
|
72
|
+
timestamp=info.timestamp,
|
|
73
|
+
size=info.total_length if info.HasField("total_length") else 0,
|
|
74
|
+
attributes=dict(info.attributes),
|
|
75
|
+
attachments=list(info.attached_stream_ids),
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _byte_stream_info_from_proto(info: proto_data_stream.ByteStreamInfo) -> ByteStreamInfo:
|
|
80
|
+
return ByteStreamInfo(
|
|
81
|
+
stream_id=info.stream_id,
|
|
82
|
+
mime_type=info.mime_type,
|
|
83
|
+
topic=info.topic,
|
|
84
|
+
timestamp=info.timestamp,
|
|
85
|
+
size=info.total_length if info.HasField("total_length") else 0,
|
|
86
|
+
attributes=dict(info.attributes),
|
|
87
|
+
name=info.name,
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class TextStreamReader:
|
|
92
|
+
"""An incoming text stream.
|
|
93
|
+
|
|
94
|
+
Use as an async iterator to receive chunks, or :meth:`read_all` to collect
|
|
95
|
+
the whole payload::
|
|
96
|
+
|
|
97
|
+
async for chunk in reader:
|
|
98
|
+
process(chunk)
|
|
99
|
+
|
|
100
|
+
A reader subscribes to the FFI event queue as soon as it is handed to your
|
|
101
|
+
handler, so one that is never iterated to completion — abandoned after a
|
|
102
|
+
``break``, or received by a handler that never reads it — must be closed
|
|
103
|
+
with :meth:`close`, or its subscription lives for the rest of the process.
|
|
104
|
+
Iterating to the end closes the reader for you.
|
|
105
|
+
|
|
106
|
+
If the stream terminates abnormally (aborted by the sender, oversized, or
|
|
107
|
+
the room disconnects mid-stream), :class:`StreamError` is raised instead of
|
|
108
|
+
a normal ``StopAsyncIteration``.
|
|
109
|
+
"""
|
|
110
|
+
|
|
111
|
+
def __init__(
|
|
112
|
+
self,
|
|
113
|
+
owned_info: proto_data_stream.OwnedTextStreamReader,
|
|
114
|
+
*,
|
|
115
|
+
on_close: Optional[Callable[[], None]] = None,
|
|
116
|
+
) -> None:
|
|
117
|
+
self._info = _text_stream_info_from_proto(owned_info.info)
|
|
118
|
+
# the read_incremental request below consumes the FFI handle, so it
|
|
119
|
+
# must be kept as a raw id and never wrapped in an FfiHandle
|
|
120
|
+
handle_id = owned_info.handle.id
|
|
121
|
+
self._reader_handle = handle_id
|
|
122
|
+
self._on_close = on_close
|
|
123
|
+
self._closed = False
|
|
124
|
+
self._error: Optional[StreamError] = None
|
|
125
|
+
# subscribe before read_incremental so no reader event can be missed
|
|
126
|
+
self._queue = FfiClient.instance.queue.subscribe(
|
|
127
|
+
filter_fn=lambda e: (
|
|
128
|
+
e.WhichOneof("message") == "text_stream_reader_event"
|
|
129
|
+
and e.text_stream_reader_event.reader_handle == handle_id
|
|
130
|
+
),
|
|
131
|
+
)
|
|
132
|
+
req = proto_ffi.FfiRequest()
|
|
133
|
+
req.text_read_incremental.reader_handle = handle_id
|
|
134
|
+
FfiClient.instance.request(req)
|
|
135
|
+
|
|
136
|
+
def __aiter__(self) -> AsyncIterator[str]:
|
|
137
|
+
return self
|
|
138
|
+
|
|
139
|
+
async def __anext__(self) -> str:
|
|
140
|
+
while True:
|
|
141
|
+
if self._closed:
|
|
142
|
+
if self._error is not None:
|
|
143
|
+
raise self._error
|
|
144
|
+
raise StopAsyncIteration
|
|
145
|
+
event: proto_ffi.FfiEvent = await self._queue.get()
|
|
146
|
+
stream_event = event.text_stream_reader_event
|
|
147
|
+
detail = stream_event.WhichOneof("detail")
|
|
148
|
+
if detail == "chunk_received":
|
|
149
|
+
if not stream_event.chunk_received.content:
|
|
150
|
+
continue
|
|
151
|
+
return stream_event.chunk_received.content
|
|
152
|
+
elif detail == "eos":
|
|
153
|
+
eos = stream_event.eos
|
|
154
|
+
self._info.attributes = self._info.attributes or {}
|
|
155
|
+
self._info.attributes.update(eos.attributes)
|
|
156
|
+
if eos.HasField("error"):
|
|
157
|
+
self._error = StreamError(eos.error.description)
|
|
158
|
+
self._close()
|
|
159
|
+
if self._error is not None:
|
|
160
|
+
raise self._error
|
|
161
|
+
raise StopAsyncIteration
|
|
162
|
+
|
|
163
|
+
@property
|
|
164
|
+
def info(self) -> TextStreamInfo:
|
|
165
|
+
return self._info
|
|
166
|
+
|
|
167
|
+
async def read_all(self) -> str:
|
|
168
|
+
final_string = ""
|
|
169
|
+
async for chunk in self:
|
|
170
|
+
final_string += chunk
|
|
171
|
+
return final_string
|
|
172
|
+
|
|
173
|
+
def _unsubscribe(self) -> None:
|
|
174
|
+
"""Drops the FFI queue subscription without ending the stream.
|
|
175
|
+
|
|
176
|
+
Events already delivered stay in the queue and remain readable; only
|
|
177
|
+
the global subscription, and the filter it runs against every FFI
|
|
178
|
+
event, goes away. Safe to call repeatedly — unsubscribing a queue that
|
|
179
|
+
is no longer registered is a no-op.
|
|
180
|
+
|
|
181
|
+
Only the subscription is released: the reader handle was consumed by
|
|
182
|
+
the read_incremental request in __init__, so there is nothing left to
|
|
183
|
+
dispose (and disposing it would drop a handle the FFI server no longer
|
|
184
|
+
owns).
|
|
185
|
+
"""
|
|
186
|
+
FfiClient.instance.queue.unsubscribe(self._queue)
|
|
187
|
+
|
|
188
|
+
def _wake_pending_reads(self) -> None:
|
|
189
|
+
"""Pushes a terminal event into this reader's own queue.
|
|
190
|
+
|
|
191
|
+
Closing unsubscribes, so no further FFI event can ever arrive; without
|
|
192
|
+
this, a read already parked in ``await self._queue.get()`` would wait
|
|
193
|
+
forever. The sentinel carries no error, so the woken read raises
|
|
194
|
+
``StopAsyncIteration`` — or the stored :class:`StreamError`, which
|
|
195
|
+
``__anext__`` checks first.
|
|
196
|
+
"""
|
|
197
|
+
event = proto_ffi.FfiEvent()
|
|
198
|
+
event.text_stream_reader_event.reader_handle = self._reader_handle
|
|
199
|
+
event.text_stream_reader_event.eos.SetInParent()
|
|
200
|
+
self._queue.put_nowait(event)
|
|
201
|
+
|
|
202
|
+
def _close(self) -> None:
|
|
203
|
+
if not self._closed:
|
|
204
|
+
self._closed = True
|
|
205
|
+
self._unsubscribe()
|
|
206
|
+
self._wake_pending_reads()
|
|
207
|
+
if self._on_close is not None:
|
|
208
|
+
self._on_close()
|
|
209
|
+
|
|
210
|
+
def close(self) -> None:
|
|
211
|
+
"""Explicitly close the reader and unsubscribe.
|
|
212
|
+
|
|
213
|
+
Call this on a reader you stop consuming before it ends. Closing a
|
|
214
|
+
reader that already reached end-of-stream is a no-op, and iterating a
|
|
215
|
+
closed reader raises ``StopAsyncIteration`` (or :class:`StreamError`
|
|
216
|
+
if the stream had already failed).
|
|
217
|
+
"""
|
|
218
|
+
self._close()
|
|
219
|
+
|
|
220
|
+
async def aclose(self) -> None:
|
|
221
|
+
self.close()
|
|
222
|
+
|
|
223
|
+
def _signal_disconnect(self) -> None:
|
|
224
|
+
"""Injects a synthetic EOS-with-error event so pending reads wake up
|
|
225
|
+
and raise StreamError when the room disconnects mid-stream.
|
|
226
|
+
|
|
227
|
+
Also drops the queue subscription, which would otherwise outlive the
|
|
228
|
+
room: no further events can arrive once the room is gone, and the
|
|
229
|
+
injected one is already queued. The reader is deliberately left open
|
|
230
|
+
so chunks that arrived before the disconnect are still delivered
|
|
231
|
+
before the StreamError, as they were before this was unsubscribed
|
|
232
|
+
here.
|
|
233
|
+
"""
|
|
234
|
+
if self._closed:
|
|
235
|
+
return
|
|
236
|
+
event = proto_ffi.FfiEvent()
|
|
237
|
+
event.text_stream_reader_event.reader_handle = self._reader_handle
|
|
238
|
+
event.text_stream_reader_event.eos.error.description = _DISCONNECT_ERROR
|
|
239
|
+
self._queue.put_nowait(event)
|
|
240
|
+
self._unsubscribe()
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
class ByteStreamReader:
|
|
244
|
+
"""An incoming byte stream.
|
|
245
|
+
|
|
246
|
+
Use as an async iterator to receive chunks::
|
|
247
|
+
|
|
248
|
+
async for chunk in reader:
|
|
249
|
+
process(chunk)
|
|
250
|
+
|
|
251
|
+
A reader subscribes to the FFI event queue as soon as it is handed to your
|
|
252
|
+
handler, so one that is never iterated to completion — abandoned after a
|
|
253
|
+
``break``, or received by a handler that never reads it — must be closed
|
|
254
|
+
with :meth:`close`, or its subscription lives for the rest of the process.
|
|
255
|
+
Iterating to the end closes the reader for you.
|
|
256
|
+
|
|
257
|
+
If the stream terminates abnormally (aborted by the sender, oversized, or
|
|
258
|
+
the room disconnects mid-stream), :class:`StreamError` is raised instead of
|
|
259
|
+
a normal ``StopAsyncIteration``.
|
|
260
|
+
"""
|
|
261
|
+
|
|
262
|
+
def __init__(
|
|
263
|
+
self,
|
|
264
|
+
owned_info: proto_data_stream.OwnedByteStreamReader,
|
|
265
|
+
capacity: int = 0,
|
|
266
|
+
*,
|
|
267
|
+
on_close: Optional[Callable[[], None]] = None,
|
|
268
|
+
) -> None:
|
|
269
|
+
# capacity is ignored: chunk delivery is push-based from the FFI
|
|
270
|
+
self._info = _byte_stream_info_from_proto(owned_info.info)
|
|
271
|
+
handle_id = owned_info.handle.id
|
|
272
|
+
self._reader_handle = handle_id
|
|
273
|
+
self._on_close = on_close
|
|
274
|
+
self._closed = False
|
|
275
|
+
self._error: Optional[StreamError] = None
|
|
276
|
+
self._queue = FfiClient.instance.queue.subscribe(
|
|
277
|
+
filter_fn=lambda e: (
|
|
278
|
+
e.WhichOneof("message") == "byte_stream_reader_event"
|
|
279
|
+
and e.byte_stream_reader_event.reader_handle == handle_id
|
|
280
|
+
),
|
|
281
|
+
)
|
|
282
|
+
req = proto_ffi.FfiRequest()
|
|
283
|
+
req.byte_read_incremental.reader_handle = handle_id
|
|
284
|
+
FfiClient.instance.request(req)
|
|
285
|
+
|
|
286
|
+
def __aiter__(self) -> AsyncIterator[bytes]:
|
|
287
|
+
return self
|
|
288
|
+
|
|
289
|
+
async def __anext__(self) -> bytes:
|
|
290
|
+
while True:
|
|
291
|
+
if self._closed:
|
|
292
|
+
if self._error is not None:
|
|
293
|
+
raise self._error
|
|
294
|
+
raise StopAsyncIteration
|
|
295
|
+
event: proto_ffi.FfiEvent = await self._queue.get()
|
|
296
|
+
stream_event = event.byte_stream_reader_event
|
|
297
|
+
detail = stream_event.WhichOneof("detail")
|
|
298
|
+
if detail == "chunk_received":
|
|
299
|
+
if not stream_event.chunk_received.content:
|
|
300
|
+
continue
|
|
301
|
+
return stream_event.chunk_received.content
|
|
302
|
+
elif detail == "eos":
|
|
303
|
+
eos = stream_event.eos
|
|
304
|
+
self._info.attributes = self._info.attributes or {}
|
|
305
|
+
self._info.attributes.update(eos.attributes)
|
|
306
|
+
if eos.HasField("error"):
|
|
307
|
+
self._error = StreamError(eos.error.description)
|
|
308
|
+
self._close()
|
|
309
|
+
if self._error is not None:
|
|
310
|
+
raise self._error
|
|
311
|
+
raise StopAsyncIteration
|
|
312
|
+
|
|
313
|
+
@property
|
|
314
|
+
def info(self) -> ByteStreamInfo:
|
|
315
|
+
return self._info
|
|
316
|
+
|
|
317
|
+
def _unsubscribe(self) -> None:
|
|
318
|
+
"""Drops the FFI queue subscription without ending the stream.
|
|
319
|
+
|
|
320
|
+
Events already delivered stay in the queue and remain readable; only
|
|
321
|
+
the global subscription, and the filter it runs against every FFI
|
|
322
|
+
event, goes away. Safe to call repeatedly — unsubscribing a queue that
|
|
323
|
+
is no longer registered is a no-op.
|
|
324
|
+
|
|
325
|
+
Only the subscription is released: the reader handle was consumed by
|
|
326
|
+
the read_incremental request in __init__, so there is nothing left to
|
|
327
|
+
dispose (and disposing it would drop a handle the FFI server no longer
|
|
328
|
+
owns).
|
|
329
|
+
"""
|
|
330
|
+
FfiClient.instance.queue.unsubscribe(self._queue)
|
|
331
|
+
|
|
332
|
+
def _wake_pending_reads(self) -> None:
|
|
333
|
+
"""Pushes a terminal event into this reader's own queue.
|
|
334
|
+
|
|
335
|
+
Closing unsubscribes, so no further FFI event can ever arrive; without
|
|
336
|
+
this, a read already parked in ``await self._queue.get()`` would wait
|
|
337
|
+
forever. The sentinel carries no error, so the woken read raises
|
|
338
|
+
``StopAsyncIteration`` — or the stored :class:`StreamError`, which
|
|
339
|
+
``__anext__`` checks first.
|
|
340
|
+
"""
|
|
341
|
+
event = proto_ffi.FfiEvent()
|
|
342
|
+
event.byte_stream_reader_event.reader_handle = self._reader_handle
|
|
343
|
+
event.byte_stream_reader_event.eos.SetInParent()
|
|
344
|
+
self._queue.put_nowait(event)
|
|
345
|
+
|
|
346
|
+
def _close(self) -> None:
|
|
347
|
+
if not self._closed:
|
|
348
|
+
self._closed = True
|
|
349
|
+
self._unsubscribe()
|
|
350
|
+
self._wake_pending_reads()
|
|
351
|
+
if self._on_close is not None:
|
|
352
|
+
self._on_close()
|
|
353
|
+
|
|
354
|
+
def close(self) -> None:
|
|
355
|
+
"""Explicitly close the reader and unsubscribe.
|
|
356
|
+
|
|
357
|
+
Call this on a reader you stop consuming before it ends. Closing a
|
|
358
|
+
reader that already reached end-of-stream is a no-op, and iterating a
|
|
359
|
+
closed reader raises ``StopAsyncIteration`` (or :class:`StreamError`
|
|
360
|
+
if the stream had already failed).
|
|
361
|
+
"""
|
|
362
|
+
self._close()
|
|
363
|
+
|
|
364
|
+
async def aclose(self) -> None:
|
|
365
|
+
self.close()
|
|
366
|
+
|
|
367
|
+
def _signal_disconnect(self) -> None:
|
|
368
|
+
"""Injects a synthetic EOS-with-error event so pending reads wake up
|
|
369
|
+
and raise StreamError when the room disconnects mid-stream.
|
|
370
|
+
|
|
371
|
+
Also drops the queue subscription, which would otherwise outlive the
|
|
372
|
+
room: no further events can arrive once the room is gone, and the
|
|
373
|
+
injected one is already queued. The reader is deliberately left open
|
|
374
|
+
so chunks that arrived before the disconnect are still delivered
|
|
375
|
+
before the StreamError, as they were before this was unsubscribed
|
|
376
|
+
here.
|
|
377
|
+
"""
|
|
378
|
+
if self._closed:
|
|
379
|
+
return
|
|
380
|
+
event = proto_ffi.FfiEvent()
|
|
381
|
+
event.byte_stream_reader_event.reader_handle = self._reader_handle
|
|
382
|
+
event.byte_stream_reader_event.eos.error.description = _DISCONNECT_ERROR
|
|
383
|
+
self._queue.put_nowait(event)
|
|
384
|
+
self._unsubscribe()
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
class BaseStreamWriter:
|
|
388
|
+
def __init__(
|
|
389
|
+
self,
|
|
390
|
+
local_participant: LocalParticipant,
|
|
391
|
+
topic: str = "",
|
|
392
|
+
attributes: Optional[Dict[str, str]] = None,
|
|
393
|
+
stream_id: str | None = None,
|
|
394
|
+
total_size: int | None = None,
|
|
395
|
+
mime_type: str = "",
|
|
396
|
+
destination_identities: Optional[List[str]] = None,
|
|
397
|
+
sender_identity: str | None = None,
|
|
398
|
+
):
|
|
399
|
+
self._local_participant = local_participant
|
|
400
|
+
self._total_size = total_size
|
|
401
|
+
self._sender_identity = sender_identity or self._local_participant.identity
|
|
402
|
+
# the writer handle is assigned by the FFI when the stream is opened;
|
|
403
|
+
# the close request consumes it, so it is kept as a raw id
|
|
404
|
+
self._writer_handle: Optional[int] = None
|
|
405
|
+
self._writer_ffi_handle: Optional[FfiHandle] = None
|
|
406
|
+
self._write_lock = asyncio.Lock()
|
|
407
|
+
self._closed = False
|
|
408
|
+
|
|
409
|
+
def _provisional_timestamp(self) -> int:
|
|
410
|
+
return int(datetime.datetime.now().timestamp() * 1000)
|
|
411
|
+
|
|
412
|
+
def _register_open(self, handle_id: int) -> None:
|
|
413
|
+
"""Takes ownership of a freshly opened writer handle.
|
|
414
|
+
|
|
415
|
+
Wrapping it in an FfiHandle means a writer that is simply dropped —
|
|
416
|
+
abandoned, or left behind by an exception or a cancelled task — still
|
|
417
|
+
releases the native writer when it is garbage collected. It is also
|
|
418
|
+
registered with the participant so the room can drop it deterministically
|
|
419
|
+
at disconnect, for writers still referenced at that point. The registry
|
|
420
|
+
holds the handle weakly, so registration does not itself keep an
|
|
421
|
+
abandoned writer alive.
|
|
422
|
+
"""
|
|
423
|
+
self._writer_handle = handle_id
|
|
424
|
+
self._writer_ffi_handle = FfiHandle(handle_id)
|
|
425
|
+
self._local_participant._open_stream_writers[handle_id] = self._writer_ffi_handle
|
|
426
|
+
|
|
427
|
+
def _unregister_open(self) -> None:
|
|
428
|
+
"""Forgets the writer because a close request has been issued for it.
|
|
429
|
+
|
|
430
|
+
The close consumes the handle on the native side, so it must no longer
|
|
431
|
+
be dropped by the disconnect cleanup.
|
|
432
|
+
"""
|
|
433
|
+
if self._writer_handle is None:
|
|
434
|
+
return
|
|
435
|
+
self._local_participant._open_stream_writers.pop(self._writer_handle, None)
|
|
436
|
+
if self._writer_ffi_handle is not None:
|
|
437
|
+
self._writer_ffi_handle.mark_consumed()
|
|
438
|
+
|
|
439
|
+
async def _wait_for_callback(
|
|
440
|
+
self, req: proto_ffi.FfiRequest, callback_field: str, response_field: str
|
|
441
|
+
) -> proto_ffi.FfiEvent:
|
|
442
|
+
queue = FfiClient.instance.queue.subscribe()
|
|
443
|
+
try:
|
|
444
|
+
resp = FfiClient.instance.request(req)
|
|
445
|
+
async_id = getattr(resp, response_field).async_id
|
|
446
|
+
cb: proto_ffi.FfiEvent = await queue.wait_for(
|
|
447
|
+
lambda e: getattr(e, callback_field).async_id == async_id
|
|
448
|
+
)
|
|
449
|
+
finally:
|
|
450
|
+
FfiClient.instance.queue.unsubscribe(queue)
|
|
451
|
+
|
|
452
|
+
if getattr(cb, callback_field).HasField("error"):
|
|
453
|
+
raise StreamError(getattr(cb, callback_field).error.description)
|
|
454
|
+
return cb
|
|
455
|
+
|
|
456
|
+
async def aclose(
|
|
457
|
+
self, *, reason: str = "", attributes: Optional[Dict[str, str]] = None
|
|
458
|
+
) -> None:
|
|
459
|
+
async with self._write_lock:
|
|
460
|
+
if self._closed:
|
|
461
|
+
raise RuntimeError("Stream already closed")
|
|
462
|
+
if self._writer_handle is None:
|
|
463
|
+
raise RuntimeError("Stream is not open")
|
|
464
|
+
self._closed = True
|
|
465
|
+
# unregister before sending: the request consumes the handle natively,
|
|
466
|
+
# so it must not be dropped again by the disconnect cleanup even if the
|
|
467
|
+
# close itself reports an error
|
|
468
|
+
self._unregister_open()
|
|
469
|
+
await self._send_close(reason=reason, attributes=attributes)
|
|
470
|
+
|
|
471
|
+
async def _send_close(self, *, reason: str, attributes: Optional[Dict[str, str]]) -> None:
|
|
472
|
+
raise NotImplementedError
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
class TextStreamWriter(BaseStreamWriter):
|
|
476
|
+
def __init__(
|
|
477
|
+
self,
|
|
478
|
+
local_participant: LocalParticipant,
|
|
479
|
+
*,
|
|
480
|
+
topic: str = "",
|
|
481
|
+
attributes: Optional[Dict[str, str]] = None,
|
|
482
|
+
stream_id: str | None = None,
|
|
483
|
+
total_size: int | None = None,
|
|
484
|
+
reply_to_id: str | None = None,
|
|
485
|
+
destination_identities: Optional[List[str]] = None,
|
|
486
|
+
sender_identity: str | None = None,
|
|
487
|
+
) -> None:
|
|
488
|
+
super().__init__(
|
|
489
|
+
local_participant,
|
|
490
|
+
topic,
|
|
491
|
+
attributes,
|
|
492
|
+
stream_id,
|
|
493
|
+
total_size,
|
|
494
|
+
mime_type="text/plain",
|
|
495
|
+
destination_identities=destination_identities,
|
|
496
|
+
sender_identity=sender_identity,
|
|
497
|
+
)
|
|
498
|
+
options = proto_data_stream.StreamTextOptions(topic=topic)
|
|
499
|
+
if attributes:
|
|
500
|
+
options.attributes.update(attributes)
|
|
501
|
+
if destination_identities:
|
|
502
|
+
options.destination_identities.extend(destination_identities)
|
|
503
|
+
if stream_id is not None:
|
|
504
|
+
options.id = stream_id
|
|
505
|
+
if reply_to_id:
|
|
506
|
+
options.reply_to_stream_id = reply_to_id
|
|
507
|
+
options.sender_identity = self._sender_identity
|
|
508
|
+
self._options = options
|
|
509
|
+
# provisional info, replaced by the FFI-provided info once opened
|
|
510
|
+
self._info = TextStreamInfo(
|
|
511
|
+
stream_id=stream_id or "",
|
|
512
|
+
mime_type="text/plain",
|
|
513
|
+
topic=topic,
|
|
514
|
+
timestamp=self._provisional_timestamp(),
|
|
515
|
+
size=total_size,
|
|
516
|
+
attributes=dict(attributes) if attributes else {},
|
|
517
|
+
attachments=[],
|
|
518
|
+
)
|
|
519
|
+
|
|
520
|
+
async def _send_header(self) -> None:
|
|
521
|
+
req = proto_ffi.FfiRequest()
|
|
522
|
+
req.text_stream_open.local_participant_handle = self._local_participant._ffi_handle.handle
|
|
523
|
+
req.text_stream_open.options.CopyFrom(self._options)
|
|
524
|
+
cb = await self._wait_for_callback(req, "text_stream_open", "text_stream_open")
|
|
525
|
+
owned = cb.text_stream_open.writer
|
|
526
|
+
self._register_open(owned.handle.id)
|
|
527
|
+
self._info = _text_stream_info_from_proto(owned.info)
|
|
528
|
+
if not self._info.size and self._total_size is not None:
|
|
529
|
+
# total_size is not transmitted for text streams; keep it local
|
|
530
|
+
self._info.size = self._total_size
|
|
531
|
+
|
|
532
|
+
async def write(self, text: str) -> None:
|
|
533
|
+
async with self._write_lock:
|
|
534
|
+
if self._closed:
|
|
535
|
+
raise RuntimeError("Cannot write after stream is closed")
|
|
536
|
+
if self._writer_handle is None:
|
|
537
|
+
raise RuntimeError("Stream is not open")
|
|
538
|
+
req = proto_ffi.FfiRequest()
|
|
539
|
+
req.text_stream_write.writer_handle = self._writer_handle
|
|
540
|
+
req.text_stream_write.text = text
|
|
541
|
+
await self._wait_for_callback(req, "text_stream_writer_write", "text_stream_write")
|
|
542
|
+
|
|
543
|
+
async def _send_close(self, *, reason: str, attributes: Optional[Dict[str, str]]) -> None:
|
|
544
|
+
assert self._writer_handle is not None
|
|
545
|
+
req = proto_ffi.FfiRequest()
|
|
546
|
+
req.text_stream_close.writer_handle = self._writer_handle
|
|
547
|
+
req.text_stream_close.reason = reason
|
|
548
|
+
if attributes:
|
|
549
|
+
req.text_stream_close.attributes.update(attributes)
|
|
550
|
+
await self._wait_for_callback(req, "text_stream_writer_close", "text_stream_close")
|
|
551
|
+
|
|
552
|
+
@property
|
|
553
|
+
def info(self) -> TextStreamInfo:
|
|
554
|
+
return self._info
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
class ByteStreamWriter(BaseStreamWriter):
|
|
558
|
+
def __init__(
|
|
559
|
+
self,
|
|
560
|
+
local_participant: LocalParticipant,
|
|
561
|
+
*,
|
|
562
|
+
name: str,
|
|
563
|
+
topic: str = "",
|
|
564
|
+
attributes: Optional[Dict[str, str]] = None,
|
|
565
|
+
stream_id: str | None = None,
|
|
566
|
+
total_size: int | None = None,
|
|
567
|
+
mime_type: str = "application/octet-stream",
|
|
568
|
+
destination_identities: Optional[List[str]] = None,
|
|
569
|
+
) -> None:
|
|
570
|
+
super().__init__(
|
|
571
|
+
local_participant,
|
|
572
|
+
topic,
|
|
573
|
+
attributes,
|
|
574
|
+
stream_id,
|
|
575
|
+
total_size,
|
|
576
|
+
mime_type=mime_type,
|
|
577
|
+
destination_identities=destination_identities,
|
|
578
|
+
)
|
|
579
|
+
options = proto_data_stream.StreamByteOptions(
|
|
580
|
+
topic=topic,
|
|
581
|
+
name=name,
|
|
582
|
+
mime_type=mime_type,
|
|
583
|
+
)
|
|
584
|
+
if attributes:
|
|
585
|
+
options.attributes.update(attributes)
|
|
586
|
+
if destination_identities:
|
|
587
|
+
options.destination_identities.extend(destination_identities)
|
|
588
|
+
if stream_id is not None:
|
|
589
|
+
options.id = stream_id
|
|
590
|
+
if total_size is not None:
|
|
591
|
+
options.total_length = total_size
|
|
592
|
+
options.sender_identity = self._sender_identity
|
|
593
|
+
self._options = options
|
|
594
|
+
# provisional info, replaced by the FFI-provided info once opened
|
|
595
|
+
self._info = ByteStreamInfo(
|
|
596
|
+
stream_id=stream_id or "",
|
|
597
|
+
mime_type=mime_type,
|
|
598
|
+
topic=topic,
|
|
599
|
+
timestamp=self._provisional_timestamp(),
|
|
600
|
+
size=total_size,
|
|
601
|
+
attributes=dict(attributes) if attributes else {},
|
|
602
|
+
name=name,
|
|
603
|
+
)
|
|
604
|
+
|
|
605
|
+
async def _send_header(self) -> None:
|
|
606
|
+
req = proto_ffi.FfiRequest()
|
|
607
|
+
req.byte_stream_open.local_participant_handle = self._local_participant._ffi_handle.handle
|
|
608
|
+
req.byte_stream_open.options.CopyFrom(self._options)
|
|
609
|
+
cb = await self._wait_for_callback(req, "byte_stream_open", "byte_stream_open")
|
|
610
|
+
owned = cb.byte_stream_open.writer
|
|
611
|
+
self._register_open(owned.handle.id)
|
|
612
|
+
self._info = _byte_stream_info_from_proto(owned.info)
|
|
613
|
+
|
|
614
|
+
async def write(self, data: bytes) -> None:
|
|
615
|
+
async with self._write_lock:
|
|
616
|
+
if self._closed:
|
|
617
|
+
raise RuntimeError("Cannot write after stream is closed")
|
|
618
|
+
if self._writer_handle is None:
|
|
619
|
+
raise RuntimeError("Stream is not open")
|
|
620
|
+
req = proto_ffi.FfiRequest()
|
|
621
|
+
req.byte_stream_write.writer_handle = self._writer_handle
|
|
622
|
+
req.byte_stream_write.bytes = data
|
|
623
|
+
await self._wait_for_callback(req, "byte_stream_writer_write", "byte_stream_write")
|
|
624
|
+
|
|
625
|
+
async def _send_close(self, *, reason: str, attributes: Optional[Dict[str, str]]) -> None:
|
|
626
|
+
assert self._writer_handle is not None
|
|
627
|
+
req = proto_ffi.FfiRequest()
|
|
628
|
+
req.byte_stream_close.writer_handle = self._writer_handle
|
|
629
|
+
req.byte_stream_close.reason = reason
|
|
630
|
+
if attributes:
|
|
631
|
+
req.byte_stream_close.attributes.update(attributes)
|
|
632
|
+
await self._wait_for_callback(req, "byte_stream_writer_close", "byte_stream_close")
|
|
633
|
+
|
|
634
|
+
@property
|
|
635
|
+
def info(self) -> ByteStreamInfo:
|
|
636
|
+
return self._info
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
TextStreamHandler = Callable[[TextStreamReader, str], None]
|
|
640
|
+
ByteStreamHandler = Callable[[ByteStreamReader, str], None]
|