summaryrefslogtreecommitdiff
path: root/src/audio/include
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2022-12-03 11:10:06 +1100
committerjacqueline <me@jacqueline.id.au>2022-12-03 11:10:06 +1100
commit16d5d29049c08e21f57f7928ceedf40586a2d294 (patch)
treea4647f951f90b036c58c879ae186fa7e452ed950 /src/audio/include
parent00d4883d3a683eaf9cfaefacdd81434e0974ad13 (diff)
downloadtangara-fw-16d5d29049c08e21f57f7928ceedf40586a2d294.tar.gz
Use std::span (backported) and std::byte to make our buffers safer
Diffstat (limited to 'src/audio/include')
-rw-r--r--src/audio/include/audio_decoder.hpp9
-rw-r--r--src/audio/include/audio_element.hpp7
-rw-r--r--src/audio/include/chunk.hpp13
-rw-r--r--src/audio/include/fatfs_audio_input.hpp23
-rw-r--r--src/audio/include/stream_message.hpp25
5 files changed, 42 insertions, 35 deletions
diff --git a/src/audio/include/audio_decoder.hpp b/src/audio/include/audio_decoder.hpp
index 4d2fd5f3..a32442da 100644
--- a/src/audio/include/audio_decoder.hpp
+++ b/src/audio/include/audio_decoder.hpp
@@ -1,8 +1,10 @@
#pragma once
#include <cstddef>
+#include <cstdint>
#include "ff.h"
+#include "span.hpp"
#include "audio_element.hpp"
#include "codec.hpp"
@@ -23,8 +25,8 @@ class AudioDecoder : public IAudioElement {
auto ProcessStreamInfo(StreamInfo& info)
-> cpp::result<void, AudioProcessingError>;
- auto ProcessChunk(uint8_t* data, std::size_t length)
- -> cpp::result<size_t, AudioProcessingError>;
+ auto ProcessChunk(cpp::span<std::byte>& chunk)
+ -> cpp::result<std::size_t, AudioProcessingError>;
auto ProcessIdle() -> cpp::result<void, AudioProcessingError>;
AudioDecoder(const AudioDecoder&) = delete;
@@ -34,7 +36,8 @@ class AudioDecoder : public IAudioElement {
std::unique_ptr<codecs::ICodec> current_codec_;
std::optional<StreamInfo> stream_info_;
- uint8_t* chunk_buffer_;
+ std::byte* raw_chunk_buffer_;
+ cpp::span<std::byte> chunk_buffer_;
};
} // namespace audio
diff --git a/src/audio/include/audio_element.hpp b/src/audio/include/audio_element.hpp
index 5b697784..06e47b35 100644
--- a/src/audio/include/audio_element.hpp
+++ b/src/audio/include/audio_element.hpp
@@ -1,7 +1,5 @@
#pragma once
-#include <stdint.h>
-
#include <cstdint>
#include "freertos/FreeRTOS.h"
@@ -9,6 +7,7 @@
#include "freertos/message_buffer.h"
#include "freertos/portmacro.h"
#include "result.hpp"
+#include "span.hpp"
#include "stream_info.hpp"
#include "types.hpp"
@@ -77,8 +76,8 @@ class IAudioElement {
* bytes in this chunk that were actually used; leftover bytes will be
* prepended to the next call.
*/
- virtual auto ProcessChunk(uint8_t* data, std::size_t length)
- -> cpp::result<size_t, AudioProcessingError> = 0;
+ virtual auto ProcessChunk(cpp::span<std::byte>& chunk)
+ -> cpp::result<std::size_t, AudioProcessingError> = 0;
/*
* Called when there has been no data received over the input buffer for some
diff --git a/src/audio/include/chunk.hpp b/src/audio/include/chunk.hpp
index 365c83d0..0cbe8d5c 100644
--- a/src/audio/include/chunk.hpp
+++ b/src/audio/include/chunk.hpp
@@ -12,6 +12,7 @@
#include "freertos/portmacro.h"
#include "freertos/queue.h"
#include "result.hpp"
+#include "span.hpp"
namespace audio {
@@ -37,9 +38,8 @@ enum ChunkWriteResult {
* more input to read.
*/
auto WriteChunksToStream(MessageBufferHandle_t* stream,
- uint8_t* working_buffer,
- size_t working_buffer_length,
- std::function<size_t(uint8_t*, size_t)> callback,
+ cpp::span<std::byte> working_buffer,
+ std::function<size_t(cpp::span<std::byte>)> callback,
TickType_t max_wait) -> ChunkWriteResult;
enum ChunkReadResult {
@@ -64,7 +64,7 @@ class ChunkReader {
auto Reset() -> void;
- auto GetLastMessage() -> std::pair<uint8_t*, size_t>;
+ auto GetLastMessage() -> cpp::span<std::byte>;
/*
* Reads chunks of data from the given input stream, and invokes the given
@@ -79,12 +79,13 @@ class ChunkReader {
* will place the message at the start of the working_buffer and then return.
*/
auto ReadChunkFromStream(
- std::function<std::optional<size_t>(uint8_t*, size_t)> callback,
+ std::function<std::optional<size_t>(cpp::span<std::byte>)> callback,
TickType_t max_wait) -> ChunkReadResult;
private:
MessageBufferHandle_t* stream_;
- uint8_t* working_buffer_;
+ std::byte* raw_working_buffer_;
+ cpp::span<std::byte> working_buffer_;
std::size_t leftover_bytes_ = 0;
std::size_t last_message_size_ = 0;
diff --git a/src/audio/include/fatfs_audio_input.hpp b/src/audio/include/fatfs_audio_input.hpp
index c54b32bd..3ca79457 100644
--- a/src/audio/include/fatfs_audio_input.hpp
+++ b/src/audio/include/fatfs_audio_input.hpp
@@ -8,6 +8,7 @@
#include "freertos/message_buffer.h"
#include "freertos/queue.h"
+#include "span.hpp"
#include "audio_element.hpp"
#include "storage.hpp"
@@ -21,28 +22,28 @@ class FatfsAudioInput : public IAudioElement {
auto ProcessStreamInfo(StreamInfo& info)
-> cpp::result<void, AudioProcessingError>;
- auto ProcessChunk(uint8_t* data, std::size_t length)
- -> cpp::result<size_t, AudioProcessingError>;
+ auto ProcessChunk(cpp::span<std::byte>& chunk)
+ -> cpp::result<std::size_t, AudioProcessingError> = 0;
auto ProcessIdle() -> cpp::result<void, AudioProcessingError>;
- auto SendChunk(uint8_t* buffer, size_t size) -> size_t;
+ auto SendChunk(cpp::span<std::byte> dest) -> size_t;
private:
auto GetRingBufferDistance() -> size_t;
std::shared_ptr<drivers::SdStorage> storage_;
- uint8_t* file_buffer_;
- uint8_t* file_buffer_read_pos_;
- uint8_t* pending_read_pos_;
- uint8_t* file_buffer_write_pos_;
+ std::byte* raw_file_buffer_;
+ cpp::span<std::byte> file_buffer_;
+ cpp::span<std::byte>::iterator file_buffer_read_pos_;
+ cpp::span<std::byte>::iterator pending_read_pos_;
+ cpp::span<std::byte>::iterator file_buffer_write_pos_;
- uint8_t* chunk_buffer_;
+ std::byte* raw_chunk_buffer_;
+ cpp::span<std::byte> chunk_buffer_;
FIL current_file_;
- bool is_file_open_ = false;
-
- MessageBufferHandle_t input_buffer_;
+ bool is_file_open_;
uint8_t* output_buffer_memory_;
StaticMessageBuffer_t output_buffer_metadata_;
diff --git a/src/audio/include/stream_message.hpp b/src/audio/include/stream_message.hpp
index cbd7c733..043f9dc3 100644
--- a/src/audio/include/stream_message.hpp
+++ b/src/audio/include/stream_message.hpp
@@ -1,12 +1,12 @@
#pragma once
-#include <stdint.h>
-
+#include <cstdint>
#include <functional>
#include <optional>
#include "cbor.h"
#include "result.hpp"
+#include "span.hpp"
namespace audio {
@@ -20,14 +20,13 @@ enum MessageType {
};
template <typename Writer>
-auto WriteMessage(MessageType type,
- Writer&& writer,
- uint8_t* buffer,
- size_t length) -> cpp::result<size_t, CborError> {
+auto WriteMessage(MessageType type, Writer&& writer, cpp::span<std::byte> data)
+ -> cpp::result<size_t, CborError> {
CborEncoder root;
CborEncoder container;
+ uint8_t* cast_data = reinterpret_cast<uint8_t*>(data.data());
- cbor_encoder_init(&root, buffer, length, kEncoderFlags);
+ cbor_encoder_init(&root, cast_data, data.size(), kEncoderFlags);
cbor_encoder_create_array(&root, &container, 2);
cbor_encode_uint(&container, type);
@@ -37,17 +36,18 @@ auto WriteMessage(MessageType type,
}
cbor_encoder_close_container(&root, &container);
- return cbor_encoder_get_buffer_size(&root, buffer);
+ return cbor_encoder_get_buffer_size(&root, cast_data);
}
template <typename Result, typename Reader>
-auto ReadMessage(Reader&& reader, uint8_t* buffer, size_t length)
+auto ReadMessage(Reader&& reader, cpp::span<std::byte> data)
-> cpp::result<Result, CborError> {
CborParser parser;
CborValue root;
CborValue container;
- cbor_parser_init(buffer, length, kDecoderFlags, &parser, &root);
+ cbor_parser_init(reinterpret_cast<uint8_t*>(data.data()), data.size(),
+ kDecoderFlags, &parser, &root);
cbor_value_enter_container(&root, &container);
// Skip the type header
cbor_value_advance_fixed(&container);
@@ -55,6 +55,9 @@ auto ReadMessage(Reader&& reader, uint8_t* buffer, size_t length)
return std::invoke(reader, container);
}
-auto ReadMessageType(uint8_t* buffer, size_t length) -> MessageType;
+auto WriteTypeOnlyMessage(MessageType type, cpp::span<std::byte> data)
+ -> cpp::result<size_t, CborError>;
+auto ReadMessageType(cpp::span<std::byte> msg) -> MessageType;
+auto GetAdditionalData(cpp::span<std::byte> msg) -> cpp::span<std::byte>;
} // namespace audio