comparison libervia/web/pages/calls/_browser/webrtc.py @ 1559:410064b31dca

browser (calls): add some logs useful for debugging
author Goffi <goffi@goffi.org>
date Wed, 16 Aug 2023 16:52:53 +0200
parents 83c2a6faa2ae
children 7dbb131bbb9e
comparison
equal deleted inserted replaced
1558:2f2250ddad23 1559:410064b31dca
16 16
17 class WebRTC: 17 class WebRTC:
18 def __init__(self): 18 def __init__(self):
19 self.reset_instance() 19 self.reset_instance()
20 bridge.register_signal("ice_candidates_new", self._on_ice_candidates_new) 20 bridge.register_signal("ice_candidates_new", self._on_ice_candidates_new)
21 bridge.register_signal("ice_restart", self._on_ice_restart)
21 self.is_audio_muted = None 22 self.is_audio_muted = None
22 self.is_video_muted = None 23 self.is_video_muted = None
23 self._is_sharing_screen = False 24 self._is_sharing_screen = False
24 self.screen_sharing_cb = None 25 self.screen_sharing_cb = None
25 self.local_video_elt = document["local_video"] 26 self.local_video_elt = document["local_video"]
184 self.media_candidates.setdefault(media_type, []).append(parsed_candidate) 185 self.media_candidates.setdefault(media_type, []).append(parsed_candidate)
185 log.debug(f"ICE candidate [{media_type}]: {event.candidate.candidate}") 186 log.debug(f"ICE candidate [{media_type}]: {event.candidate.candidate}")
186 else: 187 else:
187 log.debug("All ICE candidates gathered") 188 log.debug("All ICE candidates gathered")
188 189
190 def on_ice_connection_state_change(self, event):
191 """Log ICE connection change, mainly used for debugging"""
192 state = self._peer_connection.iceConnectionState
193 log.info(f"ICE Connection State changed to: {state}")
194
195 if state == "failed":
196 log.error("ICE connection failed. Check network connectivity and ICE configurations.")
197 elif state == "disconnected":
198 log.warning("ICE connection was disconnected.")
199
200 def on_ice_candidate_error(self, event):
201 """Log ICE error, useful for debugging"""
202 log.error(f"ICE Candidate Error: {event.errorText} (Code: {event.errorCode})")
203 log.debug(
204 f"URL: {event.url}, Host candidate: {event.hostCandidate}, Port: {event.port}"
205 )
206
189 def _set_media_types(self, offer): 207 def _set_media_types(self, offer):
190 """Sets media types from offer SDP 208 """Sets media types from offer SDP
191 209
192 @param offer: RTC session description containing the offer 210 @param offer: RTC session description containing the offer
193 """ 211 """
238 rtc_configuration = {"iceServers": ice_servers} 256 rtc_configuration = {"iceServers": ice_servers}
239 257
240 peer_connection = window.RTCPeerConnection.new(rtc_configuration) 258 peer_connection = window.RTCPeerConnection.new(rtc_configuration)
241 peer_connection.addEventListener("track", self.on_track) 259 peer_connection.addEventListener("track", self.on_track)
242 peer_connection.addEventListener("negotiationneeded", self.on_negotiation_needed) 260 peer_connection.addEventListener("negotiationneeded", self.on_negotiation_needed)
261 peer_connection.addEventListener(
262 "iceconnectionstatechange",
263 self.on_ice_connection_state_change
264 )
243 peer_connection.addEventListener("icecandidate", self.on_ice_candidate) 265 peer_connection.addEventListener("icecandidate", self.on_ice_candidate)
266 peer_connection.addEventListener("icecandidateerror", self.on_ice_candidate_error)
244 peer_connection.addEventListener( 267 peer_connection.addEventListener(
245 "icegatheringstatechange", self.on_ice_gathering_state_change 268 "icegatheringstatechange", self.on_ice_gathering_state_change
246 ) 269 )
247 270
248 self._peer_connection = peer_connection 271 self._peer_connection = peer_connection
398 if sid != self.sid: 421 if sid != self.sid:
399 log.debug(f"ignoring peer ice candidates for {sid=} ({self.sid=}).") 422 log.debug(f"ignoring peer ice candidates for {sid=} ({self.sid=}).")
400 return 423 return
401 candidates = json.loads(candidates_s) 424 candidates = json.loads(candidates_s)
402 aio.run(self.on_ice_candidates_new(candidates)) 425 aio.run(self.on_ice_candidates_new(candidates))
426
427 def _on_ice_restart(self, sid: str, side: str, profile: str):
428 if sid != self.sid:
429 log.debug(f"ignoring peer ice candidates for {sid=} ({self.sid=}).")
430 return
431 log.debug("ICE has been restarted")
403 432
404 async def on_ice_candidates_new(self, candidates: dict) -> None: 433 async def on_ice_candidates_new(self, candidates: dict) -> None:
405 """Called when new ICE canidates are received from peer 434 """Called when new ICE canidates are received from peer
406 435
407 @param candidates: Dictionary containing new ICE candidates 436 @param candidates: Dictionary containing new ICE candidates