Skip to content

Commit ff8aa7b

Browse files
committed
bump rust-ffi and split utf8 string properly
1 parent 6da1b83 commit ff8aa7b

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

livekit-rtc/livekit/rtc/_utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,15 @@ def generate_random_base62(length=12):
130130
"""
131131
global _base62_characters
132132
return "".join(random.choice(_base62_characters) for _ in range(length))
133+
134+
135+
# adapted from https://stackoverflow.com/a/6043797
136+
def split_utf8(s: str, n: int):
137+
"""Split UTF-8 s into chunks of maximum length n."""
138+
while len(s) > n:
139+
k = n
140+
while (ord(s[k]) & 0xC0) == 0x80:
141+
k -= 1
142+
yield s[:k]
143+
s = s[k:]
144+
yield s

livekit-rtc/livekit/rtc/participant.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from ._proto.room_pb2 import (
3333
TranscriptionSegment as ProtoTranscriptionSegment,
3434
)
35-
from ._utils import BroadcastQueue
35+
from ._utils import BroadcastQueue, split_utf8
3636
from .track import LocalTrack
3737
from .track_publication import (
3838
LocalTrackPublication,
@@ -555,19 +555,48 @@ async def stream_text(
555555
topic: str = "",
556556
extensions: Dict[str, str] = {},
557557
reply_to_id: str | None = None,
558+
total_size: int | None = None,
558559
) -> TextStreamWriter:
560+
"""
561+
Returns a TextStreamWriter that allows to write individual chunks of text to a text stream.
562+
In most cases where you want to simply send a text message use send_text() instead.
563+
"""
559564
writer = TextStreamWriter(
560565
self,
561566
topic=topic,
562567
extensions=extensions,
563568
reply_to_id=reply_to_id,
564569
destination_identities=destination_identities,
570+
total_size=total_size,
565571
)
566572

567573
await writer._send_header()
568574

569575
return writer
570576

577+
async def send_text(
578+
self,
579+
text: str,
580+
destination_identities: List[str] = [],
581+
topic: str = "",
582+
extensions: Dict[str, str] = {},
583+
reply_to_id: str | None = None,
584+
):
585+
total_size = len(text.encode())
586+
writer = await self.stream_text(
587+
destination_identities=destination_identities,
588+
topic=topic,
589+
extensions=extensions,
590+
reply_to_id=reply_to_id,
591+
total_size=total_size,
592+
)
593+
594+
for chunk in split_utf8(text, STREAM_CHUNK_SIZE):
595+
await writer.write(chunk)
596+
await writer.close()
597+
598+
return writer.info
599+
571600
async def stream_file(
572601
self,
573602
file_name: str,
@@ -577,6 +606,10 @@ async def stream_file(
577606
stream_id: str | None = None,
578607
destination_identities: List[str] = [],
579608
) -> FileStreamWriter:
609+
"""
610+
Returns a FileStreamWriter that allows to write individual chunks of bytes to a file stream.
611+
In cases where you want to simply send a file from the file system use send_file() instead.
612+
"""
580613
writer = FileStreamWriter(
581614
self,
582615
file_name=file_name,

livekit-rtc/rust-sdks

Submodule rust-sdks updated 109 files

0 commit comments

Comments
 (0)