@geastack/host 0.1.8

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 (67) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +25 -0
  3. package/audio_runtime.cpp +807 -0
  4. package/bluetooth.cpp +125 -0
  5. package/geolocation.cpp +43 -0
  6. package/host/apps.cpp +20 -0
  7. package/host/audio.cpp +240 -0
  8. package/host/ble.cpp +239 -0
  9. package/host/camera.cpp +193 -0
  10. package/host/display.cpp +230 -0
  11. package/host/fetch.cpp +1060 -0
  12. package/host/geolocation.cpp +31 -0
  13. package/host/gpio.cpp +55 -0
  14. package/host/http.cpp +349 -0
  15. package/host/image.cpp +335 -0
  16. package/host/imu.cpp +51 -0
  17. package/host/input.cpp +12 -0
  18. package/host/media.cpp +579 -0
  19. package/host/memory.cpp +91 -0
  20. package/host/rtc.cpp +212 -0
  21. package/host/rtc_esp.cpp +291 -0
  22. package/host/tile_loader.cpp +478 -0
  23. package/host/timers.cpp +327 -0
  24. package/host/touch.cpp +25 -0
  25. package/host/websocket.cpp +279 -0
  26. package/host/wifi.cpp +99 -0
  27. package/include/gea/embedded-host.h +38 -0
  28. package/include/host/apps.h +15 -0
  29. package/include/host/audio.h +158 -0
  30. package/include/host/backends.h +455 -0
  31. package/include/host/battery.h +25 -0
  32. package/include/host/camera.h +236 -0
  33. package/include/host/clock.h +45 -0
  34. package/include/host/device_control.h +23 -0
  35. package/include/host/display.h +266 -0
  36. package/include/host/display_orientation.h +291 -0
  37. package/include/host/fetch.h +310 -0
  38. package/include/host/gpio.h +36 -0
  39. package/include/host/http.h +161 -0
  40. package/include/host/image.h +144 -0
  41. package/include/host/input.h +14 -0
  42. package/include/host/media.h +165 -0
  43. package/include/host/memory.h +44 -0
  44. package/include/host/navigator.h +283 -0
  45. package/include/host/notify.h +41 -0
  46. package/include/host/profiler.h +49 -0
  47. package/include/host/rtc.h +174 -0
  48. package/include/host/storage.h +141 -0
  49. package/include/host/tile_loader.h +59 -0
  50. package/include/host/timers.h +41 -0
  51. package/include/host/touch.h +20 -0
  52. package/include/host/websocket.h +179 -0
  53. package/include/host/window.h +31 -0
  54. package/input.cpp +68 -0
  55. package/package.json +27 -0
  56. package/services/app_state.cpp +19 -0
  57. package/services/battery_service.cpp +19 -0
  58. package/services/bluetooth_service.cpp +37 -0
  59. package/services/diagnostics.cpp +233 -0
  60. package/services/diagnostics_connection.cpp +231 -0
  61. package/services/diagnostics_internal.h +15 -0
  62. package/services/diagnostics_server.cpp +135 -0
  63. package/services/mirror.cpp +364 -0
  64. package/services/network_services.cpp +62 -0
  65. package/services/runtime_log.cpp +23 -0
  66. package/services/settings_surface.h +221 -0
  67. package/wifi.cpp +185 -0
