From f6dcd845fc80da4e3043146e4362258dd8e0c0a1 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 26 Jan 2023 15:02:57 +1100 Subject: Switch from MessageBuffer to Queue for pipeline comms --- src/audio/include/chunk.hpp | 70 +++++---------------------------------------- 1 file changed, 7 insertions(+), 63 deletions(-) (limited to 'src/audio/include/chunk.hpp') diff --git a/src/audio/include/chunk.hpp b/src/audio/include/chunk.hpp index 5c7e73dd..6154ab25 100644 --- a/src/audio/include/chunk.hpp +++ b/src/audio/include/chunk.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -17,68 +18,12 @@ namespace audio { -enum ChunkWriteResult { - // Returned when the callback does not write any data. - CHUNK_WRITE_OKAY, - // Returned when the callback does not write any data. - CHUNK_OUT_OF_DATA, - // Returned when there is an error encoding a chunk header using cbor. - CHUNK_ENCODING_ERROR, - // Returned when max_wait expires without room in the stream buffer becoming - // available. - CHUNK_WRITE_TIMEOUT, -}; - -class ChunkWriter { - public: - explicit ChunkWriter(StreamBuffer* buffer); - ~ChunkWriter(); - - auto Reset() -> void; - - auto GetLastMessage() -> cpp::span; - - /* - * Invokes the given callback to receive data, breaks the received data up - * into chunks with headers, and writes those chunks to the given output - * stream. - * - * The callback will be invoked with a byte buffer and its size. The callback - * should write as much data as it can to this buffer, and then return the - * number of bytes it wrote. Return a value of 0 to indicate that there is no - * more input to read. - */ - auto WriteChunkToStream(std::function)> callback, - TickType_t max_wait) -> ChunkWriteResult; - - private: - StreamBuffer* stream_; - std::size_t leftover_bytes_ = 0; -}; - -enum ChunkReadResult { - CHUNK_READ_OKAY, - // Returned when the chunk was read successfully, but the consumer did not - // use all of the data. - CHUNK_LEFTOVER_DATA, - // Returned an error in parsing the cbor-encoded header. - CHUNK_DECODING_ERROR, - // Returned when max_wait expired before any data was read. - CHUNK_READ_TIMEOUT, - // Returned when a non-chunk message is received. - CHUNK_STREAM_ENDED, - // Returned when the processing callback does not return a value. - CHUNK_PROCESSING_ERROR, -}; - class ChunkReader { public: - explicit ChunkReader(StreamBuffer* buffer); + explicit ChunkReader(std::size_t chunk_size); ~ChunkReader(); - auto Reset() -> void; - - auto GetLastMessage() -> cpp::span; + auto HandleLeftovers(std::size_t bytes_used) -> void; /* * Reads chunks of data from the given input stream, and invokes the given @@ -92,14 +37,13 @@ class ChunkReader { * If this function encounters a message in the stream that is not a chunk, it * will place the message at the start of the working_buffer and then return. */ - auto ReadChunkFromStream( - std::function(cpp::span)> callback, - TickType_t max_wait) -> ChunkReadResult; + auto HandleNewData(cpp::span data) -> cpp::span; private: - StreamBuffer* stream_; + std::byte* raw_working_buffer_; + cpp::span working_buffer_; + cpp::span last_data_in_working_buffer_; std::size_t leftover_bytes_ = 0; - std::size_t last_message_size_ = 0; }; } // namespace audio -- cgit v1.2.3