From 9aa8e4538af55399e07a86b42ed36feef01e1966 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 16 Jun 2023 11:47:37 +0200 Subject: [PATCH] Better termination logic for the ffmpeg audio converter. --- .../src/components/panels/Camera/Index.vue | 3 +-- .../src/components/panels/Sound/Index.vue | 3 +-- .../plugins/sound/_controllers/_recorder.py | 11 +++++------ platypush/plugins/sound/_converter.py | 18 ++++++++++-------- 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/platypush/backend/http/webapp/src/components/panels/Camera/Index.vue b/platypush/backend/http/webapp/src/components/panels/Camera/Index.vue index 3c93b9b70..1aec37fde 100644 --- a/platypush/backend/http/webapp/src/components/panels/Camera/Index.vue +++ b/platypush/backend/http/webapp/src/components/panels/Camera/Index.vue @@ -40,8 +40,7 @@
diff --git a/platypush/backend/http/webapp/src/components/panels/Sound/Index.vue b/platypush/backend/http/webapp/src/components/panels/Sound/Index.vue index 92c547ee1..db46eea85 100644 --- a/platypush/backend/http/webapp/src/components/panels/Sound/Index.vue +++ b/platypush/backend/http/webapp/src/components/panels/Sound/Index.vue @@ -2,8 +2,7 @@
diff --git a/platypush/plugins/sound/_controllers/_recorder.py b/platypush/plugins/sound/_controllers/_recorder.py index ef6adf26f..4c1393113 100644 --- a/platypush/plugins/sound/_controllers/_recorder.py +++ b/platypush/plugins/sound/_controllers/_recorder.py @@ -1,5 +1,3 @@ -from dataclasses import dataclass - from typing import IO from typing_extensions import override @@ -16,7 +14,6 @@ from .._model import AudioState from ._base import AudioThread -@dataclass class AudioRecorder(AudioThread): """ The ``AudioRecorder`` thread is responsible for recording audio from the @@ -32,7 +29,7 @@ class AudioRecorder(AudioThread): # _ = frames # __ = time def callback(indata, outdata, _, __, status): - if self.state == AudioState.PAUSED: + if self.state != AudioState.RUNNING: return if status: @@ -41,7 +38,7 @@ class AudioRecorder(AudioThread): try: audio_converter.write(indata.tobytes()) except AssertionError as e: - self.logger.warning('Audio recorder callback error: %s', e) + self.logger.warning('Audio converter callback error: %s', e) self.state = AudioState.STOPPED return @@ -63,8 +60,10 @@ class AudioRecorder(AudioThread): @override def notify_stop(self): + prev_state = self.state super().notify_stop() - get_bus().post(SoundRecordingStoppedEvent()) + if prev_state != AudioState.STOPPED: + get_bus().post(SoundRecordingStoppedEvent()) # vim:sw=4:ts=4:et: diff --git a/platypush/plugins/sound/_converter.py b/platypush/plugins/sound/_converter.py index 3aa307f2b..bdab2d03e 100644 --- a/platypush/plugins/sound/_converter.py +++ b/platypush/plugins/sound/_converter.py @@ -1,5 +1,6 @@ import asyncio from asyncio.subprocess import PIPE +from logging import getLogger from queue import Empty from queue import Queue @@ -74,6 +75,7 @@ class ConverterProcess(Thread): self._closed = False self._out_queue = Queue() self.ffmpeg = None + self.logger = getLogger(__name__) self._loop = None self._should_stop = Event() self._stop_lock = RLock() @@ -157,21 +159,21 @@ class ConverterProcess(Thread): def run(self): super().run() self._loop = get_or_create_event_loop() - self._loop.run_until_complete(self._audio_proxy(timeout=1)) + try: + self._loop.run_until_complete(self._audio_proxy(timeout=1)) + except RuntimeError as e: + self.logger.warning(e) + finally: + self.stop() def stop(self): with self._stop_lock: self._should_stop.set() if self.ffmpeg: - try: - self.ffmpeg.kill() - except ProcessLookupError: - pass + self.ffmpeg.kill() self.ffmpeg = None - - if self._loop: - self._loop = None + self._loop = None @property def should_stop(self) -> bool: