Skip to content

Commit e04c6e3

Browse files
committed
expose stream_file
1 parent f3dc6b6 commit e04c6e3

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

livekit-rtc/livekit/rtc/data_stream.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ def __init__(
141141
extensions: Optional[Dict[str, str]] = {},
142142
stream_id: str | None = None,
143143
total_size: int | None = None,
144+
mime_type: str = "",
144145
):
145146
self._local_participant = local_participant
146147
if stream_id is None:
@@ -149,7 +150,7 @@ def __init__(
149150
self._header = proto_DataStream.Header(
150151
stream_id=stream_id,
151152
timestamp=timestamp,
152-
mime_type="text/plain",
153+
mime_type=mime_type,
153154
topic=topic,
154155
extensions=extensions,
155156
total_length=total_size,
@@ -234,7 +235,14 @@ def __init__(
234235
total_size: int | None = None,
235236
reply_to_id: str | None = None,
236237
) -> None:
237-
super().__init__(local_participant, topic, extensions, stream_id, total_size)
238+
super().__init__(
239+
local_participant,
240+
topic,
241+
extensions,
242+
stream_id,
243+
total_size,
244+
mime_type="text/plain",
245+
)
238246
if reply_to_id:
239247
self._header.text_header.reply_to_stream_id = reply_to_id
240248
self._info = TextStreamInfo(
@@ -248,13 +256,16 @@ def __init__(
248256
)
249257

250258
async def write(self, text: str, chunk_index: int | None = None):
259+
content = text.encode()
260+
if len(content) > STREAM_CHUNK_SIZE:
261+
raise ValueError("maximum chunk size exceeded")
251262
if chunk_index is None:
252263
chunk_index = self._next_chunk_index
253264
self._next_chunk_index += 1
254265
chunk_msg = proto_DataStream.Chunk(
255266
stream_id=self._header.stream_id,
256267
chunk_index=chunk_index,
257-
content=text.encode(),
268+
content=content,
258269
)
259270
await self._send_chunk(chunk_msg)
260271

@@ -272,8 +283,16 @@ def __init__(
272283
extensions: Optional[Dict[str, str]] = {},
273284
stream_id: str | None = None,
274285
total_size: int | None = None,
286+
mime_type: str = "",
275287
) -> None:
276-
super().__init__(local_participant, topic, extensions, stream_id, total_size)
288+
super().__init__(
289+
local_participant,
290+
topic,
291+
extensions,
292+
stream_id,
293+
total_size,
294+
mime_type=mime_type,
295+
)
277296
self._header.file_header.file_name = file_name
278297
self._info = FileStreamInfo(
279298
id=self._header.stream_id,
@@ -286,6 +305,9 @@ def __init__(
286305
)
287306

288307
async def write(self, data: bytes, chunk_index: int | None = None):
308+
if len(data) > STREAM_CHUNK_SIZE:
309+
raise ValueError("maximum chunk size exceeded")
310+
289311
if chunk_index is None:
290312
chunk_index = self._next_chunk_index
291313
self._next_chunk_index += 1

livekit-rtc/livekit/rtc/participant.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -559,27 +559,24 @@ async def stream_text(
559559

560560
return writer
561561

562-
async def send_file(
562+
async def stream_file(
563563
self,
564-
path_to_file: str,
565564
file_name: str,
565+
file_size: int | None = None,
566+
mime_type: str = "",
566567
extensions: Dict[str, str] = {},
567568
stream_id: str | None = None,
568569
):
569-
file_size = os.stat(path_to_file).st_size
570570
writer = FileStreamWriter(
571571
self,
572572
file_name=file_name,
573573
extensions=extensions,
574574
total_size=file_size,
575575
stream_id=stream_id,
576+
mime_type=mime_type,
576577
)
577-
with open(path_to_file, "rb") as f:
578-
while bytes := f.read(STREAM_CHUNK_SIZE):
579-
await writer.write(bytes)
580-
await writer.close()
581578

582-
return writer.info
579+
return writer
583580

584581
async def publish_track(
585582
self, track: LocalTrack, options: TrackPublishOptions = TrackPublishOptions()

0 commit comments

Comments
 (0)