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/include/chunk.hpp | |
| parent | 01be69eca1fa89c047fc29f5cb0ea8ba0898dad1 (diff) | |
| download | tangara-fw-2056cad0ab7b805f0ed5629b100b50f8ea9e127e.tar.gz | |
WIP
Diffstat (limited to 'src/audio/include/chunk.hpp')
| -rw-r--r-- | src/audio/include/chunk.hpp | 40 |
1 files changed, 28 insertions, 12 deletions
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 @@ -19,6 +19,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. CHUNK_ENCODING_ERROR, @@ -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<size_t(cpp::span<std::byte>)> callback, - TickType_t max_wait) -> ChunkWriteResult; +class ChunkWriter { + public: + explicit ChunkWriter(StreamBuffer* buffer); + ~ChunkWriter(); + + auto Reset() -> void; + + auto GetLastMessage() -> cpp::span<std::byte>; + + /* + * 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<size_t(cpp::span<std::byte>)> callback, + TickType_t max_wait) -> ChunkWriteResult; + + private: + StreamBuffer* stream_; + std::size_t leftover_bytes_ = 0; +}; enum ChunkReadResult { CHUNK_READ_OKAY, |
