5
5
6
6
from livekit import rtc
7
7
8
- URL = ' ws://localhost:7880'
9
- TOKEN = ' eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE5MDY2MTMyODgsImlzcyI6IkFQSVRzRWZpZFpqclFvWSIsIm5hbWUiOiJuYXRpdmUiLCJuYmYiOjE2NzI2MTMyODgsInN1YiI6Im5hdGl2ZSIsInZpZGVvIjp7InJvb20iOiJ0ZXN0Iiwicm9vbUFkbWluIjp0cnVlLCJyb29tQ3JlYXRlIjp0cnVlLCJyb29tSm9pbiI6dHJ1ZSwicm9vbUxpc3QiOnRydWV9fQ.uSNIangMRu8jZD5mnRYoCHjcsQWCrJXgHCs0aNIgBFY' # noqa
8
+ URL = " ws://localhost:7880"
9
+ TOKEN = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE5MDY2MTMyODgsImlzcyI6IkFQSVRzRWZpZFpqclFvWSIsIm5hbWUiOiJuYXRpdmUiLCJuYmYiOjE2NzI2MTMyODgsInN1YiI6Im5hdGl2ZSIsInZpZGVvIjp7InJvb20iOiJ0ZXN0Iiwicm9vbUFkbWluIjp0cnVlLCJyb29tQ3JlYXRlIjp0cnVlLCJyb29tSm9pbiI6dHJ1ZSwicm9vbUxpc3QiOnRydWV9fQ.uSNIangMRu8jZD5mnRYoCHjcsQWCrJXgHCs0aNIgBFY" # noqa
10
10
11
11
12
12
async def main (room : rtc .Room ) -> None :
13
-
14
- @room .listens_to ("participant_connected" )
13
+ @room .on ("participant_connected" )
15
14
def on_participant_connected (participant : rtc .RemoteParticipant ) -> None :
16
15
logging .info (
17
- "participant connected: %s %s" , participant .sid , participant .identity )
16
+ "participant connected: %s %s" , participant .sid , participant .identity
17
+ )
18
18
19
- @room .listens_to ("participant_disconnected" )
19
+ @room .on ("participant_disconnected" )
20
20
def on_participant_disconnected (participant : rtc .RemoteParticipant ):
21
- logging .info ("participant disconnected: %s %s" ,
22
- participant .sid , participant .identity )
23
-
24
- @room .listens_to ("local_track_published" )
25
- def on_local_track_published (publication : rtc .LocalTrackPublication ,
26
- track : Union [rtc .LocalAudioTrack ,
27
- rtc .LocalVideoTrack ]):
21
+ logging .info (
22
+ "participant disconnected: %s %s" , participant .sid , participant .identity
23
+ )
24
+
25
+ @room .on ("local_track_published" )
26
+ def on_local_track_published (
27
+ publication : rtc .LocalTrackPublication ,
28
+ track : Union [rtc .LocalAudioTrack , rtc .LocalVideoTrack ],
29
+ ):
28
30
logging .info ("local track published: %s" , publication .sid )
29
31
30
- @room .listens_to ("active_speakers_changed" )
32
+ @room .on ("active_speakers_changed" )
31
33
def on_active_speakers_changed (speakers : list [rtc .Participant ]):
32
34
logging .info ("active speakers changed: %s" , speakers )
33
35
34
- @room .listens_to ("local_track_unpublished" )
36
+ @room .on ("local_track_unpublished" )
35
37
def on_local_track_unpublished (publication : rtc .LocalTrackPublication ):
36
38
logging .info ("local track unpublished: %s" , publication .sid )
37
39
38
- @room .listens_to ("track_published" )
39
- def on_track_published (publication : rtc .RemoteTrackPublication ,
40
- participant : rtc .RemoteParticipant ):
41
- logging .info ("track published: %s from participant %s (%s)" ,
42
- publication .sid , participant .sid , participant .identity )
43
-
44
- @room .listens_to ("track_unpublished" )
45
- def on_track_unpublished (publication : rtc .RemoteTrackPublication ,
46
- participant : rtc .RemoteParticipant ):
40
+ @room .on ("track_published" )
41
+ def on_track_published (
42
+ publication : rtc .RemoteTrackPublication , participant : rtc .RemoteParticipant
43
+ ):
44
+ logging .info (
45
+ "track published: %s from participant %s (%s)" ,
46
+ publication .sid ,
47
+ participant .sid ,
48
+ participant .identity ,
49
+ )
50
+
51
+ @room .on ("track_unpublished" )
52
+ def on_track_unpublished (
53
+ publication : rtc .RemoteTrackPublication , participant : rtc .RemoteParticipant
54
+ ):
47
55
logging .info ("track unpublished: %s" , publication .sid )
48
56
49
- @room .listens_to ("track_subscribed" )
50
- def on_track_subscribed (track : rtc .Track ,
51
- publication : rtc .RemoteTrackPublication ,
52
- participant : rtc .RemoteParticipant ):
57
+ @room .on ("track_subscribed" )
58
+ def on_track_subscribed (
59
+ track : rtc .Track ,
60
+ publication : rtc .RemoteTrackPublication ,
61
+ participant : rtc .RemoteParticipant ,
62
+ ):
53
63
logging .info ("track subscribed: %s" , publication .sid )
54
64
if track .kind == rtc .TrackKind .KIND_VIDEO :
55
65
_video_stream = rtc .VideoStream (track )
@@ -59,57 +69,61 @@ def on_track_subscribed(track: rtc.Track,
59
69
_audio_stream = rtc .AudioStream (track )
60
70
# audio_stream is an async iterator that yields AudioFrame
61
71
62
- @room .listens_to ("track_unsubscribed" )
63
- def on_track_unsubscribed (track : rtc .Track ,
64
- publication : rtc .RemoteTrackPublication ,
65
- participant : rtc .RemoteParticipant ):
72
+ @room .on ("track_unsubscribed" )
73
+ def on_track_unsubscribed (
74
+ track : rtc .Track ,
75
+ publication : rtc .RemoteTrackPublication ,
76
+ participant : rtc .RemoteParticipant ,
77
+ ):
66
78
logging .info ("track unsubscribed: %s" , publication .sid )
67
79
68
- @room .listens_to ("track_muted" )
69
- def on_track_muted (publication : rtc .RemoteTrackPublication ,
70
- participant : rtc .RemoteParticipant ):
80
+ @room .on ("track_muted" )
81
+ def on_track_muted (
82
+ publication : rtc .RemoteTrackPublication , participant : rtc .RemoteParticipant
83
+ ):
71
84
logging .info ("track muted: %s" , publication .sid )
72
85
73
- @room .listens_to ("track_unmuted" )
74
- def on_track_unmuted (publication : rtc .RemoteTrackPublication ,
75
- participant : rtc .RemoteParticipant ):
86
+ @room .on ("track_unmuted" )
87
+ def on_track_unmuted (
88
+ publication : rtc .RemoteTrackPublication , participant : rtc .RemoteParticipant
89
+ ):
76
90
logging .info ("track unmuted: %s" , publication .sid )
77
91
78
- @room .listens_to ("data_received" )
79
- def on_data_received (data : bytes ,
80
- kind : rtc .DataPacketKind ,
81
- participant : rtc . Participant ):
92
+ @room .on ("data_received" )
93
+ def on_data_received (
94
+ data : bytes , kind : rtc .DataPacketKind , participant : rtc . Participant
95
+ ):
82
96
logging .info ("received data from %s: %s" , participant .identity , data )
83
97
84
- @room .listens_to ("connection_quality_changed" )
85
- def on_connection_quality_changed (participant : rtc .Participant ,
86
- quality : rtc .ConnectionQuality ):
98
+ @room .on ("connection_quality_changed" )
99
+ def on_connection_quality_changed (
100
+ participant : rtc .Participant , quality : rtc .ConnectionQuality
101
+ ):
87
102
logging .info ("connection quality changed for %s" , participant .identity )
88
103
89
- @room .listens_to ("track_subscription_failed" )
90
- def on_track_subscription_failed (participant : rtc .RemoteParticipant ,
91
- track_sid : str ,
92
- error : str ):
93
- logging .info ("track subscription failed: %s %s" ,
94
- participant .identity , error )
104
+ @room .on ("track_subscription_failed" )
105
+ def on_track_subscription_failed (
106
+ participant : rtc .RemoteParticipant , track_sid : str , error : str
107
+ ):
108
+ logging .info ("track subscription failed: %s %s" , participant .identity , error )
95
109
96
- @room .listens_to ("connection_state_changed" )
110
+ @room .on ("connection_state_changed" )
97
111
def on_connection_state_changed (state : rtc .ConnectionState ):
98
112
logging .info ("connection state changed: %s" , state )
99
113
100
- @room .listens_to ("connected" )
114
+ @room .on ("connected" )
101
115
def on_connected () -> None :
102
116
logging .info ("connected" )
103
117
104
- @room .listens_to ("disconnected" )
118
+ @room .on ("disconnected" )
105
119
def on_disconnected () -> None :
106
120
logging .info ("disconnected" )
107
121
108
- @room .listens_to ("reconnecting" )
122
+ @room .on ("reconnecting" )
109
123
def on_reconnecting () -> None :
110
124
logging .info ("reconnecting" )
111
125
112
- @room .listens_to ("reconnected" )
126
+ @room .on ("reconnected" )
113
127
def on_reconnected () -> None :
114
128
logging .info ("reconnected" )
115
129
@@ -122,9 +136,10 @@ def on_reconnected() -> None:
122
136
123
137
124
138
if __name__ == "__main__" :
125
- logging .basicConfig (level = logging .INFO , handlers = [
126
- logging .FileHandler ("basic_room.log" ),
127
- logging .StreamHandler ()])
139
+ logging .basicConfig (
140
+ level = logging .INFO ,
141
+ handlers = [logging .FileHandler ("basic_room.log" ), logging .StreamHandler ()],
142
+ )
128
143
129
144
loop = asyncio .get_event_loop ()
130
145
room = rtc .Room (loop = loop )
@@ -135,8 +150,7 @@ async def cleanup():
135
150
136
151
asyncio .ensure_future (main (room ))
137
152
for signal in [SIGINT , SIGTERM ]:
138
- loop .add_signal_handler (
139
- signal , lambda : asyncio .ensure_future (cleanup ()))
153
+ loop .add_signal_handler (signal , lambda : asyncio .ensure_future (cleanup ()))
140
154
141
155
try :
142
156
loop .run_forever ()
0 commit comments