Skip to content

Commit

Permalink
Fix bug in the shutdown behaviour of the decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
joetoddsonos committed Mar 29, 2024
1 parent e17d036 commit cc12ef9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
18 changes: 10 additions & 8 deletions pyflac/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]" }]
Expand Down

0 comments on commit cc12ef9

Please sign in to comment.