FIX: Remove wait_for_either barrier from the streaming thread.

The `_accept_client` logic will be executed anyway at the next
iteration, unless the server socket is closed - in that case, the thread
will fail and exit anyway.

The `wait_for_either` barrier in the streaming thread may instead
prevent other connections from being established after the first one is
closed.
This commit is contained in:
Fabio Manganiello 2024-02-25 16:02:52 +01:00
parent 4c92bdd11c
commit 1c14450781

View file

@ -728,43 +728,45 @@ class CameraPlugin(RunnablePlugin, ABC):
srv_sock.settimeout(1) srv_sock.settimeout(1)
yield srv_sock yield srv_sock
def _accept_client(self, server_socket: socket.socket) -> Optional[IO]: def _accept_client(
self, server_socket: socket.socket
) -> Tuple[Optional[socket.socket], Optional[IO]]:
try: try:
sock = server_socket.accept()[0] sock = server_socket.accept()[0]
self.logger.info('Accepted client connection from %s', sock.getpeername()) self.logger.info('Accepted client connection from %s', sock.getpeername())
return sock.makefile('wb') return sock, sock.makefile('wb')
except socket.timeout: except socket.timeout:
return None return None, None
def streaming_thread( def streaming_thread(
self, camera: Camera, stream_format: str, duration: Optional[float] = None self, camera: Camera, stream_format: str, duration: Optional[float] = None
): ):
with self._prepare_server_socket(camera) as srv_sock: with self._prepare_server_socket(camera) as srv_sock:
streaming_started_time = time.time() streaming_started_time = time.time()
sock = None sock, fp = None, None
self.logger.info('Starting streaming on port %s', camera.info.listen_port) self.logger.info('Starting streaming on port %s', camera.info.listen_port)
try: try:
while camera.stream_event.is_set(): while (
camera.stream_event.is_set()
and not camera.stop_stream_event.is_set()
and not self.should_stop()
):
if duration and time.time() - streaming_started_time >= duration: if duration and time.time() - streaming_started_time >= duration:
break break
sock = self._accept_client(srv_sock) sock, fp = self._accept_client(srv_sock)
if not sock: if not (sock and fp):
continue continue
if duration and time.time() - streaming_started_time >= duration: if duration and time.time() - streaming_started_time >= duration:
break break
self._streaming_loop( self._streaming_loop(
camera, stream_format, sock=sock, duration=duration camera, stream_format, sock=fp, duration=duration
)
wait_for_either(
self._should_stop, camera.stop_stream_event, timeout=duration
) )
finally: finally:
self._cleanup_stream(camera, srv_sock, sock) self._cleanup_stream(camera, srv_sock, fp)
self.logger.info('Stopped camera stream') self.logger.info('Stopped camera stream')
@ -1034,9 +1036,9 @@ class CameraPlugin(RunnablePlugin, ABC):
break break
camera = cameras[0] camera = cameras[0]
wait_for_either(self._should_stop, camera.stop_stream_event)
try: try:
wait_for_either(self._should_stop, camera.stop_stream_event)
self.stop_streaming() self.stop_streaming()
except Exception as e: except Exception as e:
self.logger.warning('Error while stopping the camera stream: %s', e) self.logger.warning('Error while stopping the camera stream: %s', e)