@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,246 @@
|
|
|
1
|
+
// Included by rtc.cpp: one state registry and one event queue for every RTC handle.
|
|
2
|
+
RTCConfiguration RTCPeerConnection::getConfiguration() const { return rtc::configuration(nativeHandle); }
|
|
3
|
+
std::string RTCPeerConnection::signalingState() const {
|
|
4
|
+
std::lock_guard<std::mutex> lock(peerMutex());
|
|
5
|
+
auto it = peerTable().find(nativeHandle);
|
|
6
|
+
return it == peerTable().end() ? "closed" : it->second.signaling;
|
|
7
|
+
}
|
|
8
|
+
std::optional<RTCSessionDescriptionObject> RTCPeerConnection::localDescription() const {
|
|
9
|
+
std::lock_guard<std::mutex> lock(peerMutex()); return requirePeer(nativeHandle).local;
|
|
10
|
+
}
|
|
11
|
+
std::optional<RTCSessionDescriptionObject> RTCPeerConnection::remoteDescription() const {
|
|
12
|
+
std::lock_guard<std::mutex> lock(peerMutex()); return requirePeer(nativeHandle).remote;
|
|
13
|
+
}
|
|
14
|
+
namespace {
|
|
15
|
+
RTCRtpTransceiver addMediaTransceiver(uint32_t nativeHandle, const std::string& kind,
|
|
16
|
+
MediaStreamTrack track, const RTCRtpTransceiverInit& init) {
|
|
17
|
+
requireDirection(init.direction);
|
|
18
|
+
if (kind != "audio" && kind != "video") throw std::invalid_argument("TypeError: invalid transceiver kind");
|
|
19
|
+
if (kind == "video" && (track.nativeHandle || init.direction == "sendonly" || init.direction == "sendrecv"))
|
|
20
|
+
throw std::invalid_argument("NotSupportedError: native video transmission is not implemented");
|
|
21
|
+
{ std::lock_guard<std::mutex> lock(peerMutex());
|
|
22
|
+
auto& peer = requirePeer(nativeHandle);
|
|
23
|
+
for (auto id : peer.transceivers) if (!transceiverTable().at(id).stopped && transceiverTable().at(id).kind == kind)
|
|
24
|
+
throw std::runtime_error("NotSupportedError: ESP peer supports one transceiver of each media kind");
|
|
25
|
+
}
|
|
26
|
+
rtc::platform_direction(nativeHandle, kind, init.direction);
|
|
27
|
+
if (track.nativeHandle) rtc::platform_add_track(nativeHandle, track.nativeHandle);
|
|
28
|
+
uint32_t id = nextObject++;
|
|
29
|
+
{ std::lock_guard<std::mutex> lock(peerMutex());
|
|
30
|
+
transceiverTable()[id] = TransceiverState{nativeHandle, track.nativeHandle, 0, init.direction};
|
|
31
|
+
transceiverTable()[id].kind = kind;
|
|
32
|
+
registerObjectLifetime(id, nativeHandle);
|
|
33
|
+
requirePeer(nativeHandle).transceivers.push_back(id);
|
|
34
|
+
}
|
|
35
|
+
enqueue_internal(PendingEvent{nativeHandle, EventKind::Negotiation});
|
|
36
|
+
return RTCRtpTransceiver(id);
|
|
37
|
+
}
|
|
38
|
+
} // namespace
|
|
39
|
+
RTCRtpTransceiver RTCPeerConnection::addTransceiver(const std::string& kind, const RTCRtpTransceiverInit& init) const {
|
|
40
|
+
return addMediaTransceiver(nativeHandle, kind, MediaStreamTrack{}, init);
|
|
41
|
+
}
|
|
42
|
+
RTCRtpTransceiver RTCPeerConnection::addTransceiver(MediaStreamTrack track, const RTCRtpTransceiverInit& init) const {
|
|
43
|
+
return addMediaTransceiver(nativeHandle, track.nativeHandle ? track.kind() : "audio", track, init);
|
|
44
|
+
}
|
|
45
|
+
std::vector<RTCRtpTransceiver> RTCPeerConnection::getTransceivers() const {
|
|
46
|
+
std::lock_guard<std::mutex> lock(peerMutex());
|
|
47
|
+
std::vector<RTCRtpTransceiver> result;
|
|
48
|
+
auto it = peerTable().find(nativeHandle);
|
|
49
|
+
if (it != peerTable().end()) for (auto id : it->second.transceivers) result.emplace_back(id);
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
std::vector<RTCRtpSender> RTCPeerConnection::getSenders() const {
|
|
53
|
+
std::vector<RTCRtpSender> result;
|
|
54
|
+
for (auto tr : getTransceivers()) if (!tr.stopped()) result.push_back(tr.sender());
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
std::vector<RTCRtpReceiver> RTCPeerConnection::getReceivers() const {
|
|
58
|
+
std::vector<RTCRtpReceiver> result;
|
|
59
|
+
for (auto tr : getTransceivers()) if (!tr.stopped()) result.push_back(tr.receiver());
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
void RTCPeerConnection::removeTrack(RTCRtpSender sender) const {
|
|
63
|
+
std::string direction;
|
|
64
|
+
{ std::lock_guard<std::mutex> lock(peerMutex());
|
|
65
|
+
requirePeer(nativeHandle);
|
|
66
|
+
auto& tr = requireTransceiver(sender.nativeHandle);
|
|
67
|
+
if (tr.peer != nativeHandle) throw std::invalid_argument("InvalidAccessError: sender belongs to another peer");
|
|
68
|
+
if (!tr.local || tr.stopped) return;
|
|
69
|
+
direction = tr.direction == "sendrecv" ? "recvonly" : tr.direction == "sendonly" ? "inactive" : tr.direction;
|
|
70
|
+
}
|
|
71
|
+
RTCRtpTransceiver(sender.nativeHandle).setDirection(direction);
|
|
72
|
+
sender.replaceTrack();
|
|
73
|
+
}
|
|
74
|
+
std::optional<MediaStreamTrack> RTCRtpSender::track() const {
|
|
75
|
+
std::lock_guard<std::mutex> lock(peerMutex());
|
|
76
|
+
auto it = transceiverTable().find(nativeHandle);
|
|
77
|
+
if (it == transceiverTable().end() || !it->second.local) return std::nullopt;
|
|
78
|
+
return MediaStreamTrack(it->second.local);
|
|
79
|
+
}
|
|
80
|
+
void RTCRtpSender::replaceTrack() const { replaceTrack(MediaStreamTrack{}); }
|
|
81
|
+
void RTCRtpSender::replaceTrack(MediaStreamTrack track) const {
|
|
82
|
+
if (track.nativeHandle && track.kind() != "audio") throw std::invalid_argument("TypeError: replacement track kind differs");
|
|
83
|
+
uint32_t peer;
|
|
84
|
+
std::string kind;
|
|
85
|
+
{ std::lock_guard<std::mutex> lock(peerMutex()); auto& tr = requireTransceiver(nativeHandle);
|
|
86
|
+
if (tr.stopped) throw std::runtime_error("InvalidStateError: transceiver stopped");
|
|
87
|
+
if (track.nativeHandle && tr.kind != track.kind()) throw std::invalid_argument("TypeError: replacement track kind differs");
|
|
88
|
+
peer = tr.peer; kind = tr.kind; }
|
|
89
|
+
if (kind == "audio") rtc::platform_add_track(peer, track.nativeHandle);
|
|
90
|
+
{ std::lock_guard<std::mutex> lock(peerMutex()); requireTransceiver(nativeHandle).local = track.nativeHandle; }
|
|
91
|
+
}
|
|
92
|
+
std::optional<MediaStreamTrack> RTCRtpReceiver::track() const {
|
|
93
|
+
std::lock_guard<std::mutex> lock(peerMutex());
|
|
94
|
+
auto it = transceiverTable().find(nativeHandle);
|
|
95
|
+
if (it == transceiverTable().end() || !it->second.remote) return std::nullopt;
|
|
96
|
+
return MediaStreamTrack(it->second.remote);
|
|
97
|
+
}
|
|
98
|
+
std::optional<std::string> RTCRtpTransceiver::mid() const {
|
|
99
|
+
std::lock_guard<std::mutex> lock(peerMutex()); auto it = transceiverTable().find(nativeHandle);
|
|
100
|
+
return it == transceiverTable().end() ? std::nullopt : it->second.mid;
|
|
101
|
+
}
|
|
102
|
+
std::optional<std::string> RTCRtpTransceiver::currentDirection() const {
|
|
103
|
+
std::lock_guard<std::mutex> lock(peerMutex()); auto it = transceiverTable().find(nativeHandle);
|
|
104
|
+
return it == transceiverTable().end() ? std::nullopt : it->second.current;
|
|
105
|
+
}
|
|
106
|
+
std::string RTCRtpTransceiver::direction() const {
|
|
107
|
+
std::lock_guard<std::mutex> lock(peerMutex()); return requireTransceiver(nativeHandle).direction;
|
|
108
|
+
}
|
|
109
|
+
bool RTCRtpTransceiver::stopped() const {
|
|
110
|
+
std::lock_guard<std::mutex> lock(peerMutex()); auto it = transceiverTable().find(nativeHandle);
|
|
111
|
+
return it == transceiverTable().end() || it->second.stopped;
|
|
112
|
+
}
|
|
113
|
+
void RTCRtpTransceiver::setDirection(const std::string& direction) const {
|
|
114
|
+
requireDirection(direction);
|
|
115
|
+
uint32_t peer;
|
|
116
|
+
std::string kind;
|
|
117
|
+
{ std::lock_guard<std::mutex> lock(peerMutex()); auto& tr = requireTransceiver(nativeHandle);
|
|
118
|
+
if (tr.stopped) throw std::runtime_error("InvalidStateError: transceiver stopped");
|
|
119
|
+
if (tr.direction == direction) return;
|
|
120
|
+
peer = tr.peer; kind = tr.kind;
|
|
121
|
+
}
|
|
122
|
+
if (kind == "video" && direction != "recvonly" && direction != "inactive")
|
|
123
|
+
throw std::invalid_argument("NotSupportedError: native video transmission is not implemented");
|
|
124
|
+
rtc::platform_direction(peer, kind, direction);
|
|
125
|
+
{ std::lock_guard<std::mutex> lock(peerMutex()); requireTransceiver(nativeHandle).direction = direction; }
|
|
126
|
+
enqueue_internal(PendingEvent{peer, EventKind::Negotiation});
|
|
127
|
+
}
|
|
128
|
+
void RTCRtpTransceiver::stop() const {
|
|
129
|
+
if (stopped()) return;
|
|
130
|
+
setDirection("inactive");
|
|
131
|
+
sender().replaceTrack();
|
|
132
|
+
std::lock_guard<std::mutex> lock(peerMutex()); auto& tr = requireTransceiver(nativeHandle);
|
|
133
|
+
tr.stopped = true; tr.current.reset();
|
|
134
|
+
}
|
|
135
|
+
namespace rtc {
|
|
136
|
+
DataChannelCallbacks& channel_callbacks(uint32_t channel) { return channelCallbackTable()[channel]; }
|
|
137
|
+
void enqueue_channel_open(uint32_t peer, const std::string& label, uint16_t stream) {
|
|
138
|
+
uint32_t id = 0;
|
|
139
|
+
bool remote = false;
|
|
140
|
+
{ std::lock_guard<std::mutex> lock(peerMutex());
|
|
141
|
+
auto owner = peerTable().find(peer);
|
|
142
|
+
if (owner == peerTable().end() || !owner->second.open) return;
|
|
143
|
+
for (const auto& [key, ch] : channelTable()) if (ch.peer == peer && ch.label == label && ch.state == "connecting") { id = key; break; }
|
|
144
|
+
if (!id) { id = nextObject++; channelTable()[id].peer = peer; channelTable()[id].label = label; remote = true; registerObjectLifetime(id, peer); }
|
|
145
|
+
channelTable()[id].stream = stream; channelTable()[id].state = "open";
|
|
146
|
+
}
|
|
147
|
+
PendingEvent ev{peer, remote ? EventKind::DataChannel : EventKind::ChannelOpen}; ev.channel = id;
|
|
148
|
+
enqueue_internal(std::move(ev));
|
|
149
|
+
}
|
|
150
|
+
void enqueue_channel_close(uint32_t peer, uint16_t stream) {
|
|
151
|
+
std::lock_guard<std::mutex> lock(peerMutex());
|
|
152
|
+
for (auto& [id, ch] : channelTable()) if (ch.peer == peer && ch.stream == stream && ch.state != "closed") {
|
|
153
|
+
ch.state = "closed";
|
|
154
|
+
PendingEvent ev{peer, EventKind::ChannelClose}; ev.channel = id; enqueue_internal(std::move(ev)); break;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
void enqueue_channel_message(uint32_t peer, uint16_t stream, const uint8_t* bytes, size_t size, bool text) {
|
|
158
|
+
if (!bytes && size) return;
|
|
159
|
+
std::lock_guard<std::mutex> lock(peerMutex());
|
|
160
|
+
for (const auto& [id, ch] : channelTable()) if (ch.peer == peer && ch.stream == stream && ch.state == "open") {
|
|
161
|
+
PendingEvent ev{peer, EventKind::ChannelMessage}; ev.channel = id; ev.text = text;
|
|
162
|
+
if (size) ev.bytes.assign(bytes, bytes + size);
|
|
163
|
+
enqueue_internal(std::move(ev)); break;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
RTCDataChannel RTCPeerConnection::createDataChannel(const std::string& label, const RTCDataChannelInit& init) const {
|
|
168
|
+
const auto valid16 = [](const std::optional<double>& value) { return !value || (std::isfinite(*value) && *value >= 0 && *value <= 65535 && std::floor(*value) == *value); };
|
|
169
|
+
if (label.size() > 65535 || init.protocol.size() > 65535 || !valid16(init.maxPacketLifeTime) || !valid16(init.maxRetransmits) || !valid16(init.id))
|
|
170
|
+
throw std::invalid_argument("TypeError: invalid data channel parameters");
|
|
171
|
+
if (init.maxPacketLifeTime && init.maxRetransmits) throw std::invalid_argument("TypeError: choose lifetime or retransmits");
|
|
172
|
+
// esp_peer's DCEP API cannot set these; do not pretend it did.
|
|
173
|
+
if (init.negotiated || init.id || !init.protocol.empty()) throw std::invalid_argument("NotSupportedError: negotiated ID/protocol");
|
|
174
|
+
uint32_t id = nextObject++;
|
|
175
|
+
{ std::lock_guard<std::mutex> lock(peerMutex()); requirePeer(nativeHandle);
|
|
176
|
+
for (const auto& [key, ch] : channelTable()) if (ch.peer == nativeHandle && ch.label == label && ch.state != "closed")
|
|
177
|
+
throw std::invalid_argument("NotSupportedError: duplicate data channel label");
|
|
178
|
+
channelTable()[id].peer = nativeHandle; channelTable()[id].label = label; channelTable()[id].config = init;
|
|
179
|
+
registerObjectLifetime(id, nativeHandle);
|
|
180
|
+
}
|
|
181
|
+
try { rtc::platform_create_channel(nativeHandle, label, init); }
|
|
182
|
+
catch (...) {
|
|
183
|
+
std::lock_guard<std::mutex> lock(peerMutex());
|
|
184
|
+
std::lock_guard<std::mutex> owners(lifetimeMutex());
|
|
185
|
+
channelTable().erase(id); objectLifetimeTable().erase(id); throw;
|
|
186
|
+
}
|
|
187
|
+
return RTCDataChannel(id);
|
|
188
|
+
}
|
|
189
|
+
std::string RTCDataChannel::label() const { std::lock_guard<std::mutex> lock(peerMutex()); return requireChannel(nativeHandle).label; }
|
|
190
|
+
std::string RTCDataChannel::protocol() const { std::lock_guard<std::mutex> lock(peerMutex()); return requireChannel(nativeHandle).config.protocol; }
|
|
191
|
+
bool RTCDataChannel::ordered() const { std::lock_guard<std::mutex> lock(peerMutex()); return requireChannel(nativeHandle).config.ordered; }
|
|
192
|
+
std::string RTCDataChannel::binaryType() const { std::lock_guard<std::mutex> lock(peerMutex()); return requireChannel(nativeHandle).binary_type; }
|
|
193
|
+
void RTCDataChannel::setBinaryType(const std::string& type) const {
|
|
194
|
+
if (type != "arraybuffer") throw std::invalid_argument("NotSupportedError: use arraybuffer data channels");
|
|
195
|
+
std::lock_guard<std::mutex> lock(peerMutex()); requireChannel(nativeHandle).binary_type = type;
|
|
196
|
+
}
|
|
197
|
+
std::string RTCDataChannel::readyState() const {
|
|
198
|
+
std::lock_guard<std::mutex> lock(peerMutex()); auto it = channelTable().find(nativeHandle);
|
|
199
|
+
return it == channelTable().end() ? "closed" : it->second.state;
|
|
200
|
+
}
|
|
201
|
+
std::optional<double> RTCDataChannel::id() const {
|
|
202
|
+
std::lock_guard<std::mutex> lock(peerMutex()); auto& c = requireChannel(nativeHandle);
|
|
203
|
+
return c.stream ? std::optional<double>(*c.stream) : std::nullopt;
|
|
204
|
+
}
|
|
205
|
+
std::optional<double> RTCDataChannel::maxRetransmits() const { std::lock_guard<std::mutex> lock(peerMutex()); return requireChannel(nativeHandle).config.maxRetransmits; }
|
|
206
|
+
std::optional<double> RTCDataChannel::maxPacketLifeTime() const { std::lock_guard<std::mutex> lock(peerMutex()); return requireChannel(nativeHandle).config.maxPacketLifeTime; }
|
|
207
|
+
double RTCDataChannel::bufferedAmount() const { std::lock_guard<std::mutex> lock(peerMutex()); return requireChannel(nativeHandle).buffered; }
|
|
208
|
+
double RTCDataChannel::bufferedAmountLowThreshold() const { std::lock_guard<std::mutex> lock(peerMutex()); return requireChannel(nativeHandle).low_threshold; }
|
|
209
|
+
void RTCDataChannel::setBufferedAmountLowThreshold(double value) const {
|
|
210
|
+
if (!std::isfinite(value) || value < 0 || value > 4294967295.0) throw std::invalid_argument("Invalid bufferedAmountLowThreshold");
|
|
211
|
+
std::lock_guard<std::mutex> lock(peerMutex()); requireChannel(nativeHandle).low_threshold = std::floor(value);
|
|
212
|
+
}
|
|
213
|
+
namespace {
|
|
214
|
+
void sendChannel(uint32_t id, const uint8_t* bytes, size_t size, bool text) {
|
|
215
|
+
uint32_t peer; uint16_t stream;
|
|
216
|
+
{ std::lock_guard<std::mutex> lock(peerMutex()); auto& ch = requireChannel(id);
|
|
217
|
+
requirePeer(ch.peer);
|
|
218
|
+
if (ch.state != "open" || !ch.stream) throw std::runtime_error("InvalidStateError: data channel is not open");
|
|
219
|
+
peer = ch.peer; stream = *ch.stream; ch.buffered += size;
|
|
220
|
+
}
|
|
221
|
+
try { rtc::platform_send_data(peer, stream, bytes, size, text); }
|
|
222
|
+
catch (...) { std::lock_guard<std::mutex> lock(peerMutex()); requireChannel(id).buffered -= size; throw; }
|
|
223
|
+
{ std::lock_guard<std::mutex> lock(peerMutex()); auto& ch = requireChannel(id);
|
|
224
|
+
const bool low = ch.buffered > ch.low_threshold && ch.buffered - size <= ch.low_threshold;
|
|
225
|
+
ch.buffered -= size;
|
|
226
|
+
if (low) { PendingEvent ev{peer, EventKind::ChannelLow}; ev.channel = id; enqueue_internal(std::move(ev)); }
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
void RTCDataChannel::send(const std::string& data) const { sendChannel(nativeHandle, reinterpret_cast<const uint8_t*>(data.data()), data.size(), true); }
|
|
231
|
+
void RTCDataChannel::send(const std::vector<uint8_t>& data) const { sendChannel(nativeHandle, data.data(), data.size(), false); }
|
|
232
|
+
void RTCDataChannel::close() const {
|
|
233
|
+
uint32_t peer; std::string label;
|
|
234
|
+
{ std::lock_guard<std::mutex> lock(peerMutex()); auto& ch = requireChannel(nativeHandle);
|
|
235
|
+
if (ch.state == "closed" || ch.state == "closing") return;
|
|
236
|
+
ch.state = "closing"; peer = ch.peer; label = ch.label;
|
|
237
|
+
}
|
|
238
|
+
try { rtc::platform_close_channel(peer, label); }
|
|
239
|
+
catch (...) { std::lock_guard<std::mutex> lock(peerMutex()); auto& ch = requireChannel(nativeHandle); ch.state = ch.stream ? "open" : "connecting"; throw; }
|
|
240
|
+
std::lock_guard<std::mutex> lock(peerMutex());
|
|
241
|
+
auto& ch = requireChannel(nativeHandle);
|
|
242
|
+
if (!ch.stream && ch.state != "closed") {
|
|
243
|
+
ch.state = "closed";
|
|
244
|
+
PendingEvent ev{peer, EventKind::ChannelClose}; ev.channel = nativeHandle; enqueue_internal(std::move(ev));
|
|
245
|
+
}
|
|
246
|
+
}
|
package/host/timers.cpp
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
2
|
#include "host/timers.h"
|
|
3
|
+
#include "host/worker.h"
|
|
3
4
|
|
|
4
5
|
#include "gea_perf_config.h" // GEA_EMBEDDED_RAF_PERF (+ GEA_EMBEDDED_PERF master)
|
|
5
6
|
|
|
@@ -12,6 +13,7 @@
|
|
|
12
13
|
#include <chrono>
|
|
13
14
|
#endif
|
|
14
15
|
#include <algorithm>
|
|
16
|
+
#include <atomic>
|
|
15
17
|
#include <limits>
|
|
16
18
|
#include <memory>
|
|
17
19
|
#include <mutex>
|
|
@@ -23,6 +25,8 @@ namespace gea::host {
|
|
|
23
25
|
|
|
24
26
|
namespace {
|
|
25
27
|
|
|
28
|
+
std::atomic<const char *> callbackStage{"idle"};
|
|
29
|
+
|
|
26
30
|
std::int64_t nowUs()
|
|
27
31
|
{
|
|
28
32
|
#ifdef ESP_PLATFORM
|
|
@@ -275,21 +279,25 @@ private:
|
|
|
275
279
|
|
|
276
280
|
double setTimeout(TimerCallback callback, double delayMs)
|
|
277
281
|
{
|
|
282
|
+
if (workers::Context::isWorkerCurrent()) return workers::Context::current()->schedule(std::move(callback), delayMs, false);
|
|
278
283
|
return TimerScheduler::instance().schedule(std::move(callback), delayMs, false);
|
|
279
284
|
}
|
|
280
285
|
|
|
281
286
|
double setInterval(TimerCallback callback, double delayMs)
|
|
282
287
|
{
|
|
288
|
+
if (workers::Context::isWorkerCurrent()) return workers::Context::current()->schedule(std::move(callback), delayMs, true);
|
|
283
289
|
return TimerScheduler::instance().schedule(std::move(callback), delayMs, true);
|
|
284
290
|
}
|
|
285
291
|
|
|
286
292
|
void clearTimeout(double id)
|
|
287
293
|
{
|
|
294
|
+
if (workers::Context::isWorkerCurrent()) { workers::Context::current()->cancel(id); return; }
|
|
288
295
|
TimerScheduler::instance().clear(id);
|
|
289
296
|
}
|
|
290
297
|
|
|
291
298
|
void clearInterval(double id)
|
|
292
299
|
{
|
|
300
|
+
if (workers::Context::isWorkerCurrent()) { workers::Context::current()->cancel(id); return; }
|
|
293
301
|
TimerScheduler::instance().clear(id);
|
|
294
302
|
}
|
|
295
303
|
|
|
@@ -305,8 +313,18 @@ double requestAnimationFrame(AnimationFrameCallback callback)
|
|
|
305
313
|
|
|
306
314
|
void runAnimationFrameCallbacks(AnimationFrameTimestamp timestampMs)
|
|
307
315
|
{
|
|
316
|
+
callbackStage.store("worker_callbacks", std::memory_order_relaxed);
|
|
317
|
+
workers::Context::runMainPending();
|
|
318
|
+
callbackStage.store("timer_callbacks", std::memory_order_relaxed);
|
|
308
319
|
TimerScheduler::instance().run(static_cast<double>(timestampMs));
|
|
320
|
+
callbackStage.store("animation_callbacks", std::memory_order_relaxed);
|
|
309
321
|
AnimationFrameQueue::instance().run(timestampMs);
|
|
322
|
+
callbackStage.store("idle", std::memory_order_relaxed);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const char *animationFrameCallbackStage()
|
|
326
|
+
{
|
|
327
|
+
return callbackStage.load(std::memory_order_relaxed);
|
|
310
328
|
}
|
|
311
329
|
|
|
312
330
|
void resetAnimationFrameCallbacks()
|
package/host/video.cpp
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#include "host/video.h"
|
|
3
|
+
#include "host/mjpeg.h"
|
|
4
|
+
#include "ui/node.h"
|
|
5
|
+
#include "image.h"
|
|
6
|
+
#include "memory.h"
|
|
7
|
+
#include "pixel.h"
|
|
8
|
+
#include <stdexcept>
|
|
9
|
+
#include <unordered_map>
|
|
10
|
+
|
|
11
|
+
namespace gea::host {
|
|
12
|
+
struct HTMLVideoElement::State {
|
|
13
|
+
int node = -1, image = -1;
|
|
14
|
+
uint32_t width = 0, height = 0;
|
|
15
|
+
uint64_t sequence = 0;
|
|
16
|
+
MediaStream source;
|
|
17
|
+
std::string url;
|
|
18
|
+
std::unique_ptr<video::MjpegStream> mjpeg;
|
|
19
|
+
bool autoplay = false, paused = true, playing = false;
|
|
20
|
+
std::function<void()> onPlaying;
|
|
21
|
+
std::shared_ptr<const media::VideoFrame> presentedFrame;
|
|
22
|
+
~State() { if (image >= 0) gea::framework::graphics::ImageStore::instance().dispose(image); }
|
|
23
|
+
static std::unordered_map<int, std::weak_ptr<State>>& registry() {
|
|
24
|
+
static std::unordered_map<int, std::weak_ptr<State>> states;
|
|
25
|
+
return states;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
HTMLVideoElement::HTMLVideoElement(const gea::embedded::ui::NodeHandle& node) {
|
|
30
|
+
if (!node.valid()) return;
|
|
31
|
+
auto& entry = State::registry()[node.id()];
|
|
32
|
+
state_ = entry.lock();
|
|
33
|
+
if (!state_) { state_ = std::make_shared<State>(); state_->node = node.id(); entry = state_; }
|
|
34
|
+
}
|
|
35
|
+
bool HTMLVideoElement::play() const {
|
|
36
|
+
if (!state_) return false;
|
|
37
|
+
if (!state_->url.empty() && !state_->mjpeg) {
|
|
38
|
+
state_->mjpeg = std::make_unique<video::MjpegStream>(state_->url);
|
|
39
|
+
state_->source = state_->mjpeg->stream();
|
|
40
|
+
state_->sequence = 0;
|
|
41
|
+
}
|
|
42
|
+
if (state_->source.getVideoTracks().empty()) return false;
|
|
43
|
+
state_->paused = false;
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
void HTMLVideoElement::pause() const {
|
|
47
|
+
if (state_) {
|
|
48
|
+
state_->paused = true;
|
|
49
|
+
state_->playing = false;
|
|
50
|
+
if (state_->mjpeg) { state_->mjpeg.reset(); state_->source = {}; }
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
bool HTMLVideoElement::paused() const { return !state_ || state_->paused; }
|
|
54
|
+
bool HTMLVideoElement::autoplay() const { return state_ && state_->autoplay; }
|
|
55
|
+
void HTMLVideoElement::setAutoplay(bool enabled) const {
|
|
56
|
+
if (!state_) return;
|
|
57
|
+
state_->autoplay = enabled;
|
|
58
|
+
if (enabled) play();
|
|
59
|
+
}
|
|
60
|
+
std::string HTMLVideoElement::src() const { return state_ ? state_->url : std::string{}; }
|
|
61
|
+
void HTMLVideoElement::setSrc(const std::string& url) const {
|
|
62
|
+
setSrcObject(MediaStream{});
|
|
63
|
+
state_->url = url;
|
|
64
|
+
if (state_->autoplay && !url.empty()) play();
|
|
65
|
+
}
|
|
66
|
+
MediaStream HTMLVideoElement::srcObject() const {
|
|
67
|
+
return state_ && state_->url.empty() ? state_->source : MediaStream{};
|
|
68
|
+
}
|
|
69
|
+
void HTMLVideoElement::setSrcObject(MediaStream stream) const {
|
|
70
|
+
if (!state_) throw std::logic_error("null HTMLVideoElement");
|
|
71
|
+
state_->mjpeg.reset();
|
|
72
|
+
state_->url.clear();
|
|
73
|
+
state_->source = stream;
|
|
74
|
+
state_->sequence = 0;
|
|
75
|
+
state_->width = state_->height = 0;
|
|
76
|
+
state_->playing = false;
|
|
77
|
+
// Detaching/replacing a stream releases its presentation buffer immediately.
|
|
78
|
+
// ImageStore borrows that buffer on RGB565 targets, so remove the slot first.
|
|
79
|
+
if (state_->image >= 0) {
|
|
80
|
+
gea::embedded::ui::NodeHandle(state_->node).style().imageId(-1);
|
|
81
|
+
gea::framework::graphics::ImageStore::instance().dispose(state_->image);
|
|
82
|
+
state_->image = -1;
|
|
83
|
+
}
|
|
84
|
+
state_->presentedFrame.reset();
|
|
85
|
+
if (state_->autoplay) play();
|
|
86
|
+
}
|
|
87
|
+
double HTMLVideoElement::videoWidth() const { return state_ ? state_->width : 0; }
|
|
88
|
+
double HTMLVideoElement::videoHeight() const { return state_ ? state_->height : 0; }
|
|
89
|
+
void HTMLVideoElement::setOnPlayingHandler(std::function<void()> callback) const {
|
|
90
|
+
if (state_) state_->onPlaying = std::move(callback);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
void HTMLVideoElement::presentFrames() {
|
|
94
|
+
using namespace gea::framework::graphics;
|
|
95
|
+
using gea::framework::memory::Allocator;
|
|
96
|
+
// Only the UI thread touches this registry and ImageStore. Decode workers
|
|
97
|
+
// publish immutable frames through media; they never paint the display.
|
|
98
|
+
std::vector<std::shared_ptr<State>> active;
|
|
99
|
+
for (auto it = State::registry().begin(); it != State::registry().end();) {
|
|
100
|
+
auto state = it->second.lock();
|
|
101
|
+
if (!state) it = State::registry().erase(it);
|
|
102
|
+
else { active.push_back(std::move(state)); ++it; }
|
|
103
|
+
}
|
|
104
|
+
for (const auto& state : active) {
|
|
105
|
+
if (state->paused) continue;
|
|
106
|
+
const auto tracks = state->source.getVideoTracks();
|
|
107
|
+
if (tracks.empty()) continue;
|
|
108
|
+
auto frame = media::latest_video_frame(tracks.front().nativeHandle);
|
|
109
|
+
if (!frame || frame->sequence == state->sequence) continue;
|
|
110
|
+
auto& store = ImageStore::instance();
|
|
111
|
+
#if GEA_EMBEDDED_PIXEL_FORMAT == GEA_PIXEL_RGB565
|
|
112
|
+
// Decoder and renderer already agree on the pixel format. Retain the
|
|
113
|
+
// immutable frame for the whole slot lifetime instead of allocating and
|
|
114
|
+
// converting a second full-sized buffer for every presentation.
|
|
115
|
+
const bool borrowed = frame->panelEndian == bool(GEA_EMBEDDED_PIXEL_PANEL_ENDIAN);
|
|
116
|
+
#else
|
|
117
|
+
const bool borrowed = false;
|
|
118
|
+
#endif
|
|
119
|
+
int image = -1;
|
|
120
|
+
if (borrowed) {
|
|
121
|
+
image = store.registerBuffer(reinterpret_cast<pixel::native_t*>(
|
|
122
|
+
const_cast<uint16_t*>(frame->rgb565.data())), frame->width, frame->height);
|
|
123
|
+
} else {
|
|
124
|
+
const size_t size = frame->rgb565.size() * sizeof(pixel::native_t);
|
|
125
|
+
auto* pixels = static_cast<pixel::native_t*>(Allocator::allocatePreferSpiram(size, 128));
|
|
126
|
+
if (!pixels) continue;
|
|
127
|
+
for (size_t i = 0; i < frame->rgb565.size(); ++i) {
|
|
128
|
+
const uint16_t p = frame->panelEndian ? pixel::byteSwap16(frame->rgb565[i]) : frame->rgb565[i];
|
|
129
|
+
pixels[i] = pixel::nativeColor(((p >> 11) & 31) * 255 / 31, ((p >> 5) & 63) * 255 / 63, (p & 31) * 255 / 31);
|
|
130
|
+
}
|
|
131
|
+
image = store.registerBuffer(pixels, frame->width, frame->height, -1, true);
|
|
132
|
+
if (image < 0) { Allocator::free(pixels); continue; }
|
|
133
|
+
}
|
|
134
|
+
if (image < 0) continue;
|
|
135
|
+
// A new slot makes normal image invalidation redraw the video while
|
|
136
|
+
// preserving its CSS clipping, stacking and the controls above it.
|
|
137
|
+
gea::embedded::ui::NodeHandle(state->node).style().imageId(image);
|
|
138
|
+
if (state->image >= 0) store.dispose(state->image);
|
|
139
|
+
state->image = image;
|
|
140
|
+
state->presentedFrame = borrowed ? frame : nullptr;
|
|
141
|
+
state->sequence = frame->sequence;
|
|
142
|
+
state->width = frame->width;
|
|
143
|
+
state->height = frame->height;
|
|
144
|
+
if (!state->playing) {
|
|
145
|
+
state->playing = true;
|
|
146
|
+
const auto callback = state->onPlaying;
|
|
147
|
+
if (callback) callback();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
namespace video { void presentFrames() { HTMLVideoElement::presentFrames(); } }
|
|
152
|
+
} // namespace gea::host
|