diff options
| author | jacqueline <me@jacqueline.id.au> | 2022-11-23 17:15:06 +1100 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2022-11-23 17:15:06 +1100 |
| commit | a7df2855889055976956a58d2a36f23626371ee9 (patch) | |
| tree | 16e180e57f84474acaeb1893208cc07e278af6f4 /src/audio/stream_info.cpp | |
| parent | dfa9ab6e04689b99267092e016a91d9254f94cd8 (diff) | |
| download | tangara-fw-a7df2855889055976956a58d2a36f23626371ee9.tar.gz | |
Mostly done pipeline arch. Now onto cleanup and building.
Diffstat (limited to 'src/audio/stream_info.cpp')
| -rw-r--r-- | src/audio/stream_info.cpp | 80 |
1 files changed, 23 insertions, 57 deletions
diff --git a/src/audio/stream_info.cpp b/src/audio/stream_info.cpp index bb9b1fa2..6011571d 100644 --- a/src/audio/stream_info.cpp +++ b/src/audio/stream_info.cpp @@ -1,45 +1,26 @@ #include "stream_info.hpp" -#include "stream_message.hpp" #include <cstdint> +#include "cbor_decoder.hpp" #include "esp-idf/components/cbor/tinycbor/src/cbor.h" +#include "stream_message.hpp" namespace audio { - static const char* kKeyPath = "p"; - static const char* kKeyChannels = "c"; - static const char* kKeyBitsPerSample = "b"; - static const char* kKeySampleRate = "r"; - - static auto find_uint64(CborValue &map, char *key) -> cpp::optional<uint64_t> { - CborValue val; - cbor_value_map_find_value(&map, key, &val); - if (cbor_value_is_unsigned_integer(&val)) { - uint64_t raw_val; - cbor_value_get_uint64(&val, &raw_val); - return raw_val; - } - return {}; - } - - - static auto write_uint64(CborEncoder &map, const char *key, const optional<uint64_t> &val) -> cpp::result<void, StreamInfo::EncodeError> { - if (val) { - cbor_encode_byte_string(&map, key, 1); - cbor_encode_uint(&map, *val); - } - return {}; - } +static const char* kKeyPath = "p"; +static const char* kKeyChannels = "c"; +static const char* kKeyBitsPerSample = "b"; +static const char* kKeySampleRate = "r"; -static auto StreamInfo::Create(const uint8_t *buffer, size_t length) -> cpp::result<StreamInfo, ParseError> { +static auto StreamInfo::Create(const uint8_t* buffer, size_t length) + -> cpp::result<StreamInfo, ParseError> { CborParser parser; CborValue value; cbor_parser_init(buffer, len, 0, &parser, &value); uint8_t type = 0; - if (!cbor_value_is_integer(&value) - || !cbor_value_get_integer(&value, &type) - || type != STREAM_INFO) { + if (!cbor_value_is_integer(&value) || + !cbor_value_get_integer(&value, &type) || type != STREAM_INFO) { return cpp::fail(WRONG_TYPE); } @@ -55,36 +36,21 @@ static auto StreamInfo::Create(const uint8_t *buffer, size_t length) -> cpp::res StreamInfo::StreamInfo(CborValue& map) { // TODO: this method is n^2, which seems less than ideal. But you don't do it // that frequently, so maybe it's okay? Needs investigation. - channels_ = find_uint64(map, kKeyChannels); - bits_per_sample_ = find_uint64(map, kKeyBitsPerSample); - sample_rate_ = find_uint64(map, kKeySampleRate); - - CborValue val; - cbor_value_map_find_value(&map, kKeyPath, &val); - if (cbor_value_is_text_string(&val)) { - size_t len; - char *str; - cbor_value_dup_text_string(&val, &str, &len, &val); - path_ = std::string(str, len); - free(str); - } + cbor::MapDecoder decoder(map); + channels_ = decoder.FindValue(kKeyChannels); + bits_per_sample_ = decoder.FindValue(kKeyBitsPerSample); + sample_rate_ = decoder.FindValue(kKeySampleRate); + path_ = decoder.FindValue(kKeyPath); } -auto StreamInfo::WriteToStream(CborEncoder encoder) -> cpp::result<void, EncodeError> { - cbor_encode_int(&encoder, STREAM_INFO); - +auto StreamInfo::WriteToMap(cbor::Encoder& map_encoder) + -> cpp::result<size_t, EncodeError> { CborEncoder map; - cbor_encoder_create_map(&encoder, &map, length); - - write_uint64(&map, kKeyChannels, channels_); - write_uint64(&map, kKeyBitsPerSample, bits_per_sample_); - write_uint64(&map, kKeySampleRate, sample_rate_); - - if (path_) { - cbor_encode_text_string(&map, path_->c_str(), path_->size()); - } - - cbor_encoder_close_container(&encoder, &map); + map_encoder.WriteKeyValue(kKeyChannels, channels_); + map_encoder.WriteKeyValue(kKeyBitsPerSample, bits_per_sample_); + map_encoder.WriteKeyValue(kKeySampleRate, sample_rate_); + map_encoder.WriteKeyValue(kKeyPath, path_); + return map_encoder.Finish(); } -} // namespace audio +} // namespace audio |
