@@ -45,15 +45,17 @@ type ParticipantInfo struct {
45
45
46
46
type Room struct {
47
47
log logger.Logger
48
+ roomLog logger.Logger // deferred logger
48
49
room * lksdk.Room
49
50
mix * mixer.Mixer
50
51
out * media.SwitchWriter
51
52
outDtmf atomic.Pointer [dtmf.Writer ]
52
53
p ParticipantInfo
53
- ready atomic. Bool
54
+ ready core. Fuse
54
55
subscribe atomic.Bool
55
56
subscribed core.Fuse
56
57
stopped core.Fuse
58
+ closed core.Fuse
57
59
}
58
60
59
61
type ParticipantConfig struct {
@@ -75,6 +77,21 @@ type RoomConfig struct {
75
77
func NewRoom (log logger.Logger ) * Room {
76
78
r := & Room {log : log , out : media .NewSwitchWriter (RoomSampleRate )}
77
79
r .mix = mixer .NewMixer (r .out , rtp .DefFrameDur )
80
+
81
+ roomLog , resolve := log .WithDeferredValues ()
82
+ r .roomLog = roomLog
83
+
84
+ go func () {
85
+ select {
86
+ case <- r .ready .Watch ():
87
+ resolve ("room" , r .room .Name (), "roomID" , r .room .SID ())
88
+ case <- r .stopped .Watch ():
89
+ resolve ()
90
+ case <- r .closed .Watch ():
91
+ resolve ()
92
+ }
93
+ }()
94
+
78
95
return r
79
96
}
80
97
@@ -100,7 +117,7 @@ func (r *Room) Room() *lksdk.Room {
100
117
}
101
118
102
119
func (r * Room ) participantJoin (rp * lksdk.RemoteParticipant ) {
103
- log := r .log .WithValues ("participant" , rp .Identity ())
120
+ log := r .roomLog .WithValues ("participant" , rp .Identity (), "pID" , rp . SID ())
104
121
log .Debugw ("participant joined" )
105
122
switch rp .Kind () {
106
123
case lksdk .ParticipantSIP :
@@ -112,8 +129,13 @@ func (r *Room) participantJoin(rp *lksdk.RemoteParticipant) {
112
129
}
113
130
}
114
131
132
+ func (r * Room ) participantLeft (rp * lksdk.RemoteParticipant ) {
133
+ log := r .roomLog .WithValues ("participant" , rp .Identity (), "pID" , rp .SID ())
134
+ log .Debugw ("participant left" )
135
+ }
136
+
115
137
func (r * Room ) subscribeTo (pub * lksdk.RemoteTrackPublication , rp * lksdk.RemoteParticipant ) {
116
- log := r .log .WithValues ("participant" , rp .Identity (), "track " , pub .SID (), "trackName" , pub .Name ())
138
+ log := r .roomLog .WithValues ("participant" , rp .Identity (), "pID" , rp . SID (), "trackID " , pub .SID (), "trackName" , pub .Name ())
117
139
if pub .Kind () != lksdk .TrackKindAudio {
118
140
log .Debugw ("skipping non-audio track" )
119
141
return
@@ -138,25 +160,28 @@ func (r *Room) Connect(conf *config.Config, rconf RoomConfig) error {
138
160
}
139
161
roomCallback := & lksdk.RoomCallback {
140
162
OnParticipantConnected : func (rp * lksdk.RemoteParticipant ) {
141
- log := r .log .WithValues ("participant" , rp .Identity ())
163
+ log := r .roomLog .WithValues ("participant" , rp .Identity (), "pID" , rp . SID ())
142
164
if ! r .subscribe .Load () {
143
165
log .Debugw ("skipping participant join event - subscribed flag not set" )
144
166
return // will subscribe later
145
167
}
146
168
r .participantJoin (rp )
147
169
},
170
+ OnParticipantDisconnected : func (rp * lksdk.RemoteParticipant ) {
171
+ r .participantLeft (rp )
172
+ },
148
173
ParticipantCallback : lksdk.ParticipantCallback {
149
174
OnTrackPublished : func (pub * lksdk.RemoteTrackPublication , rp * lksdk.RemoteParticipant ) {
150
- log := r .log .WithValues ("participant" , rp .Identity (), "track " , pub .SID (), "trackName" , pub .Name ())
175
+ log := r .roomLog .WithValues ("participant" , rp .Identity (), "pID" , rp . SID (), "trackID " , pub .SID (), "trackName" , pub .Name ())
151
176
if ! r .subscribe .Load () {
152
177
log .Debugw ("skipping track publish event - subscribed flag not set" )
153
178
return // will subscribe later
154
179
}
155
180
r .subscribeTo (pub , rp )
156
181
},
157
182
OnTrackSubscribed : func (track * webrtc.TrackRemote , pub * lksdk.RemoteTrackPublication , rp * lksdk.RemoteParticipant ) {
158
- log := r .log .WithValues ("participant" , rp .Identity (), "track " , track .ID (), "trackName" , pub .Name ())
159
- if ! r .ready .Load () {
183
+ log := r .roomLog .WithValues ("participant" , rp .Identity (), "pID" , rp . SID (), "trackID " , track .ID (), "trackName" , pub .Name ())
184
+ if ! r .ready .IsBroken () {
160
185
log .Warnw ("ignoring track, room not ready" , nil )
161
186
return
162
187
}
@@ -236,7 +261,7 @@ func (r *Room) Connect(conf *config.Config, rconf RoomConfig) error {
236
261
r .p .ID = r .room .LocalParticipant .SID ()
237
262
r .p .Identity = r .room .LocalParticipant .Identity ()
238
263
room .LocalParticipant .SetAttributes (partConf .Attributes )
239
- r .ready .Store ( true )
264
+ r .ready .Break ( )
240
265
r .subscribe .Store (false ) // already false, but keep for visibility
241
266
242
267
// Not subscribing to any tracks just yet!
@@ -316,7 +341,8 @@ func (r *Room) CloseWithReason(reason livekit.DisconnectReason) error {
316
341
if r == nil {
317
342
return nil
318
343
}
319
- r .ready .Store (false )
344
+
345
+ r .closed .Break ()
320
346
r .subscribe .Store (false )
321
347
err := r .CloseOutput ()
322
348
r .SetDTMFOutput (nil )
@@ -358,7 +384,7 @@ func (r *Room) NewParticipantTrack(sampleRate int) (media.WriteCloser[media.PCM1
358
384
}
359
385
360
386
func (r * Room ) SendData (data lksdk.DataPacket , opts ... lksdk.DataPublishOption ) error {
361
- if r == nil || ! r .ready .Load () {
387
+ if r == nil || ! r .ready .IsBroken () || r . closed . IsBroken () {
362
388
return nil
363
389
}
364
390
return r .room .LocalParticipant .PublishDataPacket (data , opts ... )
0 commit comments