Skip to content

Commit

Permalink
bump rust-ffi and split utf8 string properly
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasIO committed Jan 17, 2025
1 parent 6da1b83 commit ff8aa7b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
12 changes: 12 additions & 0 deletions livekit-rtc/livekit/rtc/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,15 @@ def generate_random_base62(length=12):
"""
global _base62_characters
return "".join(random.choice(_base62_characters) for _ in range(length))


# adapted from https://stackoverflow.com/a/6043797
def split_utf8(s: str, n: int):
"""Split UTF-8 s into chunks of maximum length n."""
while len(s) > n:
k = n
while (ord(s[k]) & 0xC0) == 0x80:
k -= 1
yield s[:k]
s = s[k:]
yield s
35 changes: 34 additions & 1 deletion livekit-rtc/livekit/rtc/participant.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from ._proto.room_pb2 import (
TranscriptionSegment as ProtoTranscriptionSegment,
)
from ._utils import BroadcastQueue
from ._utils import BroadcastQueue, split_utf8
from .track import LocalTrack
from .track_publication import (
LocalTrackPublication,
Expand Down Expand Up @@ -555,19 +555,48 @@ async def stream_text(
topic: str = "",
extensions: Dict[str, str] = {},
reply_to_id: str | None = None,
total_size: int | None = None,
) -> TextStreamWriter:
"""
Returns a TextStreamWriter that allows to write individual chunks of text to a text stream.
In most cases where you want to simply send a text message use send_text() instead.
"""
writer = TextStreamWriter(
self,
topic=topic,
extensions=extensions,
reply_to_id=reply_to_id,
destination_identities=destination_identities,
total_size=total_size,
)

await writer._send_header()

return writer

async def send_text(
self,
text: str,
destination_identities: List[str] = [],
topic: str = "",
extensions: Dict[str, str] = {},
reply_to_id: str | None = None,
):
total_size = len(text.encode())
writer = await self.stream_text(
destination_identities=destination_identities,
topic=topic,
extensions=extensions,
reply_to_id=reply_to_id,
total_size=total_size,
)

for chunk in split_utf8(text, STREAM_CHUNK_SIZE):
await writer.write(chunk)
await writer.close()

return writer.info

async def stream_file(
self,
file_name: str,
Expand All @@ -577,6 +606,10 @@ async def stream_file(
stream_id: str | None = None,
destination_identities: List[str] = [],
) -> FileStreamWriter:
"""
Returns a FileStreamWriter that allows to write individual chunks of bytes to a file stream.
In cases where you want to simply send a file from the file system use send_file() instead.
"""
writer = FileStreamWriter(
self,
file_name=file_name,
Expand Down
2 changes: 1 addition & 1 deletion livekit-rtc/rust-sdks
Submodule rust-sdks updated 109 files

0 comments on commit ff8aa7b

Please sign in to comment.