package/host/rtc.cpp ADDED
@@ -0,0 +1,212 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #include "host/rtc.h"
3
+
4
+ #include <atomic>
5
+ #include <cstdint>
6
+ #include <mutex>
7
+ #include <string>
8
+ #include <unordered_map>
9
+ #include <utility>
10
+ #include <vector>
11
+
12
+ namespace gea::host {
13
+
14
+ namespace {
15
+
16
+ struct PeerState {
17
+ std::string connectionState = "new";
18
+ std::string iceConnectionState = "new";
19
+ bool open = true;
20
+ };
21
+
22
+ std::unordered_map<NativeRtcPeerHandle, PeerState> &peerTable() {
23
+ static std::unordered_map<NativeRtcPeerHandle, PeerState> table;
24
+ return table;
25
+ }
26
+
27
+ std::mutex &peerMutex() {
28
+ static std::mutex m;
29
+ return m;
30
+ }
31
+
32
+ std::atomic<NativeRtcPeerHandle> &nextHandle() {
33
+ static std::atomic<NativeRtcPeerHandle> id{1};
34
+ return id;
35
+ }
36
+
37
+ enum class EventKind { IceCandidate, Track, ConnectionStateChange, IceConnectionStateChange };
38
+
39
+ struct PendingEvent {
40
+ NativeRtcPeerHandle handle = 0;
41
+ EventKind kind = EventKind::ConnectionStateChange;
42
+ std::string candidate;
43
+ std::string sdp_mid;
44
+ int sdp_mline_index = 0;
45
+ NativeMediaTrackHandle track = 0;
46
+ };
47
+
48
+ std::mutex &queueMutex() {
49
+ static std::mutex m;
50
+ return m;
51
+ }
52
+
53
+ std::vector<PendingEvent> &eventQueue() {
54
+ static std::vector<PendingEvent> q;
55
+ return q;
56
+ }
57
+
58
+ void enqueue_internal(PendingEvent ev) {
59
+ std::lock_guard<std::mutex> lock(queueMutex());
60
+ eventQueue().push_back(std::move(ev));
61
+ }
62
+
63
+ } // namespace
64
+
65
+ namespace rtc {
66
+
67
+ std::unordered_map<NativeRtcPeerHandle, CallbackTable> &callbackTable() {
68
+ static std::unordered_map<NativeRtcPeerHandle, CallbackTable> table;
69
+ return table;
70
+ }
71
+
72
+ #if defined(ESP_PLATFORM) && !defined(GEA_EMBEDDED_WIFI_DISABLED)
73
+ NativeRtcPeerHandle platform_create(const std::string &);
74
+ void platform_destroy(NativeRtcPeerHandle);
75
+ void platform_add_track(NativeRtcPeerHandle, NativeMediaTrackHandle);
76
+ std::string platform_create_offer(NativeRtcPeerHandle);
77
+ std::string platform_create_answer(NativeRtcPeerHandle);
78
+ void platform_set_local_description(NativeRtcPeerHandle, const std::string &, const std::string &);
79
+ void platform_set_remote_description(NativeRtcPeerHandle, const std::string &, const std::string &);
80
+ void platform_add_ice_candidate(NativeRtcPeerHandle, const std::string &, const std::string &, int);
81
+ void platform_close(NativeRtcPeerHandle);
82
+ #else
83
+ __attribute__((weak)) NativeRtcPeerHandle platform_create(const std::string &) { return 0; }
84
+ __attribute__((weak)) void platform_destroy(NativeRtcPeerHandle) {}
85
+ __attribute__((weak)) void platform_add_track(NativeRtcPeerHandle, NativeMediaTrackHandle) {}
86
+ __attribute__((weak)) std::string platform_create_offer(NativeRtcPeerHandle) { return "v=0\r\no=- 0 0 IN IP4 0.0.0.0\r\n"; }
87
+ __attribute__((weak)) std::string platform_create_answer(NativeRtcPeerHandle) { return "v=0\r\no=- 0 0 IN IP4 0.0.0.0\r\n"; }
88
+ __attribute__((weak)) void platform_set_local_description(NativeRtcPeerHandle, const std::string &, const std::string &) {}
89
+ __attribute__((weak)) void platform_set_remote_description(NativeRtcPeerHandle, const std::string &, const std::string &) {}
90
+ __attribute__((weak)) void platform_add_ice_candidate(NativeRtcPeerHandle, const std::string &, const std::string &, int) {}
91
+ __attribute__((weak)) void platform_close(NativeRtcPeerHandle) {}
92
+ #endif
93
+
94
+ NativeRtcPeerHandle create_handle() {
95
+ return create_handle_with_config(std::string());
96
+ }
97
+
98
+ NativeRtcPeerHandle create_handle_with_config(const std::string &ice_servers_json) {
99
+ auto handle = nextHandle()++;
100
+ {
101
+ std::lock_guard<std::mutex> lock(peerMutex());
102
+ peerTable()[handle] = PeerState{};
103
+ }
104
+ platform_create(ice_servers_json);
105
+ return handle;
106
+ }
107
+
108
+ void destroy_handle(NativeRtcPeerHandle handle) {
109
+ platform_destroy(handle);
110
+ std::lock_guard<std::mutex> lock(peerMutex());
111
+ peerTable().erase(handle);
112
+ callbackTable().erase(handle);
113
+ }
114
+
115
+ void runCallbacks() {
116
+ std::vector<PendingEvent> drained;
117
+ {
118
+ std::lock_guard<std::mutex> lock(queueMutex());
119
+ drained.swap(eventQueue());
120
+ }
121
+ for (const auto &ev : drained) {
122
+ auto it = callbackTable().find(ev.handle);
123
+ if (it == callbackTable().end()) continue;
124
+ switch (ev.kind) {
125
+ case EventKind::IceCandidate:
126
+ if (it->second.on_ice_candidate) it->second.on_ice_candidate(ev.candidate, ev.sdp_mid, ev.sdp_mline_index);
127
+ break;
128
+ case EventKind::Track:
129
+ if (it->second.on_track) it->second.on_track(ev.track);
130
+ break;
131
+ case EventKind::ConnectionStateChange:
132
+ if (it->second.on_connection_state_change) it->second.on_connection_state_change();
133
+ break;
134
+ case EventKind::IceConnectionStateChange:
135
+ if (it->second.on_ice_connection_state_change) it->second.on_ice_connection_state_change();
136
+ break;
137
+ }
138
+ }
139
+ }
140
+
141
+ void enqueue_ice_candidate(NativeRtcPeerHandle h, const std::string &candidate, const std::string &sdp_mid, int idx) {
142
+ enqueue_internal(PendingEvent{h, EventKind::IceCandidate, candidate, sdp_mid, idx, 0});
143
+ }
144
+ void enqueue_track(NativeRtcPeerHandle h, NativeMediaTrackHandle t) {
145
+ enqueue_internal(PendingEvent{h, EventKind::Track, {}, {}, 0, t});
146
+ }
147
+ void enqueue_connection_state_change(NativeRtcPeerHandle h, const std::string &new_state) {
148
+ {
149
+ std::lock_guard<std::mutex> lock(peerMutex());
150
+ auto it = peerTable().find(h);
151
+ if (it != peerTable().end()) it->second.connectionState = new_state;
152
+ }
153
+ enqueue_internal(PendingEvent{h, EventKind::ConnectionStateChange, {}, {}, 0, 0});
154
+ }
155
+ void enqueue_ice_connection_state_change(NativeRtcPeerHandle h, const std::string &new_state) {
156
+ {
157
+ std::lock_guard<std::mutex> lock(peerMutex());
158
+ auto it = peerTable().find(h);
159
+ if (it != peerTable().end()) it->second.iceConnectionState = new_state;
160
+ }
161
+ enqueue_internal(PendingEvent{h, EventKind::IceConnectionStateChange, {}, {}, 0, 0});
162
+ }
163
+
164
+ } // namespace rtc
165
+
166
+ std::string RTCPeerConnection::connectionState() const {
167
+ std::lock_guard<std::mutex> lock(peerMutex());
168
+ auto it = peerTable().find(nativeHandle);
169
+ return it == peerTable().end() ? std::string("closed") : it->second.connectionState;
170
+ }
171
+
172
+ std::string RTCPeerConnection::iceConnectionState() const {
173
+ std::lock_guard<std::mutex> lock(peerMutex());
174
+ auto it = peerTable().find(nativeHandle);
175
+ return it == peerTable().end() ? std::string("closed") : it->second.iceConnectionState;
176
+ }
177
+
178
+ void RTCPeerConnection::addTrack(MediaStreamTrack track, MediaStream) const {
179
+ rtc::platform_add_track(nativeHandle, track.nativeHandle);
180
+ }
181
+
182
+ std::string RTCPeerConnection::createOffer() const {
183
+ return rtc::platform_create_offer(nativeHandle);
184
+ }
185
+
186
+ std::string RTCPeerConnection::createAnswer() const {
187
+ return rtc::platform_create_answer(nativeHandle);
188
+ }
189
+
190
+ void RTCPeerConnection::setLocalDescription(const std::string &sdp_type, const std::string &sdp) const {
191
+ rtc::platform_set_local_description(nativeHandle, sdp_type, sdp);
192
+ }
193
+
194
+ void RTCPeerConnection::setRemoteDescription(const std::string &sdp_type, const std::string &sdp) const {
195
+ rtc::platform_set_remote_description(nativeHandle, sdp_type, sdp);
196
+ }
197
+
198
+ void RTCPeerConnection::addIceCandidate(const std::string &candidate, const std::string &sdp_mid, double sdp_mline_index) const {
199
+ rtc::platform_add_ice_candidate(nativeHandle, candidate, sdp_mid, static_cast<int>(sdp_mline_index));
200
+ }
201
+
202
+ void RTCPeerConnection::close() const {
203
+ rtc::platform_close(nativeHandle);
204
+ std::lock_guard<std::mutex> lock(peerMutex());
205
+ auto it = peerTable().find(nativeHandle);
206
+ if (it != peerTable().end()) {
207
+ it->second.connectionState = "closed";
208
+ it->second.iceConnectionState = "closed";
209
+ }
210
+ }
211
+
212
+ } // namespace gea::host
@@ -0,0 +1,291 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #ifdef ESP_PLATFORM
3
+
4
+ #include "host/rtc.h"
5
+ #include "host/media.h"
6
+
7
+ #include "esp_log.h"
8
+ #include "esp_peer.h"
9
+ #include "esp_peer_default.h"
10
+ #include "freertos/FreeRTOS.h"
11
+ #include "freertos/semphr.h"
12
+ #include "freertos/task.h"
13
+
14
+ #include <atomic>
15
+ #include <cstdint>
16
+ #include <cstring>
17
+ #include <mutex>
18
+ #include <string>
19
+ #include <unordered_map>
20
+ #include <vector>
21
+
22
+ namespace gea::host::rtc {
23
+
24
+ namespace {
25
+
26
+ constexpr const char *kTag = "gea::host::rtc";
27
+ constexpr int kAudioSampleRate = 16000;
28
+ constexpr int kAudioChannels = 1;
29
+ constexpr std::size_t kSendFrameSamples = 320; // 20 ms @ 16 kHz mono
30
+
31
+ struct EspPeerState {
32
+ esp_peer_handle_t handle = nullptr;
33
+ NativeRtcPeerHandle our_handle = 0;
34
+ TaskHandle_t main_loop_task = nullptr;
35
+ TaskHandle_t audio_send_task = nullptr;
36
+
37
+ // create_offer blocks on this until on_msg fires with SDP.
38
+ SemaphoreHandle_t sdp_ready = nullptr;
39
+ std::string pending_sdp;
40
+
41
+ NativeMediaTrackHandle local_track = 0;
42
+ NativeMediaTrackHandle remote_track = 0;
43
+
44
+ bool open = false;
45
+ };
46
+
47
+ std::unordered_map<NativeRtcPeerHandle, EspPeerState *> &espPeerTable() {
48
+ static std::unordered_map<NativeRtcPeerHandle, EspPeerState *> table;
49
+ return table;
50
+ }
51
+
52
+ std::mutex &espPeerMutex() {
53
+ static std::mutex m;
54
+ return m;
55
+ }
56
+
57
+ EspPeerState *findPeer(NativeRtcPeerHandle handle) {
58
+ std::lock_guard<std::mutex> lock(espPeerMutex());
59
+ auto it = espPeerTable().find(handle);
60
+ return it == espPeerTable().end() ? nullptr : it->second;
61
+ }
62
+
63
+ // Callbacks fire on esp_peer's main-loop task — we marshal events onto our
64
+ // JS-side frame-loop pump via the existing rtc::enqueue_* hooks.
65
+
66
+ int on_state_cb(esp_peer_state_t state, void *ctx) {
67
+ auto *peer_state = static_cast<EspPeerState *>(ctx);
68
+ const char *name = "new";
69
+ switch (state) {
70
+ case ESP_PEER_STATE_CLOSED: name = "closed"; break;
71
+ case ESP_PEER_STATE_DISCONNECTED: name = "disconnected"; break;
72
+ case ESP_PEER_STATE_NEW_CONNECTION:
73
+ case ESP_PEER_STATE_CANDIDATE_GATHERING:
74
+ case ESP_PEER_STATE_PAIRING:
75
+ case ESP_PEER_STATE_PAIRED:
76
+ case ESP_PEER_STATE_CONNECTING:
77
+ name = "connecting";
78
+ break;
79
+ case ESP_PEER_STATE_CONNECTED: name = "connected"; break;
80
+ case ESP_PEER_STATE_CONNECT_FAILED: name = "failed"; break;
81
+ default: name = "new"; break;
82
+ }
83
+ enqueue_connection_state_change(peer_state->our_handle, std::string(name));
84
+ enqueue_ice_connection_state_change(peer_state->our_handle, std::string(name));
85
+ return 0;
86
+ }
87
+
88
+ int on_msg_cb(esp_peer_msg_t *info, void *ctx) {
89
+ auto *peer_state = static_cast<EspPeerState *>(ctx);
90
+ if (!info || !info->data || info->size <= 0) return 0;
91
+ std::string payload(reinterpret_cast<const char *>(info->data), static_cast<std::size_t>(info->size));
92
+ switch (info->type) {
93
+ case ESP_PEER_MSG_TYPE_SDP:
94
+ peer_state->pending_sdp = payload;
95
+ if (peer_state->sdp_ready) xSemaphoreGive(peer_state->sdp_ready);
96
+ break;
97
+ case ESP_PEER_MSG_TYPE_CANDIDATE:
98
+ enqueue_ice_candidate(peer_state->our_handle, payload, std::string("0"), 0);
99
+ break;
100
+ default: break;
101
+ }
102
+ return 0;
103
+ }
104
+
105
+ int on_audio_data_cb(esp_peer_audio_frame_t *frame, void *ctx) {
106
+ auto *peer_state = static_cast<EspPeerState *>(ctx);
107
+ if (!frame || !frame->data || frame->size <= 0) return 0;
108
+ if (!peer_state->remote_track) {
109
+ // Lazily create a remote track stream + track so JS can attach playback.
110
+ auto stream_handle = gea::host::media::create_stream();
111
+ peer_state->remote_track = gea::host::media::stream_audio_track(stream_handle);
112
+ enqueue_track(peer_state->our_handle, peer_state->remote_track);
113
+ }
114
+ // Frame data is decoded PCM int16; track_inject_pcm handles the ring buffer.
115
+ gea::host::media::track_inject_pcm(
116
+ peer_state->remote_track,
117
+ reinterpret_cast<const std::int16_t *>(frame->data),
118
+ static_cast<std::size_t>(frame->size) / sizeof(std::int16_t));
119
+ return 0;
120
+ }
121
+
122
+ void main_loop_trampoline(void *arg) {
123
+ auto *peer_state = static_cast<EspPeerState *>(arg);
124
+ while (peer_state->open) {
125
+ esp_peer_main_loop(peer_state->handle);
126
+ vTaskDelay(pdMS_TO_TICKS(1));
127
+ }
128
+ vTaskDelete(nullptr);
129
+ }
130
+
131
+ void audio_send_trampoline(void *arg) {
132
+ auto *peer_state = static_cast<EspPeerState *>(arg);
133
+ std::int16_t buffer[kSendFrameSamples];
134
+ while (peer_state->open) {
135
+ if (peer_state->local_track == 0) {
136
+ vTaskDelay(pdMS_TO_TICKS(20));
137
+ continue;
138
+ }
139
+ std::size_t count = gea::host::media::track_read_pcm(
140
+ peer_state->local_track, buffer, kSendFrameSamples);
141
+ if (count == 0) {
142
+ vTaskDelay(pdMS_TO_TICKS(5));
143
+ continue;
144
+ }
145
+ esp_peer_audio_frame_t frame = {};
146
+ frame.pts = 0;
147
+ frame.data = reinterpret_cast<uint8_t *>(buffer);
148
+ frame.size = static_cast<int>(count * sizeof(std::int16_t));
149
+ esp_peer_send_audio(peer_state->handle, &frame);
150
+ }
151
+ vTaskDelete(nullptr);
152
+ }
153
+
154
+ EspPeerState *ensurePeer(NativeRtcPeerHandle our_handle) {
155
+ std::lock_guard<std::mutex> lock(espPeerMutex());
156
+ auto it = espPeerTable().find(our_handle);
157
+ if (it != espPeerTable().end()) return it->second;
158
+
159
+ auto *state = new EspPeerState{};
160
+ state->our_handle = our_handle;
161
+ state->sdp_ready = xSemaphoreCreateBinary();
162
+ state->open = true;
163
+
164
+ static esp_peer_ice_server_cfg_t default_stun = {};
165
+ default_stun.stun_url = const_cast<char *>("stun:stun.l.google.com:19302");
166
+
167
+ esp_peer_cfg_t cfg = {};
168
+ cfg.server_lists = &default_stun;
169
+ cfg.server_num = 1;
170
+ cfg.role = ESP_PEER_ROLE_CONTROLLING;
171
+ cfg.ice_trans_policy = ESP_PEER_ICE_TRANS_POLICY_ALL;
172
+ cfg.audio_info.codec = ESP_PEER_AUDIO_CODEC_OPUS;
173
+ cfg.audio_info.sample_rate = kAudioSampleRate;
174
+ cfg.audio_info.channel = kAudioChannels;
175
+ cfg.audio_dir = ESP_PEER_MEDIA_DIR_SEND_RECV;
176
+ cfg.video_dir = ESP_PEER_MEDIA_DIR_NONE;
177
+ cfg.enable_data_channel = false;
178
+ cfg.ctx = state;
179
+ cfg.on_state = on_state_cb;
180
+ cfg.on_msg = on_msg_cb;
181
+ cfg.on_audio_data = on_audio_data_cb;
182
+
183
+ int err = esp_peer_open(&cfg, esp_peer_get_default_impl(), &state->handle);
184
+ if (err != 0 || !state->handle) {
185
+ ESP_LOGE(kTag, "esp_peer_open failed: %d", err);
186
+ if (state->sdp_ready) vSemaphoreDelete(state->sdp_ready);
187
+ delete state;
188
+ return nullptr;
189
+ }
190
+
191
+ xTaskCreatePinnedToCore(main_loop_trampoline, "gea_rtc_loop", 8192, state, 7, &state->main_loop_task, 1);
192
+ xTaskCreatePinnedToCore(audio_send_trampoline, "gea_rtc_send", 4096, state, 5, &state->audio_send_task, 1);
193
+
194
+ espPeerTable()[our_handle] = state;
195
+ return state;
196
+ }
197
+
198
+ void destroyPeerLocked(EspPeerState *state) {
199
+ state->open = false;
200
+ if (state->handle) {
201
+ esp_peer_close(state->handle);
202
+ state->handle = nullptr;
203
+ }
204
+ if (state->sdp_ready) {
205
+ vSemaphoreDelete(state->sdp_ready);
206
+ state->sdp_ready = nullptr;
207
+ }
208
+ // Tasks will exit on the next loop iteration once state->open is false.
209
+ delete state;
210
+ }
211
+
212
+ } // namespace
213
+
214
+ NativeRtcPeerHandle platform_create(const std::string &) {
215
+ return 0;
216
+ }
217
+
218
+ void platform_destroy(NativeRtcPeerHandle our_handle) {
219
+ std::lock_guard<std::mutex> lock(espPeerMutex());
220
+ auto it = espPeerTable().find(our_handle);
221
+ if (it == espPeerTable().end()) return;
222
+ auto *state = it->second;
223
+ espPeerTable().erase(it);
224
+ destroyPeerLocked(state);
225
+ }
226
+
227
+ void platform_add_track(NativeRtcPeerHandle our_handle, NativeMediaTrackHandle track) {
228
+ auto *state = ensurePeer(our_handle);
229
+ if (state) state->local_track = track;
230
+ }
231
+
232
+ std::string platform_create_offer(NativeRtcPeerHandle our_handle) {
233
+ auto *state = ensurePeer(our_handle);
234
+ if (!state) return std::string();
235
+ int err = esp_peer_new_connection(state->handle);
236
+ if (err != 0) {
237
+ ESP_LOGE(kTag, "esp_peer_new_connection failed: %d", err);
238
+ return std::string();
239
+ }
240
+ // Block up to 5 s for on_msg(SDP) to fire. Caller (JS code under await) is on a
241
+ // different task than the main_loop_task running the callback.
242
+ if (xSemaphoreTake(state->sdp_ready, pdMS_TO_TICKS(5000)) != pdTRUE) {
243
+ ESP_LOGE(kTag, "create_offer SDP timeout");
244
+ return std::string();
245
+ }
246
+ return state->pending_sdp;
247
+ }
248
+
249
+ std::string platform_create_answer(NativeRtcPeerHandle our_handle) {
250
+ // esp_peer auto-generates the answer on first inbound offer through send_msg.
251
+ // Block until on_msg fires with the locally-generated SDP.
252
+ auto *state = ensurePeer(our_handle);
253
+ if (!state) return std::string();
254
+ if (xSemaphoreTake(state->sdp_ready, pdMS_TO_TICKS(5000)) != pdTRUE) {
255
+ return std::string();
256
+ }
257
+ return state->pending_sdp;
258
+ }
259
+
260
+ void platform_set_local_description(NativeRtcPeerHandle, const std::string &, const std::string &) {
261
+ // esp_peer treats the SDP returned from on_msg as already the local description.
262
+ // No explicit set is required.
263
+ }
264
+
265
+ void platform_set_remote_description(NativeRtcPeerHandle our_handle, const std::string &, const std::string &sdp) {
266
+ auto *state = findPeer(our_handle);
267
+ if (!state || !state->handle) return;
268
+ esp_peer_msg_t msg = {};
269
+ msg.type = ESP_PEER_MSG_TYPE_SDP;
270
+ msg.data = reinterpret_cast<uint8_t *>(const_cast<char *>(sdp.c_str()));
271
+ msg.size = static_cast<int>(sdp.size());
272
+ esp_peer_send_msg(state->handle, &msg);
273
+ }
274
+
275
+ void platform_add_ice_candidate(NativeRtcPeerHandle our_handle, const std::string &candidate, const std::string &, int) {
276
+ auto *state = findPeer(our_handle);
277
+ if (!state || !state->handle) return;
278
+ esp_peer_msg_t msg = {};
279
+ msg.type = ESP_PEER_MSG_TYPE_CANDIDATE;
280
+ msg.data = reinterpret_cast<uint8_t *>(const_cast<char *>(candidate.c_str()));
281
+ msg.size = static_cast<int>(candidate.size());
282
+ esp_peer_send_msg(state->handle, &msg);
283
+ }
284
+
285
+ void platform_close(NativeRtcPeerHandle our_handle) {
286
+ platform_destroy(our_handle);
287
+ }
288
+
289
+ } // namespace gea::host::rtc
290
+
291
+ #endif // ESP_PLATFORM