-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathbasic_room.py
168 lines (138 loc) · 5.36 KB
/
basic_room.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import asyncio
import logging
from signal import SIGINT, SIGTERM
from typing import Union
import os
from livekit import api, rtc
# ensure LIVEKIT_URL, LIVEKIT_API_KEY, and LIVEKIT_API_SECRET are set
async def main(room: rtc.Room) -> None:
@room.on("participant_connected")
def on_participant_connected(participant: rtc.RemoteParticipant) -> None:
logging.info(
"participant connected: %s %s", participant.sid, participant.identity
)
@room.on("participant_disconnected")
def on_participant_disconnected(participant: rtc.RemoteParticipant):
logging.info(
"participant disconnected: %s %s", participant.sid, participant.identity
)
@room.on("local_track_published")
def on_local_track_published(
publication: rtc.LocalTrackPublication,
track: Union[rtc.LocalAudioTrack, rtc.LocalVideoTrack],
):
logging.info("local track published: %s", publication.sid)
@room.on("active_speakers_changed")
def on_active_speakers_changed(speakers: list[rtc.Participant]):
logging.info("active speakers changed: %s", speakers)
@room.on("local_track_unpublished")
def on_local_track_unpublished(publication: rtc.LocalTrackPublication):
logging.info("local track unpublished: %s", publication.sid)
@room.on("track_published")
def on_track_published(
publication: rtc.RemoteTrackPublication, participant: rtc.RemoteParticipant
):
logging.info(
"track published: %s from participant %s (%s)",
publication.sid,
participant.sid,
participant.identity,
)
@room.on("track_unpublished")
def on_track_unpublished(
publication: rtc.RemoteTrackPublication, participant: rtc.RemoteParticipant
):
logging.info("track unpublished: %s", publication.sid)
@room.on("track_subscribed")
def on_track_subscribed(
track: rtc.Track,
publication: rtc.RemoteTrackPublication,
participant: rtc.RemoteParticipant,
):
logging.info("track subscribed: %s", publication.sid)
if track.kind == rtc.TrackKind.KIND_VIDEO:
_video_stream = rtc.VideoStream(track)
# video_stream is an async iterator that yields VideoFrame
elif track.kind == rtc.TrackKind.KIND_AUDIO:
print("Subscribed to an Audio Track")
_audio_stream = rtc.AudioStream(track)
# audio_stream is an async iterator that yields AudioFrame
@room.on("track_unsubscribed")
def on_track_unsubscribed(
track: rtc.Track,
publication: rtc.RemoteTrackPublication,
participant: rtc.RemoteParticipant,
):
logging.info("track unsubscribed: %s", publication.sid)
@room.on("track_muted")
def on_track_muted(
publication: rtc.RemoteTrackPublication, participant: rtc.RemoteParticipant
):
logging.info("track muted: %s", publication.sid)
@room.on("track_unmuted")
def on_track_unmuted(
publication: rtc.RemoteTrackPublication, participant: rtc.RemoteParticipant
):
logging.info("track unmuted: %s", publication.sid)
@room.on("data_received")
def on_data_received(data: rtc.DataPacket):
logging.info("received data from %s: %s", data.participant.identity, data.data)
@room.on("connection_quality_changed")
def on_connection_quality_changed(
participant: rtc.Participant, quality: rtc.ConnectionQuality
):
logging.info("connection quality changed for %s", participant.identity)
@room.on("track_subscription_failed")
def on_track_subscription_failed(
participant: rtc.RemoteParticipant, track_sid: str, error: str
):
logging.info("track subscription failed: %s %s", participant.identity, error)
@room.on("connection_state_changed")
def on_connection_state_changed(state: rtc.ConnectionState):
logging.info("connection state changed: %s", state)
@room.on("connected")
def on_connected() -> None:
logging.info("connected")
@room.on("disconnected")
def on_disconnected() -> None:
logging.info("disconnected")
@room.on("reconnecting")
def on_reconnecting() -> None:
logging.info("reconnecting")
@room.on("reconnected")
def on_reconnected() -> None:
logging.info("reconnected")
token = (
api.AccessToken()
.with_identity("python-bot")
.with_name("Python Bot")
.with_grants(
api.VideoGrants(
room_join=True,
room="my-room",
)
)
.to_jwt()
)
await room.connect(os.getenv("LIVEKIT_URL"), token)
logging.info("connected to room %s", room.name)
logging.info("participants: %s", room.remote_participants)
await asyncio.sleep(2)
await room.local_participant.publish_data("hello world")
if __name__ == "__main__":
logging.basicConfig(
level=logging.INFO,
handlers=[logging.FileHandler("basic_room.log"), logging.StreamHandler()],
)
loop = asyncio.get_event_loop()
room = rtc.Room(loop=loop)
async def cleanup():
await room.disconnect()
loop.stop()
asyncio.ensure_future(main(room))
for signal in [SIGINT, SIGTERM]:
loop.add_signal_handler(signal, lambda: asyncio.ensure_future(cleanup()))
try:
loop.run_forever()
finally:
loop.close()