From cc12ef911458565b62bf72eda05512de3a0d6016 Mon Sep 17 00:00:00 2001 From: Joe Todd Date: Fri, 29 Mar 2024 14:47:58 +0000 Subject: [PATCH] Fix bug in the shutdown behaviour of the decoder --- CHANGELOG.rst | 4 ++++ pyflac/decoder.py | 18 ++++++++++-------- pyproject.toml | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index eca7004..f9b7cf5 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,10 @@ pyFLAC Changelog ---------------- +**v2.3.0** + +* Fixed bug in the shutdown behaviour of the decoder (see #22 and #23). + **v2.2.0** * Updated FLAC library to v1.4.3. diff --git a/pyflac/decoder.py b/pyflac/decoder.py index d848981..0f865ec 100644 --- a/pyflac/decoder.py +++ b/pyflac/decoder.py @@ -225,8 +225,8 @@ def finish(self): # Instruct the decoder to finish up and wait until it is done # -------------------------------------------------------------- self._done = True - super().finish() self._thread.join(timeout=3) + super().finish() if self._error: raise DecoderProcessException(self._error) @@ -314,6 +314,14 @@ def _read_callback(_decoder, If an exception is raised here, the abort status is returned. """ decoder = _ffi.from_handle(client_data) + + while len(decoder._buffer) == 0 and not (decoder._error or decoder._done): + # ---------------------------------------------------------- + # Wait until there is something in the buffer, or an error + # occurs, or the end of the stream is reached. + # ---------------------------------------------------------- + time.sleep(0.01) + if decoder._error: # ---------------------------------------------------------- # If an error has been issued via the error callback, then @@ -329,18 +337,12 @@ def _read_callback(_decoder, num_bytes[0] = 0 return _lib.FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM - maximum_bytes = int(num_bytes[0]) - while len(decoder._buffer) == 0: - # ---------------------------------------------------------- - # Wait until there is something in the buffer - # ---------------------------------------------------------- - time.sleep(0.01) - # -------------------------------------------------------------- # Ensure only the maximum bytes or less is taken from # the thread safe queue. # -------------------------------------------------------------- data = bytes() + maximum_bytes = int(num_bytes[0]) if len(decoder._buffer[0]) <= maximum_bytes: data = decoder._buffer.popleft() maximum_bytes -= len(data) diff --git a/pyproject.toml b/pyproject.toml index ae42b9e..afac4d2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta" [project] name = "pyFLAC" -version = "2.2.0" +version = "2.3.0" description = "A Python wrapper for libFLAC" readme = "README.rst" authors = [{ name = "Joe Todd", email = "joe.todd@sonos.com" }]