summaryrefslogtreecommitdiff
path: root/src/audio/audio_task.cpp
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2022-12-02 13:39:00 +1100
committerjacqueline <me@jacqueline.id.au>2022-12-02 13:39:00 +1100
commit222c810b07ffc635fc7908d121e97e4d65ccc5c8 (patch)
tree91c7b5c72a11770ebf3695bf0c234597b2bc419d /src/audio/audio_task.cpp
parent71a4f5166f5491dc0982a18d62c63e28b3a52faa (diff)
downloadtangara-fw-222c810b07ffc635fc7908d121e97e4d65ccc5c8.tar.gz
fix build errors
Diffstat (limited to 'src/audio/audio_task.cpp')
-rw-r--r--src/audio/audio_task.cpp121
1 files changed, 66 insertions, 55 deletions
diff --git a/src/audio/audio_task.cpp b/src/audio/audio_task.cpp
index a125548a..f3362897 100644
--- a/src/audio/audio_task.cpp
+++ b/src/audio/audio_task.cpp
@@ -13,7 +13,7 @@
#include "freertos/stream_buffer.h"
#include "audio_element.hpp"
-#include "include/audio_element.hpp"
+#include "chunk.hpp"
#include "stream_message.hpp"
#include "tasks.hpp"
@@ -25,72 +25,83 @@ static const size_t kChunkBufferSize = kMaxChunkSize * 1.5;
auto StartAudioTask(const std::string& name,
std::shared_ptr<IAudioElement>& element) -> void {
- AudioTaskArgs* args = new AudioTaskArgs(element);
+ AudioTaskArgs* args = new AudioTaskArgs{.element = element};
xTaskCreate(&AudioTaskMain, name.c_str(), element->StackSizeBytes(), args,
kTaskPriorityAudio, NULL);
}
void AudioTaskMain(void* args) {
- AudioTaskArgs* real_args = reinterpret_cast<AudioTaskArgs*>(args);
- std::shared_ptr<IAudioElement> element = std::move(real_args->element);
- delete real_args;
-
- ChunkReader chunk_reader = ChunkReader(element->InputBuffer());
-
- while (1) {
- cpp::result<size_t, IAudioElement::StreamError> process_res;
-
- // If this element has an input stream, then our top priority is processing
- // any chunks from it. Try doing this first, then fall back to the other
- // cases.
- bool has_received_message = false;
- if (stream != nullptr) {
- EncodeReadResult chunk_res = chunk_reader.ReadChunkFromStream(
- [&](uint8_t* data, std::size_t length) -> std::optional<size_t> {
- process_res = element->ProcessChunk(data, length);
- if (process_res.has_value()) {
- return process_res.value();
- } else {
- return {};
- }
- },
- element->IdleTimeout());
-
- if (chunk_res == CHUNK_PROCESSING_ERROR ||
- chunk_res == CHUNK_DECODING_ERROR) {
- break; // TODO.
- } else if (chunk_res == CHUNK_STREAM_ENDED) {
- has_received_message = true;
+ {
+ AudioTaskArgs* real_args = reinterpret_cast<AudioTaskArgs*>(args);
+ std::shared_ptr<IAudioElement> element = std::move(real_args->element);
+ delete real_args;
+
+ ChunkReader chunk_reader = ChunkReader(element->InputBuffer());
+
+ while (1) {
+ cpp::result<size_t, StreamError> process_res;
+
+ // If this element has an input stream, then our top priority is
+ // processing any chunks from it. Try doing this first, then fall back to
+ // the other cases.
+ bool has_received_message = false;
+ if (element->InputBuffer() != nullptr) {
+ ChunkReadResult chunk_res = chunk_reader.ReadChunkFromStream(
+ [&](uint8_t* data, std::size_t length) -> std::optional<size_t> {
+ process_res = element->ProcessChunk(data, length);
+ if (process_res.has_value()) {
+ return process_res.value();
+ } else {
+ return {};
+ }
+ },
+ element->IdleTimeout());
+
+ if (chunk_res == CHUNK_PROCESSING_ERROR ||
+ chunk_res == CHUNK_DECODING_ERROR) {
+ break; // TODO.
+ } else if (chunk_res == CHUNK_STREAM_ENDED) {
+ has_received_message = true;
+ }
}
- }
- if (has_received_message) {
- auto& [buffer, length] = chunk_reader.GetLastMessage();
- auto decoder_res = cbor::ArrayDecoder::Create(buffer, length);
- if (decoder_res.has_error()) {
- // TODO.
- break;
- }
- auto decoder = decoder_res.value();
- MessageType message_type = decoder->NextValue();
- if (message_type == TYPE_STREAM_INFO) {
- element->ProcessStreamInfo(StreamInfo(decoder->Iterator()););
+ if (has_received_message) {
+ auto [buffer, length] = chunk_reader.GetLastMessage();
+ auto decoder_res = cbor::ArrayDecoder::Create(buffer, length);
+ if (decoder_res.has_error()) {
+ // TODO.
+ break;
+ }
+ auto decoder = std::move(decoder_res.value());
+ // TODO: this can be more elegant i think
+ cpp::result<uint64_t, CborError> message_type =
+ decoder->NextValue<uint64_t>();
+ if (message_type.has_error()) {
+ break; // TODO.
+ } else if (message_type.value() == TYPE_STREAM_INFO) {
+ auto info_decoder = cbor::MapDecoder::Create(decoder->Iterator());
+ if (info_decoder.has_value()) {
+ auto process_error = element->ProcessStreamInfo(
+ StreamInfo(info_decoder.value().get()));
+ if (process_error.has_error()) {
+ break; // TODO.
+ }
+ } else {
+ break; // TODO.
+ }
+ }
}
- }
- // TODO: Do any out of band reading, such a a pause command, here.
+ // TODO: Do any out of band reading, such a a pause command, here.
- // Chunk reading must have timed out, or we don't have an input stream.
- // Signal the element to do any of its idle tasks.
- process_res = element->ProcessIdle();
- if (process_res.has_error()) {
- break; // TODO.
+ // Chunk reading must have timed out, or we don't have an input stream.
+ // Signal the element to do any of its idle tasks.
+ auto process_error = element->ProcessIdle();
+ if (process_error.has_error()) {
+ break; // TODO.
+ }
}
}
-
- element.clear();
- free(chunk_buffer_);
-
vTaskDelete(NULL);
}