diff options
| author | jacqueline <me@jacqueline.id.au> | 2022-12-03 11:10:06 +1100 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2022-12-03 11:10:06 +1100 |
| commit | 16d5d29049c08e21f57f7928ceedf40586a2d294 (patch) | |
| tree | a4647f951f90b036c58c879ae186fa7e452ed950 /src/audio/audio_decoder.cpp | |
| parent | 00d4883d3a683eaf9cfaefacdd81434e0974ad13 (diff) | |
| download | tangara-fw-16d5d29049c08e21f57f7928ceedf40586a2d294.tar.gz | |
Use std::span (backported) and std::byte to make our buffers safer
Diffstat (limited to 'src/audio/audio_decoder.cpp')
| -rw-r--r-- | src/audio/audio_decoder.cpp | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/src/audio/audio_decoder.cpp b/src/audio/audio_decoder.cpp index 48637f08..31cfb50e 100644 --- a/src/audio/audio_decoder.cpp +++ b/src/audio/audio_decoder.cpp @@ -22,13 +22,14 @@ namespace audio { AudioDecoder::AudioDecoder() : IAudioElement(), stream_info_({}), - chunk_buffer_(static_cast<uint8_t*>( - heap_caps_malloc(kMaxChunkSize, MALLOC_CAP_SPIRAM))) + raw_chunk_buffer_(static_cast<std::byte*>( + heap_caps_malloc(kMaxChunkSize, MALLOC_CAP_SPIRAM))), + chunk_buffer_(raw_chunk_buffer_, kMaxChunkSize) {} AudioDecoder::~AudioDecoder() { - free(chunk_buffer_); + free(raw_chunk_buffer_); } auto AudioDecoder::SetInputBuffer(MessageBufferHandle_t* buffer) -> void { @@ -61,21 +62,21 @@ auto AudioDecoder::ProcessStreamInfo(StreamInfo& info) return {}; } -auto AudioDecoder::ProcessChunk(uint8_t* data, std::size_t length) +auto AudioDecoder::ProcessChunk(cpp::span<std::byte>& chunk) -> cpp::result<size_t, AudioProcessingError> { if (current_codec_ == nullptr) { // Should never happen, but fail explicitly anyway. return cpp::fail(UNSUPPORTED_STREAM); } - current_codec_->SetInput(data, length); + current_codec_->SetInput(chunk); bool has_samples_to_send = false; bool needs_more_input = false; std::optional<codecs::ICodec::ProcessingError> error = std::nullopt; WriteChunksToStream( - output_buffer_, chunk_buffer_, kMaxChunkSize, - [&](uint8_t* buf, size_t len) -> std::size_t { + output_buffer_, chunk_buffer_, + [&](cpp::span<std::byte> buffer) -> std::size_t { std::size_t bytes_written = 0; // Continue filling up the output buffer so long as we have samples // leftover, or are able to synthesize more samples from the input. @@ -92,7 +93,7 @@ auto AudioDecoder::ProcessChunk(uint8_t* data, std::size_t length) } } else { auto result = current_codec_->WriteOutputSamples( - buf + bytes_written, len - bytes_written); + buffer.last(buffer.size() - bytes_written)); bytes_written += result.first; has_samples_to_send = !result.second; } |
