Use Queue instead of list to communicate.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Wed, 9 Sep 2020 18:42:38 +0000 (20:42 +0200)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Wed, 9 Sep 2020 18:42:38 +0000 (20:42 +0200)
lagarde.py

index 1994893fc2f81c9aa26587fa2912eedf1459ca36..841f125516e2d27df70cce018351f918be8538a3 100755 (executable)
@@ -5,6 +5,7 @@ import asyncio
 import json
 import logging
 import ssl
+import queue
 from typing import Optional, List
 
 import gi
@@ -27,8 +28,8 @@ class Lagarde:
         self.sdp_offer: Optional[str] = None
         self.websocket: Optional[websockets.client.WebSocketClientProtocol] = None
         self.session_id = None
-        self.received_ice_candidates = []
-        self.generated_ice_candidates = []
+        self.received_ice_candidates = queue.Queue()
+        self.generated_ice_candidates = queue.Queue()
         self.user_fragments: Optional[List] = None
         self.mids: Optional[List] = None
         self.pipe = None
@@ -39,7 +40,7 @@ class Lagarde:
 
     def on_ice_candidate(self, element, mlineindex, candidate):
         log.debug('on_ice_candidate')
-        self.generated_ice_candidates.append((mlineindex, candidate))
+        self.generated_ice_candidates.put_nowait((mlineindex, candidate))
 
     def webrtcbin_pad_added(self, element, pad):
         log.debug('webrtcbin_pad_added')
@@ -148,13 +149,13 @@ class Lagarde:
                     gst_promise.get_reply()
                     await self.websocket.send(sdp_answer_msg)
 
-                elif len(self.received_ice_candidates) > 0:
-                    ic = self.received_ice_candidates.pop(0)
+                elif self.received_ice_candidates.qsize() > 0:
+                    ic = self.received_ice_candidates.get_nowait()
                     if ic['candidate'] != '':
                         self.webrtcbin.emit('add-ice-candidate', ic['sdpMLineIndex'], ic['candidate'])
 
-                elif len(self.generated_ice_candidates) > 0:
-                    mlineindex, candidate = self.generated_ice_candidates.pop(0)
+                elif self.generated_ice_candidates.qsize() > 0:
+                    mlineindex, candidate = self.generated_ice_candidates.get_nowait()
                     icemsg_value = json.dumps({
                         "candidate": candidate,
                         "sdpMid": self.mids[mlineindex],
@@ -191,7 +192,7 @@ class Lagarde:
             elif msg_type == 'addCallerIceCandidate':
                 value_json = json.loads(msg_value)
                 log.info(f'Got ICE candidate: {value_json}')
-                self.received_ice_candidates.append(value_json)
+                self.received_ice_candidates.put_nowait(value_json)
             elif msg_type == 'roomNotFound':
                 log.error(f'The room was not found: {uri}')
                 return