From ac42f7eba434f27bce361fd550b03457414ec1f6 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 25 Sep 2020 18:06:27 +0200 Subject: [PATCH] Make sure that the PiCamera session is properly closed and re-opened on each stream request --- platypush/plugins/camera/__init__.py | 9 ++++----- platypush/plugins/camera/model/exceptions.py | 2 +- platypush/plugins/camera/pi/__init__.py | 9 ++++++--- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/platypush/plugins/camera/__init__.py b/platypush/plugins/camera/__init__.py index 9c48924d..761d550c 100644 --- a/platypush/plugins/camera/__init__.py +++ b/platypush/plugins/camera/__init__.py @@ -17,13 +17,13 @@ from platypush.message.event.camera import CameraRecordingStartedEvent, CameraPi CameraRecordingStoppedEvent, CameraVideoRenderedEvent from platypush.plugins import Plugin, action from platypush.plugins.camera.model.camera import CameraInfo, Camera -from platypush.plugins.camera.model.exceptions import CameraException, CaptureSessionAlreadyRunningException +from platypush.plugins.camera.model.exceptions import CameraException, CaptureAlreadyRunningException from platypush.plugins.camera.model.writer import VideoWriter, StreamWriter from platypush.plugins.camera.model.writer.ffmpeg import FFmpegFileWriter from platypush.plugins.camera.model.writer.preview import PreviewWriter, PreviewWriterFactory from platypush.utils import get_plugin_name_by_class -__all__ = ['Camera', 'CameraInfo', 'CameraException', 'CameraPlugin', 'CaptureSessionAlreadyRunningException', +__all__ = ['Camera', 'CameraInfo', 'CameraException', 'CameraPlugin', 'CaptureAlreadyRunningException', 'StreamWriter'] @@ -139,11 +139,10 @@ class CameraPlugin(Plugin, ABC): :return: The initialized camera device. :raises: :class:`platypush.plugins.camera.CaptureSessionAlreadyRunningException` """ + info = self._merge_info(**params) if device is None: - info = self.camera_info.clone() device = info.device elif device not in self._devices: - info = self._merge_info(**params) info.device = device else: info = self._devices[device].info.clone() @@ -152,7 +151,7 @@ class CameraPlugin(Plugin, ABC): if device in self._devices: camera = self._devices[device] if camera.capture_thread and camera.capture_thread.is_alive() and camera.start_event.is_set(): - raise CaptureSessionAlreadyRunningException(device) + raise CaptureAlreadyRunningException(device) camera.start_event.clear() camera.capture_thread = None diff --git a/platypush/plugins/camera/model/exceptions.py b/platypush/plugins/camera/model/exceptions.py index 2045d3be..0b84f3f7 100644 --- a/platypush/plugins/camera/model/exceptions.py +++ b/platypush/plugins/camera/model/exceptions.py @@ -2,7 +2,7 @@ class CameraException(RuntimeError): pass -class CaptureSessionAlreadyRunningException(CameraException): +class CaptureAlreadyRunningException(CameraException): def __init__(self, device): super().__init__('A capturing session on the device {} is already running'.format(device)) diff --git a/platypush/plugins/camera/pi/__init__.py b/platypush/plugins/camera/pi/__init__.py index 1550c448..232f8805 100644 --- a/platypush/plugins/camera/pi/__init__.py +++ b/platypush/plugins/camera/pi/__init__.py @@ -147,10 +147,13 @@ class CameraPiPlugin(CameraPlugin): if not sock: continue + picam = self.open_device(**camera.info.to_dict()) if camera.object is None or camera.object.closed \ + else camera.object + try: - camera.object.start_recording(sock, format=stream_format) + picam.start_recording(sock, format=stream_format) while camera.stream_event.is_set(): - camera.object.wait_recording(1) + picam.wait_recording(1) except ConnectionError: self.logger.info('Client closed connection') finally: @@ -160,7 +163,7 @@ class CameraPiPlugin(CameraPlugin): except Exception as e: self.logger.warning('Error while closing client socket: {}'.format(str(e))) - self.release_device(camera) + self.close_device(camera) finally: self._cleanup_stream(camera, server_socket, sock) self.logger.info('Stopped camera stream')