Skip to content

Commit df4de63

Browse files
committed
fix bugs and add text stream example
1 parent 521179d commit df4de63

File tree

17 files changed

+731
-170
lines changed

17 files changed

+731
-170
lines changed

examples/data_streams.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import os
2+
import logging
3+
import asyncio
4+
from signal import SIGINT, SIGTERM
5+
from livekit import rtc
6+
7+
# Set the following environment variables with your own values
8+
TOKEN = os.environ.get("LIVEKIT_TOKEN")
9+
URL = os.environ.get("LIVEKIT_URL")
10+
11+
12+
async def main(room: rtc.Room):
13+
logging.basicConfig(level=logging.INFO)
14+
logger = logging.getLogger(__name__)
15+
16+
async def greetParticipant(identity: str):
17+
text_writer = await room.local_participant.stream_text(
18+
destination_identities=[identity]
19+
)
20+
for char in "Hi! Just a friendly message":
21+
await text_writer.write(char)
22+
await text_writer.close()
23+
24+
async def on_text_received(reader: rtc.TextStreamReader):
25+
full_text = await reader.read_all()
26+
logger.info(full_text)
27+
28+
@room.on("participant_connected")
29+
def on_participant_connected(participant: rtc.RemoteParticipant):
30+
logger.info(
31+
"participant connected: %s %s", participant.sid, participant.identity
32+
)
33+
asyncio.create_task(greetParticipant(participant.identity))
34+
35+
# track_subscribed is emitted whenever the local participant is subscribed to a new track
36+
@room.on("text_stream_received")
37+
def on_text_stream_received(
38+
reader: rtc.TextStreamReader,
39+
participant_identity: str,
40+
):
41+
logger.info("text stream received from: %s", participant_identity)
42+
asyncio.create_task(on_text_received(reader=reader))
43+
44+
# By default, autosubscribe is enabled. The participant will be subscribed to
45+
# all published tracks in the room
46+
await room.connect(URL, TOKEN)
47+
logger.info("connected to room %s", room.name)
48+
49+
for identity, participant in room.remote_participants.items():
50+
print("Sending a welcome message to %s", participant.identity)
51+
await greetParticipant(participant.identity)
52+
53+
logger.info("exiting")
54+
55+
56+
if __name__ == "__main__":
57+
logging.basicConfig(
58+
level=logging.INFO,
59+
handlers=[logging.FileHandler("basic_room.log"), logging.StreamHandler()],
60+
)
61+
62+
loop = asyncio.get_event_loop()
63+
room = rtc.Room(loop=loop)
64+
65+
async def cleanup():
66+
await room.disconnect()
67+
loop.stop()
68+
69+
asyncio.ensure_future(main(room))
70+
for signal in [SIGINT, SIGTERM]:
71+
loop.add_signal_handler(signal, lambda: asyncio.ensure_future(cleanup()))
72+
73+
try:
74+
loop.run_forever()
75+
finally:
76+
loop.close()

livekit-rtc/livekit/rtc/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@
7474
from .utils import combine_audio_frames
7575
from .rpc import RpcError, RpcInvocationData
7676
from .synchronizer import AVSynchronizer
77+
from .data_stream import (
78+
TextStreamInfo,
79+
TextStreamUpdate,
80+
FileStreamInfo,
81+
TextStreamReader,
82+
TextStreamWriter,
83+
)
7784

7885
__all__ = [
7986
"ConnectionQuality",
@@ -140,5 +147,10 @@
140147
"EventEmitter",
141148
"combine_audio_frames",
142149
"AVSynchronizer",
150+
"TextStreamUpdate",
151+
"TextStreamInfo",
152+
"FileStreamInfo",
153+
"TextStreamReader",
154+
"TextStreamWriter",
143155
"__version__",
144156
]

livekit-rtc/livekit/rtc/_proto/audio_frame_pb2.py

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

livekit-rtc/livekit/rtc/_proto/e2ee_pb2.py

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

livekit-rtc/livekit/rtc/_proto/ffi_pb2.py

Lines changed: 31 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

livekit-rtc/livekit/rtc/_proto/ffi_pb2.pyi

Lines changed: 49 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

livekit-rtc/livekit/rtc/_proto/handle_pb2.py

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

livekit-rtc/livekit/rtc/_proto/participant_pb2.py

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

livekit-rtc/livekit/rtc/_proto/room_pb2.py

Lines changed: 135 additions & 95 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)