Exit when room closed.
[toast/stream2beamer.git] / lagarde.py
index 140ccd8d64d6e8ae8ab7c75bfa40afaebf52b78c..487b88c43816a15a3744d4d218780c4ac64f7149 100755 (executable)
@@ -102,12 +102,13 @@ class Lagarde:
         try:
             while True:
                 if bus.have_pending():
-                    msg = bus.pop()  # Gst.Message, has to be unref'ed.
-                    if msg.type != Gst.MessageType.STATE_CHANGED:
-                        # log.info(f'Receive Gst.Message: {msg.type}, {msg.seqnum}, {msg.get_structure()}')
-                        # log.info(f'{webrtcbin.props.signaling_state} {webrtcbin.props.ice_gathering_state} {webrtcbin.props.ice_connection_state}')
-                        # Gst.Message.unref(msg)
-                        pass
+                    msg = bus.pop()
+                    if msg.type == Gst.MessageType.ERROR:
+                        log.error(f'Error from gstreamer message bus: {msg.get_structure()}')
+                        return
+                    elif msg.type == Gst.MessageType.EOS:  # end of stream
+                        log.info(f'Gstreamer message bus reports end of stream')
+                        return
                 elif self.sdp_offer is not None:
                     res, sm = GstSdp.SDPMessage.new()
                     assert res == GstSdp.SDPResult.OK
@@ -182,22 +183,24 @@ class Lagarde:
                 msg_json = json.loads(msg)
                 msg_type = msg_json['Type']
                 msg_value = msg_json['Value']
-                self.session_id = msg_json['SessionID']
                 log.info(f"receive for session {self.session_id} type {msg_type}")
                 if msg_type == 'newSession':
-                    pass
+                    self.session_id = msg_json['SessionID']
                 elif msg_type == 'gotOffer':
+                    assert self.session_id == msg_json['SessionID']
                     value_json = json.loads(msg_value)
                     sdp = value_json['sdp']
                     log.info(f'SDP: {sdp}')
                     self.sdp_offer = sdp
                 elif msg_type == 'addCallerIceCandidate':
+                    assert self.session_id == msg_json['SessionID']
                     value_json = json.loads(msg_value)
                     log.info(f'ICE: {value_json}')
                     self.received_ice_candidates.append(value_json)
                 elif msg_type == 'roomClosed':
                     log.info('Oh noes, the room went away!')
-                    # and here we should clean up
+                    self.session_id = None
+                    return
                 else:
                     log.error(f'Unknown message type {msg_type}')