Fork of Tangara with customizations
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tangara-fw/src/audio/audio_task.cpp

124 lines
3.7 KiB

#include "audio_task.hpp"
#include <stdlib.h>
#include <cstdint>
2 years ago
#include <memory>
2 years ago
#include "audio_element_handle.hpp"
#include "cbor.h"
#include "esp_heap_caps.h"
2 years ago
#include "esp_log.h"
#include "freertos/portmacro.h"
#include "freertos/queue.h"
#include "freertos/stream_buffer.h"
#include "span.hpp"
#include "audio_element.hpp"
2 years ago
#include "chunk.hpp"
#include "stream_info.hpp"
#include "stream_message.hpp"
#include "tasks.hpp"
namespace audio {
auto StartAudioTask(const std::string& name,
2 years ago
std::shared_ptr<IAudioElement> element)
-> std::unique_ptr<AudioElementHandle> {
auto task_handle = std::make_unique<TaskHandle_t>();
// Newly created task will free this.
2 years ago
AudioTaskArgs* args = new AudioTaskArgs{.element = element};
2 years ago
xTaskCreate(&AudioTaskMain, name.c_str(), element->StackSizeBytes(), args,
2 years ago
kTaskPriorityAudio, task_handle.get());
return std::make_unique<AudioElementHandle>(std::move(task_handle), element);
}
void AudioTaskMain(void* args) {
2 years ago
{
AudioTaskArgs* real_args = reinterpret_cast<AudioTaskArgs*>(args);
std::shared_ptr<IAudioElement> element = std::move(real_args->element);
delete real_args;
2 years ago
char tag[] = "task";
2 years ago
ChunkReader chunk_reader = ChunkReader(element->InputBuffer());
2 years ago
while (element->ElementState() != STATE_QUIT) {
if (element->ElementState() == STATE_PAUSE) {
// TODO: park with a condition variable or something?
vTaskDelay(100);
continue;
}
cpp::result<size_t, AudioProcessingError> process_res;
2 years ago
// 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;
ChunkReadResult chunk_res = chunk_reader.ReadChunkFromStream(
[&](cpp::span<std::byte> data) -> std::optional<size_t> {
process_res = element->ProcessChunk(data);
if (process_res.has_value()) {
return process_res.value();
} else {
return {};
}
},
element->IdleTimeout());
if (chunk_res == CHUNK_PROCESSING_ERROR ||
chunk_res == CHUNK_DECODING_ERROR) {
2 years ago
ESP_LOGE(tag, "failed to process chunk");
break; // TODO.
} else if (chunk_res == CHUNK_STREAM_ENDED) {
has_received_message = true;
}
2 years ago
if (has_received_message) {
auto message = chunk_reader.GetLastMessage();
MessageType type = ReadMessageType(message);
if (type == TYPE_STREAM_INFO) {
auto parse_res = ReadMessage<StreamInfo>(&StreamInfo::Parse, message);
if (parse_res.has_error()) {
2 years ago
ESP_LOGE(tag, "failed to parse stream info");
break; // TODO.
}
auto info_res = element->ProcessStreamInfo(parse_res.value());
if (info_res.has_error()) {
2 years ago
ESP_LOGE(tag, "failed to process stream info");
2 years ago
break; // TODO.
}
}
}
2 years ago
// Chunk reading must have timed out, or we don't have an input stream.
2 years ago
ElementState state = element->ElementState();
if (state == STATE_PAUSE) {
element->PrepareForPause();
vTaskSuspend(NULL);
// Zzzzzz...
// When we wake up, skip straight to the start of the loop again.
continue;
} else if (state == STATE_QUIT) {
break;
}
2 years ago
// Signal the element to do any of its idle tasks.
auto process_error = element->ProcessIdle();
if (process_error.has_error()) {
2 years ago
ESP_LOGE(tag, "failed to process idle");
2 years ago
break; // TODO.
}
}
}
vTaskDelete(NULL);
}
} // namespace audio