@@ -145,42 +145,42 @@ func (c *Conn) init() error {
145
145
146
146
c .rtc .OnNegotiationNeeded (c .negotiate )
147
147
c .rtc .OnICEConnectionStateChange (func (iceConnectionState webrtc.ICEConnectionState ) {
148
- if c .isClosed () {
148
+ c .closedICEMutex .Lock ()
149
+ defer c .closedICEMutex .Unlock ()
150
+ select {
151
+ case <- c .closedICE :
152
+ // Don't log more state changes if we've already closed.
149
153
return
154
+ default :
155
+ if iceConnectionState == webrtc .ICEConnectionStateClosed {
156
+ // pion/webrtc can update this state multiple times.
157
+ // A connection can never become un-closed, so we
158
+ // close the channel if it isn't already.
159
+ close (c .closedICE )
160
+ }
150
161
}
151
162
152
163
c .opts .Logger .Debug (context .Background (), "ice connection state updated" ,
153
164
slog .F ("state" , iceConnectionState ))
154
-
155
- if iceConnectionState == webrtc .ICEConnectionStateClosed {
156
- // pion/webrtc can update this state multiple times.
157
- // A connection can never become un-closed, so we
158
- // close the channel if it isn't already.
159
- c .closedICEMutex .Lock ()
160
- defer c .closedICEMutex .Unlock ()
161
- select {
162
- case <- c .closedICE :
163
- default :
164
- close (c .closedICE )
165
- }
166
- }
167
165
})
168
166
c .rtc .OnICEGatheringStateChange (func (iceGatherState webrtc.ICEGathererState ) {
169
- c .opts .Logger .Debug (context .Background (), "ice gathering state updated" ,
170
- slog .F ("state" , iceGatherState ))
171
-
172
- if iceGatherState == webrtc .ICEGathererStateClosed {
173
- // pion/webrtc can update this state multiple times.
174
- // A connection can never become un-closed, so we
175
- // close the channel if it isn't already.
176
- c .closedICEMutex .Lock ()
177
- defer c .closedICEMutex .Unlock ()
178
- select {
179
- case <- c .closedICE :
180
- default :
167
+ c .closedICEMutex .Lock ()
168
+ defer c .closedICEMutex .Unlock ()
169
+ select {
170
+ case <- c .closedICE :
171
+ // Don't log more state changes if we've already closed.
172
+ return
173
+ default :
174
+ if iceGatherState == webrtc .ICEGathererStateClosed {
175
+ // pion/webrtc can update this state multiple times.
176
+ // A connection can never become un-closed, so we
177
+ // close the channel if it isn't already.
181
178
close (c .closedICE )
182
179
}
183
180
}
181
+
182
+ c .opts .Logger .Debug (context .Background (), "ice gathering state updated" ,
183
+ slog .F ("state" , iceGatherState ))
184
184
})
185
185
c .rtc .OnConnectionStateChange (func (peerConnectionState webrtc.PeerConnectionState ) {
186
186
if c .isClosed () {
0 commit comments