diff options
| author | jacqueline <me@jacqueline.id.au> | 2023-01-12 14:28:52 +1100 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2023-01-12 14:28:52 +1100 |
| commit | 2056cad0ab7b805f0ed5629b100b50f8ea9e127e (patch) | |
| tree | 1e8385d48e18551240e9ef9683b8696292f8d760 /src/audio/audio_decoder.cpp | |
| parent | 01be69eca1fa89c047fc29f5cb0ea8ba0898dad1 (diff) | |
| download | tangara-fw-2056cad0ab7b805f0ed5629b100b50f8ea9e127e.tar.gz | |
WIP
Diffstat (limited to 'src/audio/audio_decoder.cpp')
| -rw-r--r-- | src/audio/audio_decoder.cpp | 62 |
1 files changed, 36 insertions, 26 deletions
diff --git a/src/audio/audio_decoder.cpp b/src/audio/audio_decoder.cpp index 872b7ead..88ddc323 100644 --- a/src/audio/audio_decoder.cpp +++ b/src/audio/audio_decoder.cpp @@ -57,35 +57,45 @@ auto AudioDecoder::ProcessChunk(const cpp::span<std::byte>& chunk) bool has_samples_to_send = false; bool needs_more_input = false; std::optional<codecs::ICodec::ProcessingError> error = std::nullopt; - WriteChunksToStream( - output_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. - while (has_samples_to_send || !needs_more_input) { - if (!has_samples_to_send) { - auto result = current_codec_->ProcessNextFrame(); - has_samples_to_send = true; - if (result.has_error()) { - error = result.error(); - // End our output stream immediately if the codec barfed. - return 0; + while (1) { + ChunkWriteResult res = chunk_writer_.WriteChunkToStream( + [&](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. + while (has_samples_to_send || !needs_more_input) { + if (!has_samples_to_send) { + auto result = current_codec_->ProcessNextFrame(); + has_samples_to_send = true; + if (result.has_error()) { + error = result.error(); + // End our output stream immediately if the codec barfed. + return 0; + } else { + needs_more_input = result.value(); + } } else { - needs_more_input = result.value(); + auto result = current_codec_->WriteOutputSamples( + buffer.last(buffer.size() - bytes_written)); + bytes_written += result.first; + has_samples_to_send = !result.second; } - } else { - auto result = current_codec_->WriteOutputSamples( - buffer.last(buffer.size() - bytes_written)); - bytes_written += result.first; - has_samples_to_send = !result.second; } - } - return bytes_written; - }, - // This element doesn't support any kind of out of band commands, so we - // can just suspend the whole task if the output buffer fills up. - portMAX_DELAY); + return bytes_written; + }, + // TODO + portMAX_DELAY); + + switch (res) { + case CHUNK_WRITE_OKAY: + break; + case CHUNK_WRITE_TIMEOUT: + case CHUNK_OUT_OF_DATA: + return {}; + default: + return cpp::fail(IO_ERROR); + } + } if (error) { ESP_LOGE(kTag, "Codec encountered error %d", error.value()); |
