@geastack/host 0.1.10 → 0.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/audio_runtime.cpp +97 -3
- package/host/audio.cpp +395 -10
- package/host/audio_worklet.cpp +611 -0
- package/host/fetch.cpp +17 -0
- package/host/media.cpp +308 -27
- package/host/mjpeg.cpp +205 -0
- package/host/rtc.cpp +458 -48
- package/host/rtc_esp.cpp +897 -75
- package/host/rtc_objects.inc +246 -0
- package/host/timers.cpp +18 -0
- package/host/video.cpp +152 -0
- package/host/websocket.cpp +623 -72
- package/host/worker.cpp +621 -0
- package/include/gea/audio-worklet-runtime.h +165 -0
- package/include/gea/base64-runtime.h +26 -0
- package/include/gea/embedded-host.h +1 -0
- package/include/gea/worker-runtime.h +189 -0
- package/include/host/audio.h +94 -6
- package/include/host/audio_level.h +37 -0
- package/include/host/audio_worklet.h +103 -0
- package/include/host/h264_layout.h +170 -0
- package/include/host/media.h +67 -0
- package/include/host/mjpeg.h +23 -0
- package/include/host/mjpeg_parser.h +126 -0
- package/include/host/pcm_stream.h +122 -0
- package/include/host/rtc.h +38 -20
- package/include/host/rtc_h264.h +76 -0
- package/include/host/rtc_ice_candidate.h +141 -0
- package/include/host/rtc_opus.h +136 -0
- package/include/host/rtc_receive_sdp.h +245 -0
- package/include/host/rtc_rtp.h +170 -0
- package/include/host/rtc_sdp.h +71 -0
- package/include/host/rtc_types.h +166 -0
- package/include/host/rtc_vp8.h +115 -0
- package/include/host/rtc_vp8_capture.h +129 -0
- package/include/host/rtc_vp8_decoder.h +169 -0
- package/include/host/rtc_vp8_receiver.h +55 -0
- package/include/host/timers.h +3 -2
- package/include/host/video.h +39 -0
- package/include/host/video_color.h +90 -0
- package/include/host/video_frame_pool.h +50 -0
- package/include/host/video_pixels.h +178 -0
- package/include/host/websocket.h +20 -1
- package/include/host/websocket_message.h +66 -0
- package/include/host/worker.h +131 -0
- package/package.json +1 -1
- package/services/diagnostics.cpp +120 -19
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
#include <algorithm>
|
|
5
|
+
#include <cstdint>
|
|
6
|
+
#include <cstdlib>
|
|
7
|
+
#include <cstring>
|
|
8
|
+
#include <limits>
|
|
9
|
+
#include <memory>
|
|
10
|
+
#include <new>
|
|
11
|
+
#include <utility>
|
|
12
|
+
#include <type_traits>
|
|
13
|
+
#ifdef ESP_PLATFORM
|
|
14
|
+
#include "esp_heap_caps.h"
|
|
15
|
+
#endif
|
|
16
|
+
|
|
17
|
+
namespace gea::host::media {
|
|
18
|
+
class VideoFramePool;
|
|
19
|
+
|
|
20
|
+
// Packet containers retain the standard allocator contract. Large decoded
|
|
21
|
+
// pictures below have an explicit non-throwing allocation boundary instead.
|
|
22
|
+
struct VideoOverwriteTag {};
|
|
23
|
+
template<class T>
|
|
24
|
+
struct VideoAllocator {
|
|
25
|
+
using value_type = T;
|
|
26
|
+
bool valueInitialize = true;
|
|
27
|
+
VideoAllocator() = default;
|
|
28
|
+
explicit VideoAllocator(VideoOverwriteTag) : valueInitialize(false) {}
|
|
29
|
+
template<class U> VideoAllocator(const VideoAllocator<U>& other)
|
|
30
|
+
: valueInitialize(other.valueInitialize) {}
|
|
31
|
+
template<class U, class... Args>
|
|
32
|
+
void construct(U* pointer, Args&&... args) {
|
|
33
|
+
if constexpr (sizeof...(Args) == 0 && std::is_same_v<U, std::uint16_t>) {
|
|
34
|
+
if (!valueInitialize) { ::new (static_cast<void*>(pointer)) U; return; }
|
|
35
|
+
}
|
|
36
|
+
::new (static_cast<void*>(pointer)) U(std::forward<Args>(args)...);
|
|
37
|
+
}
|
|
38
|
+
T* allocate(std::size_t count) {
|
|
39
|
+
if (count > std::numeric_limits<std::size_t>::max() / sizeof(T)) throw std::bad_alloc();
|
|
40
|
+
#ifdef ESP_PLATFORM
|
|
41
|
+
constexpr std::size_t alignment = alignof(T) > 16 ? alignof(T) : 16;
|
|
42
|
+
void* result = heap_caps_aligned_alloc(alignment, count * sizeof(T),
|
|
43
|
+
MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
|
44
|
+
#else
|
|
45
|
+
void* result = std::malloc(count * sizeof(T));
|
|
46
|
+
#endif
|
|
47
|
+
if (!result && count) throw std::bad_alloc();
|
|
48
|
+
return static_cast<T*>(result);
|
|
49
|
+
}
|
|
50
|
+
void deallocate(T* pointer, std::size_t) noexcept {
|
|
51
|
+
#ifdef ESP_PLATFORM
|
|
52
|
+
heap_caps_free(pointer);
|
|
53
|
+
#else
|
|
54
|
+
std::free(pointer);
|
|
55
|
+
#endif
|
|
56
|
+
}
|
|
57
|
+
template<class U> bool operator==(const VideoAllocator<U>&) const noexcept { return true; }
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// Owned, SIMD-aligned pixels. The decoder uses the fallible overwrite operation:
|
|
61
|
+
// allocation failure must not itself allocate a C++ exception while under memory
|
|
62
|
+
// pressure. A renderer may retain an older immutable frame during replacement.
|
|
63
|
+
class VideoPixels {
|
|
64
|
+
public:
|
|
65
|
+
VideoPixels() = default;
|
|
66
|
+
explicit VideoPixels(std::size_t count) { resize(count); }
|
|
67
|
+
VideoPixels(const VideoPixels& other) {
|
|
68
|
+
if (!tryResizeForOverwrite(other.size_)) throw std::bad_alloc();
|
|
69
|
+
if (size_) std::memcpy(pixels_, other.pixels_, size_ * sizeof(*pixels_));
|
|
70
|
+
}
|
|
71
|
+
VideoPixels(VideoPixels&& other) noexcept { swap(other); }
|
|
72
|
+
VideoPixels& operator=(const VideoPixels& other) {
|
|
73
|
+
if (this != &other) { VideoPixels copy(other); swap(copy); }
|
|
74
|
+
return *this;
|
|
75
|
+
}
|
|
76
|
+
VideoPixels& operator=(VideoPixels&& other) noexcept {
|
|
77
|
+
if (this != &other) { VideoPixels moved(std::move(other)); swap(moved); }
|
|
78
|
+
return *this;
|
|
79
|
+
}
|
|
80
|
+
~VideoPixels() { releaseAllocation(); }
|
|
81
|
+
|
|
82
|
+
// Every pixel must be written before publication. Failure preserves the
|
|
83
|
+
// previous allocation, size and contents, including on integer overflow.
|
|
84
|
+
bool tryResizeForOverwrite(std::size_t count) noexcept {
|
|
85
|
+
if (count > capacity_) {
|
|
86
|
+
auto* replacement = allocate(count);
|
|
87
|
+
if (!replacement) return false;
|
|
88
|
+
releaseAllocation();
|
|
89
|
+
pixels_ = replacement;
|
|
90
|
+
capacity_ = count;
|
|
91
|
+
}
|
|
92
|
+
size_ = count;
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
void resize(std::size_t count) {
|
|
97
|
+
const auto previousSize = size_;
|
|
98
|
+
if (count > capacity_) {
|
|
99
|
+
auto* replacement = allocate(count);
|
|
100
|
+
if (!replacement) throw std::bad_alloc();
|
|
101
|
+
if (previousSize) std::memcpy(replacement, pixels_, previousSize * sizeof(*pixels_));
|
|
102
|
+
releaseAllocation();
|
|
103
|
+
pixels_ = replacement;
|
|
104
|
+
capacity_ = count;
|
|
105
|
+
}
|
|
106
|
+
if (count > previousSize) std::fill_n(pixels_ + previousSize, count - previousSize, 0);
|
|
107
|
+
size_ = count;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
void assign(std::size_t count, std::uint16_t value) {
|
|
111
|
+
if (!tryResizeForOverwrite(count)) throw std::bad_alloc();
|
|
112
|
+
if (count) std::fill_n(pixels_, count, value);
|
|
113
|
+
}
|
|
114
|
+
void pop_back() { if (size_) --size_; }
|
|
115
|
+
void swap(VideoPixels& other) noexcept {
|
|
116
|
+
std::swap(pixels_, other.pixels_);
|
|
117
|
+
std::swap(size_, other.size_);
|
|
118
|
+
std::swap(capacity_, other.capacity_);
|
|
119
|
+
pooled_.swap(other.pooled_);
|
|
120
|
+
}
|
|
121
|
+
std::size_t size() const noexcept { return size_; }
|
|
122
|
+
bool empty() const noexcept { return !size_; }
|
|
123
|
+
std::uint16_t* data() noexcept { return pixels_; }
|
|
124
|
+
const std::uint16_t* data() const noexcept { return pixels_; }
|
|
125
|
+
std::uint16_t* begin() noexcept { return pixels_; }
|
|
126
|
+
const std::uint16_t* begin() const noexcept { return pixels_; }
|
|
127
|
+
std::uint16_t* end() noexcept { return size_ ? pixels_ + size_ : pixels_; }
|
|
128
|
+
const std::uint16_t* end() const noexcept { return size_ ? pixels_ + size_ : pixels_; }
|
|
129
|
+
std::uint16_t& operator[](std::size_t index) { return pixels_[index]; }
|
|
130
|
+
const std::uint16_t& operator[](std::size_t index) const { return pixels_[index]; }
|
|
131
|
+
std::uint16_t& front() { return pixels_[0]; }
|
|
132
|
+
const std::uint16_t& front() const { return pixels_[0]; }
|
|
133
|
+
bool operator==(const VideoPixels& other) const {
|
|
134
|
+
return size_ == other.size_ && (!size_ || std::equal(begin(), end(), other.begin()));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
private:
|
|
138
|
+
friend class VideoFramePool;
|
|
139
|
+
struct Storage {
|
|
140
|
+
std::uint16_t* pixels = nullptr;
|
|
141
|
+
std::size_t capacity = 0;
|
|
142
|
+
~Storage() { release(pixels); }
|
|
143
|
+
};
|
|
144
|
+
void releaseAllocation() noexcept {
|
|
145
|
+
if (pooled_) pooled_.reset();
|
|
146
|
+
else release(pixels_);
|
|
147
|
+
}
|
|
148
|
+
static std::uint16_t* allocate(std::size_t count) noexcept {
|
|
149
|
+
if (count > std::numeric_limits<std::size_t>::max() / sizeof(std::uint16_t)) return nullptr;
|
|
150
|
+
const auto bytes = count * sizeof(std::uint16_t);
|
|
151
|
+
#ifdef ESP_PLATFORM
|
|
152
|
+
return static_cast<std::uint16_t*>(heap_caps_aligned_alloc(
|
|
153
|
+
16, bytes, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
|
|
154
|
+
#else
|
|
155
|
+
// Round up without overflowing: aligned_alloc requires a multiple of 16.
|
|
156
|
+
if (bytes > std::numeric_limits<std::size_t>::max() - 15) return nullptr;
|
|
157
|
+
return static_cast<std::uint16_t*>(std::aligned_alloc(16, (bytes + 15) & ~std::size_t(15)));
|
|
158
|
+
#endif
|
|
159
|
+
}
|
|
160
|
+
static void release(std::uint16_t* pointer) noexcept {
|
|
161
|
+
#ifdef ESP_PLATFORM
|
|
162
|
+
heap_caps_free(pointer);
|
|
163
|
+
#else
|
|
164
|
+
std::free(pointer);
|
|
165
|
+
#endif
|
|
166
|
+
}
|
|
167
|
+
std::uint16_t* pixels_ = nullptr;
|
|
168
|
+
std::size_t size_ = 0;
|
|
169
|
+
std::size_t capacity_ = 0;
|
|
170
|
+
std::shared_ptr<Storage> pooled_;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
inline VideoPixels videoPixelsForOverwrite(std::size_t count) {
|
|
174
|
+
VideoPixels pixels;
|
|
175
|
+
if (!pixels.tryResizeForOverwrite(count)) throw std::bad_alloc();
|
|
176
|
+
return pixels;
|
|
177
|
+
}
|
|
178
|
+
} // namespace gea::host::media
|
package/include/host/websocket.h
CHANGED
|
@@ -77,6 +77,7 @@ class WebSocket {
|
|
|
77
77
|
|
|
78
78
|
std::string url() const;
|
|
79
79
|
double readyState() const;
|
|
80
|
+
double bufferedAmount() const;
|
|
80
81
|
void send(const std::string &data) const;
|
|
81
82
|
void close() const;
|
|
82
83
|
void close(double code) const;
|
|
@@ -94,9 +95,27 @@ struct CallbackTable {
|
|
|
94
95
|
|
|
95
96
|
std::unordered_map<NativeWebSocketHandle, CallbackTable> &callbackTable();
|
|
96
97
|
|
|
97
|
-
NativeWebSocketHandle create_handle(const std::string &url);
|
|
98
|
+
NativeWebSocketHandle create_handle(const std::string &url, const std::string &protocols = {});
|
|
99
|
+
template <typename Protocols>
|
|
100
|
+
NativeWebSocketHandle create_handle(const std::string &url, const Protocols &protocols) {
|
|
101
|
+
std::string joined;
|
|
102
|
+
for (std::size_t i = 0; i < protocols.size(); ++i) {
|
|
103
|
+
if (i) joined += ",";
|
|
104
|
+
joined += protocols.at(i);
|
|
105
|
+
}
|
|
106
|
+
return create_handle(url, joined);
|
|
107
|
+
}
|
|
98
108
|
void destroy_handle(NativeWebSocketHandle handle);
|
|
99
109
|
|
|
110
|
+
// Native streaming sources run on the socket's sender, never on the UI loop.
|
|
111
|
+
// An empty result means no packet is ready yet. The producer must not call JS.
|
|
112
|
+
using TextProducer = std::function<std::string()>;
|
|
113
|
+
void setTextProducer(NativeWebSocketHandle handle, TextProducer producer);
|
|
114
|
+
// A native sink can consume media before JS event dispatch. Returning false
|
|
115
|
+
// leaves an ordinary control message for the owner's normal onmessage handler.
|
|
116
|
+
using TextConsumer = std::function<bool(const std::string&)>;
|
|
117
|
+
void setTextConsumer(NativeWebSocketHandle handle, TextConsumer consumer);
|
|
118
|
+
|
|
100
119
|
void runCallbacks();
|
|
101
120
|
|
|
102
121
|
void test_inject_open(NativeWebSocketHandle handle);
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <cstddef>
|
|
4
|
+
#include <string>
|
|
5
|
+
#include <string_view>
|
|
6
|
+
#include <utility>
|
|
7
|
+
|
|
8
|
+
namespace gea::host::websocket {
|
|
9
|
+
|
|
10
|
+
// ESP-IDF splits large frames into DATA events; WebSocket continuation frames
|
|
11
|
+
// may split one message further. JavaScript receives one complete text message.
|
|
12
|
+
class TextMessageAssembler {
|
|
13
|
+
public:
|
|
14
|
+
enum class Result { Pending, Complete, Invalid };
|
|
15
|
+
|
|
16
|
+
Result append(int opcode, bool fin, std::size_t offset, std::size_t frameBytes,
|
|
17
|
+
std::string_view bytes) {
|
|
18
|
+
if (opcode >= 8) return Result::Pending; // Interleaved ping/pong/close.
|
|
19
|
+
if (offset == 0) {
|
|
20
|
+
if (expectedOffset_ != frameBytes_) return invalid();
|
|
21
|
+
if (opcode == 1) {
|
|
22
|
+
if (textOpen_) return invalid();
|
|
23
|
+
textOpen_ = true;
|
|
24
|
+
message_.clear();
|
|
25
|
+
} else if (opcode == 2) {
|
|
26
|
+
if (textOpen_) return invalid();
|
|
27
|
+
} else if (opcode != 0) {
|
|
28
|
+
return invalid();
|
|
29
|
+
}
|
|
30
|
+
expectedOffset_ = 0;
|
|
31
|
+
frameBytes_ = frameBytes;
|
|
32
|
+
opcode_ = opcode;
|
|
33
|
+
}
|
|
34
|
+
if (offset != expectedOffset_ || frameBytes != frameBytes_ || opcode != opcode_ ||
|
|
35
|
+
offset > frameBytes || bytes.size() > frameBytes - offset) return invalid();
|
|
36
|
+
expectedOffset_ += bytes.size();
|
|
37
|
+
if (!textOpen_) return Result::Pending; // Binary messages are not exposed here.
|
|
38
|
+
if (bytes.size() > kMaxMessageBytes - message_.size()) return invalid();
|
|
39
|
+
message_.append(bytes);
|
|
40
|
+
if (fin && expectedOffset_ == frameBytes_) {
|
|
41
|
+
textOpen_ = false;
|
|
42
|
+
return Result::Complete;
|
|
43
|
+
}
|
|
44
|
+
return Result::Pending;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
std::string take() { return std::exchange(message_, {}); }
|
|
48
|
+
|
|
49
|
+
private:
|
|
50
|
+
Result invalid() {
|
|
51
|
+
message_.clear();
|
|
52
|
+
textOpen_ = false;
|
|
53
|
+
expectedOffset_ = frameBytes_ = 0;
|
|
54
|
+
opcode_ = -1;
|
|
55
|
+
return Result::Invalid;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
static constexpr std::size_t kMaxMessageBytes = 1024 * 1024;
|
|
59
|
+
std::string message_;
|
|
60
|
+
std::size_t expectedOffset_ = 0;
|
|
61
|
+
std::size_t frameBytes_ = 0;
|
|
62
|
+
int opcode_ = -1;
|
|
63
|
+
bool textOpen_ = false;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
} // namespace gea::host::websocket
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
#include <cstddef>
|
|
5
|
+
#include <cstdint>
|
|
6
|
+
#include <functional>
|
|
7
|
+
#include <memory>
|
|
8
|
+
#include <string>
|
|
9
|
+
#include <variant>
|
|
10
|
+
#include <vector>
|
|
11
|
+
|
|
12
|
+
namespace gea::host::workers {
|
|
13
|
+
|
|
14
|
+
// Native execution realms. Only native ownership-safe messages may cross realms;
|
|
15
|
+
// generated Ref objects and callbacks remain local to their creating realm.
|
|
16
|
+
class Context : public std::enable_shared_from_this<Context> {
|
|
17
|
+
public:
|
|
18
|
+
struct State;
|
|
19
|
+
static std::shared_ptr<Context> current();
|
|
20
|
+
static std::shared_ptr<Context> main();
|
|
21
|
+
static void runMainPending();
|
|
22
|
+
static const char *mainPendingStage();
|
|
23
|
+
static bool isWorkerCurrent();
|
|
24
|
+
// Native scheduling only: -1 leaves affinity unset; desktop ignores core.
|
|
25
|
+
static std::shared_ptr<Context> create(const std::string &name, int priority = 8, int core = -1);
|
|
26
|
+
~Context();
|
|
27
|
+
bool post(std::function<void()> callback, std::size_t bytes = 0);
|
|
28
|
+
void start(std::function<void()> entry);
|
|
29
|
+
void stop();
|
|
30
|
+
// Called on the owner task before destroying its compiler realm.
|
|
31
|
+
void finish();
|
|
32
|
+
bool stopped() const;
|
|
33
|
+
bool isMain() const;
|
|
34
|
+
void runPending(double budgetMs = 4);
|
|
35
|
+
// Audio protocol callbacks must share CPU with UI, below capture/mixer.
|
|
36
|
+
// Owner-task only; no application-level scheduling knob.
|
|
37
|
+
void prioritizeAudio();
|
|
38
|
+
void setPump(std::function<void()> pump);
|
|
39
|
+
void addCleanup(std::function<void()> cleanup);
|
|
40
|
+
void setErrorHandler(std::function<void(const std::string &)> handler);
|
|
41
|
+
double schedule(std::function<void()> callback, double delayMs, bool repeat);
|
|
42
|
+
void cancel(double id);
|
|
43
|
+
private:
|
|
44
|
+
explicit Context(bool main, std::string name, int priority = 8, int core = -1);
|
|
45
|
+
void loop(std::function<void()> entry);
|
|
46
|
+
std::unique_ptr<State> state_;
|
|
47
|
+
friend class ContextScope;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
class ContextScope {
|
|
51
|
+
public:
|
|
52
|
+
explicit ContextScope(std::shared_ptr<Context> context);
|
|
53
|
+
~ContextScope();
|
|
54
|
+
private:
|
|
55
|
+
std::shared_ptr<Context> previous_;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
struct MessageData {
|
|
59
|
+
using Bytes = std::vector<std::uint8_t>;
|
|
60
|
+
std::variant<std::string, Bytes> value;
|
|
61
|
+
MessageData() : value(std::string{}) {}
|
|
62
|
+
MessageData(std::string text) : value(std::move(text)) {}
|
|
63
|
+
MessageData(const char *text) : value(std::string(text)) {}
|
|
64
|
+
MessageData(Bytes bytes) : value(std::move(bytes)) {}
|
|
65
|
+
std::size_t byteSize() const;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
struct MessageEvent;
|
|
69
|
+
class MessagePort {
|
|
70
|
+
public:
|
|
71
|
+
struct Endpoint;
|
|
72
|
+
struct Ownership;
|
|
73
|
+
MessagePort() = default;
|
|
74
|
+
explicit operator bool() const { return bool(ownership_); }
|
|
75
|
+
void postMessage(MessageData data, const std::vector<MessagePort> &transfer = {}) const;
|
|
76
|
+
void validateTransfers(const std::vector<MessagePort> &transfer) const;
|
|
77
|
+
void setOnMessage(std::function<void(MessageEvent &)> callback) const;
|
|
78
|
+
void start() const;
|
|
79
|
+
void close() const;
|
|
80
|
+
bool detached() const;
|
|
81
|
+
const void *identity() const { return ownership_.get(); }
|
|
82
|
+
// Host-only transfer primitives. Validate the whole list before detaching any
|
|
83
|
+
// port. Adoption takes place on the receiving task, never the sending task.
|
|
84
|
+
MessagePort transfer() const;
|
|
85
|
+
void adopt(const std::shared_ptr<Context> &owner) const;
|
|
86
|
+
private:
|
|
87
|
+
explicit MessagePort(std::shared_ptr<Ownership> ownership);
|
|
88
|
+
MessagePort transfer(std::function<void(MessageEvent &)> &retiredHandler) const;
|
|
89
|
+
std::shared_ptr<Ownership> ownership_;
|
|
90
|
+
friend class MessageChannel;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
struct MessageEvent {
|
|
94
|
+
MessageData data;
|
|
95
|
+
std::vector<MessagePort> ports;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
class MessageChannel {
|
|
99
|
+
public:
|
|
100
|
+
MessageChannel();
|
|
101
|
+
MessagePort port1;
|
|
102
|
+
MessagePort port2;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
void setRealmRunner(std::function<void(std::function<void()>)> runner);
|
|
106
|
+
void registerModule(const std::string &url, std::function<void()> entry);
|
|
107
|
+
class DedicatedWorkerGlobalScope {
|
|
108
|
+
public:
|
|
109
|
+
void postMessage(MessageData data, const std::vector<MessagePort> &transfer = {}) const;
|
|
110
|
+
void validateTransfers(const std::vector<MessagePort> &transfer) const;
|
|
111
|
+
void setOnMessage(std::function<void(MessageEvent &)> callback) const;
|
|
112
|
+
void close() const;
|
|
113
|
+
};
|
|
114
|
+
DedicatedWorkerGlobalScope self();
|
|
115
|
+
|
|
116
|
+
class Worker {
|
|
117
|
+
public:
|
|
118
|
+
Worker() = default;
|
|
119
|
+
explicit operator bool() const { return bool(state_); }
|
|
120
|
+
static Worker create(const std::string &url);
|
|
121
|
+
void postMessage(MessageData data, const std::vector<MessagePort> &transfer = {}) const;
|
|
122
|
+
void validateTransfers(const std::vector<MessagePort> &transfer) const;
|
|
123
|
+
void setOnMessage(std::function<void(MessageEvent &)> callback) const;
|
|
124
|
+
void setOnError(std::function<void(const std::string &)> callback) const;
|
|
125
|
+
void terminate() const;
|
|
126
|
+
private:
|
|
127
|
+
struct State;
|
|
128
|
+
std::shared_ptr<State> state_;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
} // namespace gea::host::workers
|
package/package.json
CHANGED
package/services/diagnostics.cpp
CHANGED
|
@@ -5,10 +5,19 @@
|
|
|
5
5
|
#include "memory.h"
|
|
6
6
|
#include "services/diagnostics_platform.h"
|
|
7
7
|
|
|
8
|
+
#include <algorithm>
|
|
8
9
|
#include <cstdarg>
|
|
9
10
|
#include <cstddef>
|
|
10
11
|
#include <cstdio>
|
|
12
|
+
#include <cstring>
|
|
11
13
|
#include <mutex>
|
|
14
|
+
#ifdef ESP_PLATFORM
|
|
15
|
+
#include <atomic>
|
|
16
|
+
#include "esp_heap_caps.h"
|
|
17
|
+
#include "freertos/FreeRTOS.h"
|
|
18
|
+
#include "freertos/task.h"
|
|
19
|
+
#include "freertos/idf_additions.h"
|
|
20
|
+
#endif
|
|
12
21
|
|
|
13
22
|
namespace gea::framework::services {
|
|
14
23
|
|
|
@@ -36,11 +45,12 @@ public:
|
|
|
36
45
|
bool begin()
|
|
37
46
|
{
|
|
38
47
|
std::lock_guard<std::mutex> guard(mutex_);
|
|
48
|
+
if (data_) return true;
|
|
39
49
|
if (!data_) {
|
|
40
50
|
data_ = static_cast<char *>(DiagnosticsMemory::allocate(kLogRingSize));
|
|
41
51
|
if (!data_) {
|
|
42
|
-
std::fputs("Diagnostics log ring allocation failed
|
|
43
|
-
return
|
|
52
|
+
std::fputs("Diagnostics log ring allocation failed\n", stdout);
|
|
53
|
+
return false;
|
|
44
54
|
}
|
|
45
55
|
}
|
|
46
56
|
head_ = 0;
|
|
@@ -54,12 +64,16 @@ public:
|
|
|
54
64
|
if (!data || len == 0) return;
|
|
55
65
|
std::lock_guard<std::mutex> guard(mutex_);
|
|
56
66
|
if (!data_) return;
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
67
|
+
// Retain the latest ringful, preserving absolute byte positions even
|
|
68
|
+
// when one message itself is larger than the ring.
|
|
69
|
+
const std::size_t retained = std::min(len, kLogRingSize);
|
|
70
|
+
const std::size_t begin = (head_ + len - retained) % kLogRingSize;
|
|
71
|
+
const std::size_t first = std::min(retained, kLogRingSize - begin);
|
|
72
|
+
std::memcpy(data_ + begin, data + len - retained, first);
|
|
73
|
+
std::memcpy(data_, data + len - retained + first, retained - first);
|
|
74
|
+
head_ = (head_ + len) % kLogRingSize;
|
|
75
|
+
used_ = std::min(kLogRingSize, used_ + len);
|
|
76
|
+
total_ += len;
|
|
63
77
|
}
|
|
64
78
|
|
|
65
79
|
std::size_t oldestTotal()
|
|
@@ -86,10 +100,10 @@ public:
|
|
|
86
100
|
std::size_t count = available > static_cast<std::size_t>(cap)
|
|
87
101
|
? static_cast<std::size_t>(cap)
|
|
88
102
|
: available;
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
103
|
+
const std::size_t begin = *cursor % kLogRingSize;
|
|
104
|
+
const std::size_t first = std::min(count, kLogRingSize - begin);
|
|
105
|
+
std::memcpy(dst, data_ + begin, first);
|
|
106
|
+
std::memcpy(dst + first, data_, count - first);
|
|
93
107
|
*cursor += count;
|
|
94
108
|
return static_cast<int>(count);
|
|
95
109
|
}
|
|
@@ -113,6 +127,91 @@ LogRing &logRing()
|
|
|
113
127
|
return ring;
|
|
114
128
|
}
|
|
115
129
|
|
|
130
|
+
#ifdef ESP_PLATFORM
|
|
131
|
+
std::atomic<TaskHandle_t> consoleTask{nullptr};
|
|
132
|
+
|
|
133
|
+
void consoleLogTask(void *)
|
|
134
|
+
{
|
|
135
|
+
std::size_t cursor = logRing().oldestTotal();
|
|
136
|
+
char chunk[2048];
|
|
137
|
+
std::size_t pending = 0;
|
|
138
|
+
for (;;) {
|
|
139
|
+
const std::size_t oldest = logRing().oldestTotal();
|
|
140
|
+
if (cursor < oldest) {
|
|
141
|
+
std::fprintf(stdout, "[gea_log] dropped_bytes=%u\n",
|
|
142
|
+
static_cast<unsigned>(oldest - cursor));
|
|
143
|
+
cursor = oldest;
|
|
144
|
+
pending = 0;
|
|
145
|
+
}
|
|
146
|
+
const int count = logRing().copySince(&cursor, chunk + pending, sizeof(chunk) - pending);
|
|
147
|
+
if (count) {
|
|
148
|
+
pending += static_cast<std::size_t>(count);
|
|
149
|
+
std::size_t complete = pending;
|
|
150
|
+
while (complete && chunk[complete - 1] != '\n') --complete;
|
|
151
|
+
// UART VFS can spin while its FIFO drains. Only this background
|
|
152
|
+
// task may pay that cost; no ring mutex is held during the write.
|
|
153
|
+
// Emit complete lines together so GEADEV replies cannot appear
|
|
154
|
+
// inside a partially emitted diagnostic line.
|
|
155
|
+
if (complete) {
|
|
156
|
+
std::fwrite(chunk, 1, complete, stdout);
|
|
157
|
+
pending -= complete;
|
|
158
|
+
std::memmove(chunk, chunk + complete, pending);
|
|
159
|
+
} else if (pending == sizeof(chunk)) {
|
|
160
|
+
// Bound arbitrarily long lines without pinning the console lock
|
|
161
|
+
// across task sleeps. The network ring keeps the original bytes.
|
|
162
|
+
flockfile(stdout);
|
|
163
|
+
std::fwrite(chunk, 1, pending, stdout);
|
|
164
|
+
std::fputs("\n[gea_log] continued_line=1\n", stdout);
|
|
165
|
+
funlockfile(stdout);
|
|
166
|
+
pending = 0;
|
|
167
|
+
}
|
|
168
|
+
vTaskDelay(1);
|
|
169
|
+
} else {
|
|
170
|
+
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
bool beginConsoleLogTask()
|
|
176
|
+
{
|
|
177
|
+
static std::mutex startupMutex;
|
|
178
|
+
std::lock_guard<std::mutex> guard(startupMutex);
|
|
179
|
+
if (consoleTask.load(std::memory_order_acquire)) return true;
|
|
180
|
+
TaskHandle_t task = nullptr;
|
|
181
|
+
BaseType_t created = pdFAIL;
|
|
182
|
+
#if CONFIG_FREERTOS_TASK_CREATE_ALLOW_EXT_MEM
|
|
183
|
+
// The 2 KB line staging buffer plus newlib/VFS write frames exceeds 4 KB.
|
|
184
|
+
// Keep ample console stack in PSRAM; audio never uses it.
|
|
185
|
+
created = xTaskCreateWithCaps(consoleLogTask, "gea_log", 64 * 1024, nullptr, 1,
|
|
186
|
+
&task, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
|
187
|
+
#endif
|
|
188
|
+
if (created != pdPASS) {
|
|
189
|
+
created = xTaskCreate(consoleLogTask, "gea_log", 8192, nullptr, 1, &task);
|
|
190
|
+
}
|
|
191
|
+
if (created != pdPASS) {
|
|
192
|
+
std::fputs("Diagnostics console task allocation failed\n", stdout);
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
consoleTask.store(task, std::memory_order_release);
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
#endif
|
|
199
|
+
|
|
200
|
+
void emitLog(const char *data, std::size_t len)
|
|
201
|
+
{
|
|
202
|
+
logRing().write(data, len);
|
|
203
|
+
#ifdef ESP_PLATFORM
|
|
204
|
+
if (auto task = consoleTask.load(std::memory_order_acquire)) {
|
|
205
|
+
xTaskNotifyGive(task);
|
|
206
|
+
} else {
|
|
207
|
+
// Preserve early boot output before the diagnostics service starts.
|
|
208
|
+
std::fwrite(data, 1, len, stdout);
|
|
209
|
+
}
|
|
210
|
+
#else
|
|
211
|
+
std::fwrite(data, 1, len, stdout);
|
|
212
|
+
#endif
|
|
213
|
+
}
|
|
214
|
+
|
|
116
215
|
} // namespace
|
|
117
216
|
|
|
118
217
|
namespace diagnostics_internal {
|
|
@@ -146,7 +245,12 @@ int copyLogSince(std::size_t *cursor, char *dst, int cap)
|
|
|
146
245
|
|
|
147
246
|
bool DiagnosticsServer::begin()
|
|
148
247
|
{
|
|
149
|
-
|
|
248
|
+
if (!diagnostics_internal::beginLogRing()) return false;
|
|
249
|
+
#ifdef ESP_PLATFORM
|
|
250
|
+
return beginConsoleLogTask();
|
|
251
|
+
#else
|
|
252
|
+
return true;
|
|
253
|
+
#endif
|
|
150
254
|
}
|
|
151
255
|
|
|
152
256
|
int DiagnosticsServer::port()
|
|
@@ -174,8 +278,7 @@ int formatAndEmitLog(const char *fmt, std::va_list args)
|
|
|
174
278
|
return len;
|
|
175
279
|
}
|
|
176
280
|
if (len < static_cast<int>(sizeof(stackBuf))) {
|
|
177
|
-
|
|
178
|
-
diagnostics_internal::writeLog(stackBuf, static_cast<std::size_t>(len));
|
|
281
|
+
emitLog(stackBuf, static_cast<std::size_t>(len));
|
|
179
282
|
va_end(argsCopy);
|
|
180
283
|
return len;
|
|
181
284
|
}
|
|
@@ -183,13 +286,11 @@ int formatAndEmitLog(const char *fmt, std::va_list args)
|
|
|
183
286
|
char *heapBuf = static_cast<char *>(gea::framework::memory::Allocator::allocatePreferSpiram(size));
|
|
184
287
|
if (heapBuf) {
|
|
185
288
|
std::vsnprintf(heapBuf, size, fmt, argsCopy);
|
|
186
|
-
|
|
187
|
-
diagnostics_internal::writeLog(heapBuf, static_cast<std::size_t>(len));
|
|
289
|
+
emitLog(heapBuf, static_cast<std::size_t>(len));
|
|
188
290
|
gea::framework::memory::Allocator::free(heapBuf);
|
|
189
291
|
} else {
|
|
190
292
|
// Allocation failed: emit the clipped stack buffer rather than nothing.
|
|
191
|
-
|
|
192
|
-
diagnostics_internal::writeLog(stackBuf, sizeof(stackBuf) - 1);
|
|
293
|
+
emitLog(stackBuf, sizeof(stackBuf) - 1);
|
|
193
294
|
}
|
|
194
295
|
va_end(argsCopy);
|
|
195
296
|
return len;
|