fix pipeline heap corruption and chunk ignores

custom
jacqueline 2 years ago
parent 9eecf78e08
commit cabfd4b75e
  1. 2
      src/audio/audio_decoder.cpp
  2. 3
      src/audio/audio_task.cpp
  3. 17
      src/audio/chunk.cpp
  4. 1
      src/audio/i2s_audio_output.cpp
  5. 7
      src/audio/include/chunk.hpp

@ -40,7 +40,7 @@ auto AudioDecoder::ProcessStreamInfo(const StreamInfo& info)
stream_info_ = info; stream_info_ = info;
if (info.chunk_size) { if (info.chunk_size) {
chunk_reader_ = ChunkReader(info.chunk_size.value()); chunk_reader_.emplace(info.chunk_size.value());
} else { } else {
ESP_LOGE(kTag, "no chunk size given"); ESP_LOGE(kTag, "no chunk size given");
return cpp::fail(UNSUPPORTED_STREAM); return cpp::fail(UNSUPPORTED_STREAM);

@ -151,6 +151,9 @@ void AudioTaskMain(void* args) {
ESP_LOGE(kTag, "failed to process chunk"); ESP_LOGE(kTag, "failed to process chunk");
continue; continue;
} }
// TODO: think about whether to do the whole queue
break;
} }
} }
} }

@ -14,10 +14,14 @@
namespace audio { namespace audio {
static const std::size_t kWorkingBufferMultiple = 2;
ChunkReader::ChunkReader(std::size_t chunk_size) ChunkReader::ChunkReader(std::size_t chunk_size)
: raw_working_buffer_(static_cast<std::byte*>( : raw_working_buffer_(static_cast<std::byte*>(
heap_caps_malloc(chunk_size * 2, MALLOC_CAP_SPIRAM))), heap_caps_malloc(chunk_size * kWorkingBufferMultiple,
working_buffer_(raw_working_buffer_, chunk_size * 1.5) {} MALLOC_CAP_SPIRAM))),
working_buffer_(raw_working_buffer_,
chunk_size * kWorkingBufferMultiple) {}
ChunkReader::~ChunkReader() { ChunkReader::~ChunkReader() {
free(raw_working_buffer_); free(raw_working_buffer_);
@ -29,8 +33,8 @@ auto ChunkReader::HandleNewData(cpp::span<std::byte> data)
// Copy the new data onto the front for anything that was left over from the // Copy the new data onto the front for anything that was left over from the
// last portion. Note: this could be optimised for the '0 leftover bytes' // last portion. Note: this could be optimised for the '0 leftover bytes'
// case, which technically shouldn't need a copy. // case, which technically shouldn't need a copy.
cpp::span<std::byte> new_data_dest = working_buffer_.subspan(leftover_bytes_); std::copy(data.begin(), data.end(),
std::copy(data.begin(), data.end(), new_data_dest.begin()); working_buffer_.begin() + leftover_bytes_);
last_data_in_working_buffer_ = last_data_in_working_buffer_ =
working_buffer_.first(leftover_bytes_ + data.size()); working_buffer_.first(leftover_bytes_ + data.size());
leftover_bytes_ = 0; leftover_bytes_ = 0;
@ -39,6 +43,11 @@ auto ChunkReader::HandleNewData(cpp::span<std::byte> data)
auto ChunkReader::HandleLeftovers(std::size_t bytes_used) -> void { auto ChunkReader::HandleLeftovers(std::size_t bytes_used) -> void {
leftover_bytes_ = last_data_in_working_buffer_.size() - bytes_used; leftover_bytes_ = last_data_in_working_buffer_.size() - bytes_used;
// Ensure that we don't have more than a chunk of leftever bytes. This is
// bad, because we probably won't have enough data to store the next chunk.
assert(leftover_bytes_ <= working_buffer_.size() / kWorkingBufferMultiple);
if (leftover_bytes_ > 0) { if (leftover_bytes_ > 0) {
auto data_to_keep = last_data_in_working_buffer_.last(leftover_bytes_); auto data_to_keep = last_data_in_working_buffer_.last(leftover_bytes_);
std::copy(data_to_keep.begin(), data_to_keep.end(), std::copy(data_to_keep.begin(), data_to_keep.end(),

@ -92,6 +92,7 @@ auto I2SAudioOutput::ProcessStreamInfo(const StreamInfo& info)
auto I2SAudioOutput::ProcessChunk(const cpp::span<std::byte>& chunk) auto I2SAudioOutput::ProcessChunk(const cpp::span<std::byte>& chunk)
-> cpp::result<std::size_t, AudioProcessingError> { -> cpp::result<std::size_t, AudioProcessingError> {
ESP_LOGI(kTag, "playing samples");
SetSoftMute(false); SetSoftMute(false);
// TODO(jacqueline): write smaller parts with a small delay so that we can // TODO(jacqueline): write smaller parts with a small delay so that we can
// be responsive to pause and seek commands. // be responsive to pause and seek commands.

@ -18,6 +18,10 @@
namespace audio { namespace audio {
/**
* Utility for handling an input stream of chunk data, which simplifies needing
* access to blocks of data spanning two chunks.
*/
class ChunkReader { class ChunkReader {
public: public:
explicit ChunkReader(std::size_t chunk_size); explicit ChunkReader(std::size_t chunk_size);
@ -39,6 +43,9 @@ class ChunkReader {
*/ */
auto HandleNewData(cpp::span<std::byte> data) -> cpp::span<std::byte>; auto HandleNewData(cpp::span<std::byte> data) -> cpp::span<std::byte>;
ChunkReader(const ChunkReader&) = delete;
ChunkReader& operator=(const ChunkReader&) = delete;
private: private:
std::byte* raw_working_buffer_; std::byte* raw_working_buffer_;
cpp::span<std::byte> working_buffer_; cpp::span<std::byte> working_buffer_;

Loading…
Cancel
Save