changeset 1561:7dbb131bbb9e

browser (calls): update status on various events (connection established, connection lost, etc.)
author Goffi <goffi@goffi.org>
date Thu, 17 Aug 2023 14:58:21 +0200
parents 84f312be53b4
children 4afafce0c4c9
files libervia/web/pages/calls/_browser/__init__.py libervia/web/pages/calls/_browser/webrtc.py
diffstat 2 files changed, 25 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/web/pages/calls/_browser/__init__.py	Wed Aug 16 18:34:07 2023 +0200
+++ b/libervia/web/pages/calls/_browser/__init__.py	Thu Aug 17 14:58:21 2023 +0200
@@ -14,7 +14,7 @@
 profile = window.profile or ""
 bridge = Bridge()
 GATHER_TIMEOUT = 10000
-ALLOWED_STATUSES = (None, "dialing", "ringing")
+ALLOWED_STATUSES = (None, "dialing", "ringing", "in-call", "on-hold", "connecting", "connection-lost", "reconnecting")
 AUDIO = "audio"
 VIDEO = "video"
 ALLOWED_CALL_MODES = {AUDIO, VIDEO}
@@ -27,6 +27,9 @@
     def __init__(self):
         self.webrtc = WebRTC()
         self.webrtc.screen_sharing_cb = self.on_sharing_screen
+        self.webrtc.on_connection_established_cb = self.on_connection_established
+        self.webrtc.on_reconnect_cb = self.on_reconnect
+        self.webrtc.on_connection_lost_cb = self.on_connection_lost
         self.mode = "search"
         self._status = None
         self._callee = None
@@ -203,6 +206,7 @@
             log.debug(f"Call SID: {sid}")
 
             # Answer the call
+            self.status = "connecting"
             self.switch_mode("call")
         else:
             log.info(f"your are declining the call from {peer_jid}")
@@ -275,6 +279,15 @@
             )
             return
 
+    def on_connection_established(self):
+        self.status = "in-call"
+
+    def on_reconnect(self):
+        self.status = "reconnecting"
+
+    def on_connection_lost(self):
+        self.status = "connection-lost"
+
     async def make_call(self, audio: bool = True, video: bool = True) -> None:
         """Start a WebRTC call
 
--- a/libervia/web/pages/calls/_browser/webrtc.py	Wed Aug 16 18:34:07 2023 +0200
+++ b/libervia/web/pages/calls/_browser/webrtc.py	Thu Aug 17 14:58:21 2023 +0200
@@ -19,6 +19,9 @@
         self.reset_instance()
         bridge.register_signal("ice_candidates_new", self._on_ice_candidates_new)
         bridge.register_signal("ice_restart", self._on_ice_restart)
+        self.on_connection_established_cb = None
+        self.on_reconnect_cb = None
+        self.on_connection_lost_cb = None
         self.is_audio_muted = None
         self.is_video_muted = None
         self._is_sharing_screen = False
@@ -192,10 +195,15 @@
         state = self._peer_connection.iceConnectionState
         log.info(f"ICE Connection State changed to: {state}")
 
-        if state == "failed":
+        if state == "connected":
+            if self.on_connection_established_cb is not None:
+                self.on_connection_established_cb()
+        elif state == "failed":
             log.error("ICE connection failed. Check network connectivity and ICE configurations.")
         elif state == "disconnected":
             log.warning("ICE connection was disconnected.")
+            if self.on_connection_lost_cb is not None:
+                self.on_connection_lost_cb()
 
     def on_ice_candidate_error(self, event):
         """Log ICE error, useful for debugging"""
@@ -429,6 +437,8 @@
             log.debug(f"ignoring peer ice candidates for {sid=} ({self.sid=}).")
             return
         log.debug("ICE has been restarted")
+        if self.on_reconnect_cb is not None:
+            self.on_reconnect_cb()
 
     async def on_ice_candidates_new(self, candidates: dict) -> None:
         """Called when new ICE canidates are received from peer