Skip to content

Commit 49c8efb

Browse files
committed
Fix data stream example
1 parent ea42eb8 commit 49c8efb

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

examples/data-streams/data_streams.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ async def main(room: rtc.Room):
1313
logging.basicConfig(level=logging.INFO)
1414
logger = logging.getLogger(__name__)
1515

16+
active_tasks = []
17+
1618
async def greetParticipant(identity: str):
1719
text_writer = await room.local_participant.stream_text(
1820
destination_identities=[identity], topic="chat"
@@ -32,8 +34,8 @@ async def on_chat_message_received(reader: rtc.TextStreamReader, participant_ide
3234
logger.info("Received chat message from %s: '%s'", participant_identity, full_text)
3335

3436
async def on_welcome_image_received(reader: rtc.ByteStreamReader, participant_identity: str):
35-
logger.info("Received image from %s: '%s'", participant_identity, reader.info["name"])
36-
with open(reader.info["name"], mode="wb") as f:
37+
logger.info("Received image from %s: '%s'", participant_identity, reader.info.name)
38+
with open(reader.info.name, mode="wb") as f:
3739
async for chunk in reader:
3840
f.write(chunk)
3941

@@ -44,19 +46,19 @@ def on_participant_connected(participant: rtc.RemoteParticipant):
4446
logger.info("participant connected: %s %s", participant.sid, participant.identity)
4547
asyncio.create_task(greetParticipant(participant.identity))
4648

47-
room.set_text_stream_handler(
48-
"chat",
49-
lambda reader, participant_identity: asyncio.create_task(
50-
on_chat_message_received(reader, participant_identity)
51-
),
52-
)
49+
def _handle_chat_stream(reader, participant_identity):
50+
task = asyncio.create_task(on_chat_message_received(reader, participant_identity))
51+
active_tasks.append(task)
52+
task.add_done_callback(lambda _: active_tasks.remove(task))
5353

54-
room.set_byte_stream_handler(
55-
"files",
56-
lambda reader, participant_identity: asyncio.create_task(
57-
on_welcome_image_received(reader, participant_identity)
58-
),
59-
)
54+
room.set_text_stream_handler("chat", _handle_chat_stream)
55+
56+
def _handle_welcome_image_stream(reader, participant_identity):
57+
task = asyncio.create_task(on_welcome_image_received(reader, participant_identity))
58+
active_tasks.append(task)
59+
task.add_done_callback(lambda _: active_tasks.remove(task))
60+
61+
room.set_byte_stream_handler("files", _handle_welcome_image_stream)
6062

6163
# By default, autosubscribe is enabled. The participant will be subscribed to
6264
# all published tracks in the room

livekit-rtc/livekit/rtc/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
"""LiveKit RTC SDK"""
15+
"""LiveKit SDK for Python
16+
`pip install livekit`
17+
18+
See https://docs.livekit.io/home/client/connect/#installing-the-livekit-sdk for more information.
19+
"""
1620

1721
from ._proto import stats_pb2 as stats
1822
from ._proto.e2ee_pb2 import EncryptionState, EncryptionType

0 commit comments

Comments
 (0)