From 2056cad0ab7b805f0ed5629b100b50f8ea9e127e Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 12 Jan 2023 14:28:52 +1100 Subject: WIP --- src/audio/include/chunk.hpp | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) (limited to 'src/audio/include/chunk.hpp') diff --git a/src/audio/include/chunk.hpp b/src/audio/include/chunk.hpp index d55e5d9d..5c7e73dd 100644 --- a/src/audio/include/chunk.hpp +++ b/src/audio/include/chunk.hpp @@ -18,6 +18,8 @@ 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. @@ -27,18 +29,32 @@ enum ChunkWriteResult { CHUNK_WRITE_TIMEOUT, }; -/* - * 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 WriteChunksToStream(StreamBuffer* stream, - std::function)> callback, - TickType_t max_wait) -> ChunkWriteResult; +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, -- cgit v1.2.3