Skip to content

Commit d334100

Browse files
committed
Add auto chunking for text stream writer
1 parent fe1d072 commit d334100

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

livekit-rtc/livekit/rtc/data_stream.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from ._proto import ffi_pb2 as proto_ffi
2525
from ._proto import room_pb2 as proto_room
2626
from ._ffi_client import FfiClient
27-
27+
from ._utils import split_utf8
2828
from typing import TYPE_CHECKING
2929

3030
if TYPE_CHECKING:
@@ -287,19 +287,17 @@ def __init__(
287287
attachments=list(self._header.text_header.attached_stream_ids),
288288
)
289289

290-
async def write(self, text: str, chunk_index: int | None = None):
291-
content = text.encode()
292-
if len(content) > STREAM_CHUNK_SIZE:
293-
raise ValueError("maximum chunk size exceeded")
294-
if chunk_index is None:
290+
async def write(self, text: str):
291+
for chunk in split_utf8(text, STREAM_CHUNK_SIZE):
292+
content = chunk.encode()
295293
chunk_index = self._next_chunk_index
296294
self._next_chunk_index += 1
297-
chunk_msg = proto_DataStream.Chunk(
298-
stream_id=self._header.stream_id,
299-
chunk_index=chunk_index,
300-
content=content,
301-
)
302-
await self._send_chunk(chunk_msg)
295+
chunk_msg = proto_DataStream.Chunk(
296+
stream_id=self._header.stream_id,
297+
chunk_index=chunk_index,
298+
content=content,
299+
)
300+
await self._send_chunk(chunk_msg)
303301

304302
@property
305303
def info(self) -> TextStreamInfo:

0 commit comments

Comments
 (0)