@geastack/host 0.1.9 → 0.1.11

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.
Files changed (48) hide show
  1. package/audio_runtime.cpp +97 -3
  2. package/host/audio.cpp +395 -10
  3. package/host/audio_worklet.cpp +611 -0
  4. package/host/fetch.cpp +17 -0
  5. package/host/media.cpp +308 -27
  6. package/host/mjpeg.cpp +199 -0
  7. package/host/rtc.cpp +458 -48
  8. package/host/rtc_esp.cpp +897 -75
  9. package/host/rtc_objects.inc +246 -0
  10. package/host/timers.cpp +18 -0
  11. package/host/video.cpp +152 -0
  12. package/host/websocket.cpp +623 -72
  13. package/host/worker.cpp +621 -0
  14. package/include/gea/audio-worklet-runtime.h +165 -0
  15. package/include/gea/base64-runtime.h +26 -0
  16. package/include/gea/embedded-host.h +1 -0
  17. package/include/gea/worker-runtime.h +184 -0
  18. package/include/host/audio.h +94 -6
  19. package/include/host/audio_level.h +37 -0
  20. package/include/host/audio_worklet.h +103 -0
  21. package/include/host/display.h +1 -1
  22. package/include/host/h264_layout.h +170 -0
  23. package/include/host/media.h +67 -0
  24. package/include/host/mjpeg.h +22 -0
  25. package/include/host/mjpeg_parser.h +84 -0
  26. package/include/host/pcm_stream.h +122 -0
  27. package/include/host/rtc.h +38 -20
  28. package/include/host/rtc_h264.h +76 -0
  29. package/include/host/rtc_ice_candidate.h +141 -0
  30. package/include/host/rtc_opus.h +136 -0
  31. package/include/host/rtc_receive_sdp.h +245 -0
  32. package/include/host/rtc_rtp.h +170 -0
  33. package/include/host/rtc_sdp.h +71 -0
  34. package/include/host/rtc_types.h +166 -0
  35. package/include/host/rtc_vp8.h +115 -0
  36. package/include/host/rtc_vp8_capture.h +129 -0
  37. package/include/host/rtc_vp8_decoder.h +169 -0
  38. package/include/host/rtc_vp8_receiver.h +55 -0
  39. package/include/host/timers.h +3 -2
  40. package/include/host/video.h +39 -0
  41. package/include/host/video_color.h +90 -0
  42. package/include/host/video_frame_pool.h +50 -0
  43. package/include/host/video_pixels.h +178 -0
  44. package/include/host/websocket.h +20 -1
  45. package/include/host/websocket_message.h +66 -0
  46. package/include/host/worker.h +131 -0
  47. package/package.json +1 -1
  48. package/services/diagnostics.cpp +120 -19
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geastack/host",
3
- "version": "0.1.9",
3
+ "version": "0.1.11",
4
4
  "description": "GeaStack host bindings and platform service sources.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -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; continuing without ring replay\n", stdout);
43
- return true;
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
- for (std::size_t i = 0; i < len; i++) {
58
- data_[head_] = data[i];
59
- head_ = (head_ + 1) % kLogRingSize;
60
- if (used_ < kLogRingSize) used_++;
61
- total_++;
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
- for (std::size_t i = 0; i < count; i++) {
90
- std::size_t absolute = *cursor + i;
91
- dst[i] = data_[absolute % kLogRingSize];
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
- return diagnostics_internal::beginLogRing();
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
- std::fwrite(stackBuf, 1, static_cast<std::size_t>(len), stdout);
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
- std::fwrite(heapBuf, 1, static_cast<std::size_t>(len), stdout);
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
- std::fwrite(stackBuf, 1, sizeof(stackBuf) - 1, stdout);
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;