Add wrapper that listens to the availability of the room.
[toast/stream2beamer.git] / laplace_client.py
index 3600fdc3a30462cd4dbd1ab97ff0412a343d6953..7378c62d8037b083e80ce0c07a25a9ff93dd143d 100755 (executable)
@@ -31,6 +31,7 @@ class WebRTCClient:
         self.ssl_context.verify_mode = ssl.CERT_NONE
         self.websocket = None
         self.session_id = None
+        self.userfragments = []
 
     def send_sdp_offer(self, offer):
         text = offer.sdp.as_text()
@@ -57,6 +58,9 @@ class WebRTCClient:
         promise.interrupt()
         self.send_sdp_offer(offer)
 
+        sdp = offer.sdp
+        self.userfragments = [sdp.get_media(i).get_attribute_val('ice-ufrag') for i in range(sdp.medias_len())]
+
     def on_negotiation_needed(self, element):
         log.info('on_negotiation_needed')
         promise = Gst.Promise.new_with_change_func(self.on_offer_created, element, None)
@@ -68,8 +72,9 @@ class WebRTCClient:
             'Type': 'addCalleeIceCandidate',
             'Value': json.dumps({
                 "candidate": candidate,
-                "sdpMid": "0",
+                "sdpMid": f"{mlineindex}",
                 "sdpMLineIndex": mlineindex,
+                "usernameFragment": self.userfragments[mlineindex],
                 })
             })
         log.info(f'send_ice_candidate_message with {icemsg}')
@@ -155,27 +160,31 @@ class WebRTCClient:
         self.webrtc.emit('add-ice-candidate', sdpmlineindex, candidate)
 
     async def run(self):
-        async with websockets.connect(self.uri, ssl=self.ssl_context) as websocket:
-            self.websocket = websocket
-            self.start_pipeline()
-            async for msg in websocket:
-                msg_json = json.loads(msg)
-                msg_type = msg_json['Type']
-                msg_value = msg_json['Value']
-                session_id = msg_json['SessionID']
-                log.info(f"receive for session {session_id} type {msg_type}")
-                if msg_type == 'newSession':
-                    self.session_id = session_id
-                elif msg_type == 'gotOffer':
-                    value_json = json.loads(msg_value)
-                    sdp = value_json['sdp']
-                    self.handle_sdp(sdp)
-                elif msg_type == 'addCallerIceCandidate':
-                    value_json = json.loads(msg_value)
-                    self.handle_ice(value_json)
-        self.close_pipeline()
-        self.websocket = None
-        self.session_id = None
+        try:
+            async with websockets.connect(self.uri, ssl=self.ssl_context) as websocket:
+                self.websocket = websocket
+                self.start_pipeline()
+                async for msg in websocket:
+                    msg_json = json.loads(msg)
+                    msg_type = msg_json['Type']
+                    msg_value = msg_json['Value']
+                    session_id = msg_json['SessionID']
+                    log.info(f"receive for session {session_id} type {msg_type}")
+                    if msg_type == 'newSession':
+                        self.session_id = session_id
+                    elif msg_type == 'gotOffer':
+                        value_json = json.loads(msg_value)
+                        sdp = value_json['sdp']
+                        self.handle_sdp(sdp)
+                    elif msg_type == 'addCallerIceCandidate':
+                        value_json = json.loads(msg_value)
+                        self.handle_ice(value_json)
+            self.close_pipeline()
+            self.websocket = None
+            self.session_id = None
+        except:
+            log.error(f'Connection to "{self.uri}" failed')
+            sys.exit(1)
 
 
 def check_plugins():
@@ -193,7 +202,7 @@ def main():
     if not check_plugins():
         sys.exit(1)
     parser = argparse.ArgumentParser()
-    parser.add_argument('--uri', default='wss://localhost:2222/ws_connect?id=cug',
+    parser.add_argument('--uri', default='wss://localhost:1234/ws_connect?id=cug',
         help='Signalling server URI')
     args = parser.parse_args()
     c = WebRTCClient(args.uri)