ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
microphone_http_stream.h
Go to the documentation of this file.
1#pragma once
2
4
5#ifdef USE_ESP32
6
14#include "esphome/core/log.h"
16
17#include <atomic>
18#include <cstddef>
19#include <cstdint>
20#include <memory>
21#include <span>
22
24
25// Keep in sync with STREAM_PATH_PREFIX in __init__.py
26static constexpr char STREAM_PATH_PREFIX[] = "/audio/microphones/";
27static constexpr size_t STREAM_PATH_PREFIX_LEN = sizeof(STREAM_PATH_PREFIX) - 1;
28// The prefix without its trailing slash
29static constexpr StringRef LIST_PATH(STREAM_PATH_PREFIX, STREAM_PATH_PREFIX_LEN - 1);
30
31enum class StreamState : uint8_t {
35 STREAM_STATE_FINISHING, // Stream deleted; waits for in-flight audio writes before becoming IDLE
36};
37
38// Keep in sync with StopReasonStrings in microphone_http_stream.cpp
46
47struct ClientSlot {
48 std::unique_ptr<ring_buffer::RingBuffer> ring;
49 AsyncStreamResponse *stream{nullptr}; // Published by the httpd task before state becomes READY
50 // HTTP chunk being sent, main loop only. The ring item stays acquired until the whole chunk is out.
51 void *chunk_data{nullptr};
52 uint32_t last_audio_ms{0}; // Main loop time; 0 until the first send pass starts the audio timeout
53 uint16_t chunk_len{0};
54 uint16_t chunk_sent{0}; // Bytes of chunk_header + data + CHUNK_END already accepted by the socket
56 std::atomic<StopReason> stop_reason{StopReason::STOP_REASON_NONE}; // Not NONE once the stream must stop
57 std::atomic<StreamState> state{StreamState::STREAM_STATE_IDLE};
58 std::atomic<uint8_t> writers{0}; // Audio callbacks currently using this slot
59
63 this->stop_reason.compare_exchange_strong(none, reason);
64 }
65};
66
67class MicrophoneHttpStream final : public Component, public AsyncWebHandler {
68 public:
72 const char *stream_path, std::span<ClientSlot> clients)
73 : source_(source), base_(base), stream_path_(stream_path), clients_(clients) {}
74
75 void setup() override;
76 void loop() override;
77 void dump_config() override;
78 float get_setup_priority() const override { return setup_priority::WIFI - 2.0f; }
79
80 bool canHandle(AsyncWebServerRequest *request) const override;
81 void handleRequest(AsyncWebServerRequest *request) override;
82
83 protected:
84 static constexpr size_t SINGLE_RING_SIZE = 16 * 1024;
85 static constexpr size_t MULTI_RING_SIZE = 128 * 1024;
86 static constexpr size_t SEND_CHUNK_SIZE = 2048;
87 static constexpr uint32_t AUDIO_WAIT_TIMEOUT_MS = 5000;
88
89 void service_client_(ClientSlot *client);
90 void send_audio_(ClientSlot *client);
91 void finish_client_(ClientSlot *client);
92 void on_audio_(std::span<const uint8_t> data);
93 void write_audio_(ClientSlot *client, std::span<const uint8_t> data);
94 const char *stream_id_() const { return this->stream_path_ + STREAM_PATH_PREFIX_LEN; }
95 unsigned client_index_(const ClientSlot *client) const {
96 return static_cast<unsigned>(client - this->clients_.data());
97 }
98
101 const char *stream_path_;
102 std::span<ClientSlot> clients_;
103 bool large_ring_{false}; // Set in setup()
104};
105
106} // namespace esphome::microphone_http_stream
107
108#endif // USE_ESP32
bool canHandle(AsyncWebServerRequest *request) const override
MicrophoneHttpStream(microphone::MicrophoneSource *source, web_server_base::WebServerBase *base, const char *stream_path, std::span< ClientSlot > clients)
unsigned client_index_(const ClientSlot *client) const
void write_audio_(ClientSlot *client, std::span< const uint8_t > data)
void handleRequest(AsyncWebServerRequest *request) override
constexpr float WIFI
Definition component.h:51
constexpr size_t CHUNK_HDR_LEN
Definition chunk.h:13
static void uint32_t
char chunk_header[web_server_base::CHUNK_HDR_LEN]
void set_stop_reason(StopReason reason)
Record the first reason the stream stops; later reasons are ignored.
std::unique_ptr< ring_buffer::RingBuffer > ring