Skip to content

Commit 318b8d4

Browse files
committed
add send_file method
1 parent 61eb7fd commit 318b8d4

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

livekit-rtc/livekit/rtc/data_stream.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ def __init__(
143143
stream_id: str | None = None,
144144
total_size: int | None = None,
145145
mime_type: str = "",
146+
destination_identities: List[str] = [],
146147
):
147148
self._local_participant = local_participant
148149
if stream_id is None:
@@ -157,13 +158,15 @@ def __init__(
157158
total_length=total_size,
158159
)
159160
self._next_chunk_index: int = 0
161+
self._destination_identities = destination_identities
160162

161-
async def _send_header(self, destination_identities: List[str] = []):
163+
async def _send_header(self):
162164
req = proto_ffi.FfiRequest(
163165
send_stream_header=proto_room.SendStreamHeaderRequest(
164166
header=self._header,
165167
local_participant_handle=self._local_participant._ffi_handle.handle,
166-
destination_identities=destination_identities,
168+
destination_identities=self._destination_identities,
169+
sender_identity=self._local_participant.identity,
167170
)
168171
)
169172

@@ -185,6 +188,8 @@ async def _send_chunk(self, chunk: proto_DataStream.Chunk):
185188
send_stream_chunk=proto_room.SendStreamChunkRequest(
186189
chunk=chunk,
187190
local_participant_handle=self._local_participant._ffi_handle.handle,
191+
sender_identity=self._local_participant.identity,
192+
destination_identities=self._destination_identities,
188193
)
189194
)
190195

@@ -235,6 +240,7 @@ def __init__(
235240
stream_id: str | None = None,
236241
total_size: int | None = None,
237242
reply_to_id: str | None = None,
243+
destination_identities: List[str] = [],
238244
) -> None:
239245
super().__init__(
240246
local_participant,
@@ -243,6 +249,7 @@ def __init__(
243249
stream_id,
244250
total_size,
245251
mime_type="text/plain",
252+
destination_identities=destination_identities,
246253
)
247254
if reply_to_id:
248255
self._header.text_header.reply_to_stream_id = reply_to_id
@@ -285,6 +292,7 @@ def __init__(
285292
stream_id: str | None = None,
286293
total_size: int | None = None,
287294
mime_type: str = "",
295+
destination_identities: List[str] = [],
288296
) -> None:
289297
super().__init__(
290298
local_participant,
@@ -293,6 +301,7 @@ def __init__(
293301
stream_id,
294302
total_size,
295303
mime_type=mime_type,
304+
destination_identities=destination_identities,
296305
)
297306
self._header.file_header.file_name = file_name
298307
self._info = FileStreamInfo(

livekit-rtc/livekit/rtc/participant.py

+42-4
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
from __future__ import annotations
1616

1717
import ctypes
18+
import asyncio
19+
import os
20+
import mimetypes
21+
import aiofiles
1822
from typing import List, Union, Callable, Dict, Awaitable, Optional, Mapping, cast
1923
from abc import abstractmethod, ABC
2024

25+
2126
from ._ffi_client import FfiClient, FfiHandle
2227
from ._proto import ffi_pb2 as proto_ffi
2328
from ._proto import participant_pb2 as proto_participant
@@ -38,10 +43,9 @@
3843
from .rpc import RpcError
3944
from ._proto.rpc_pb2 import RpcMethodInvocationResponseRequest
4045
from .log import logger
41-
import asyncio
4246

4347
from .rpc import RpcInvocationData
44-
from .data_stream import TextStreamWriter, FileStreamWriter
48+
from .data_stream import TextStreamWriter, FileStreamWriter, STREAM_CHUNK_SIZE
4549

4650

4751
class PublishTrackError(Exception):
@@ -552,19 +556,21 @@ async def stream_text(
552556
topic=topic,
553557
extensions=extensions,
554558
reply_to_id=reply_to_id,
559+
destination_identities=destination_identities,
555560
)
556561

557-
await writer._send_header(destination_identities=destination_identities)
562+
await writer._send_header()
558563

559564
return writer
560565

561566
async def stream_file(
562567
self,
563568
file_name: str,
564569
file_size: int | None = None,
565-
mime_type: str = "",
570+
mime_type: str = "application/octet-stream",
566571
extensions: Dict[str, str] = {},
567572
stream_id: str | None = None,
573+
destination_identities: List[str] = [],
568574
):
569575
writer = FileStreamWriter(
570576
self,
@@ -573,10 +579,42 @@ async def stream_file(
573579
total_size=file_size,
574580
stream_id=stream_id,
575581
mime_type=mime_type,
582+
destination_identities=destination_identities,
576583
)
577584

585+
await writer._send_header()
586+
578587
return writer
579588

589+
async def send_file(
590+
self,
591+
file_path: str,
592+
destination_identities: List[str] = [],
593+
extensions: Dict[str, str] = {},
594+
stream_id: str | None = None,
595+
):
596+
file_size = os.path.getsize(file_path)
597+
file_name = os.path.basename(file_path)
598+
mime_type, _ = mimetypes.guess_type(file_path)
599+
if mime_type is None:
600+
mime_type = (
601+
"application/octet-stream" # Fallback MIME type for unknown files
602+
)
603+
604+
writer: FileStreamWriter = await self.stream_file(
605+
file_name=file_name,
606+
file_size=file_size,
607+
mime_type=mime_type,
608+
extensions=extensions,
609+
stream_id=stream_id,
610+
destination_identities=destination_identities,
611+
)
612+
613+
async with aiofiles.open(file_path, "rb") as f:
614+
while bytes := await f.read(STREAM_CHUNK_SIZE):
615+
await writer.write(bytes)
616+
await writer.close()
617+
580618
async def publish_track(
581619
self, track: LocalTrack, options: TrackPublishOptions = TrackPublishOptions()
582620
) -> LocalTrackPublication:

0 commit comments

Comments
 (0)