@geastack/host 0.1.10 → 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.
- 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 +199 -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 +184 -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 +22 -0
- package/include/host/mjpeg_parser.h +84 -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,169 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
#include "host/media.h"
|
|
5
|
+
#include "host/rtc_vp8.h"
|
|
6
|
+
#include "host/video_color.h"
|
|
7
|
+
#include "host/video_frame_pool.h"
|
|
8
|
+
#include <vpx/vp8dx.h>
|
|
9
|
+
#include <vpx/vpx_decoder.h>
|
|
10
|
+
#include <algorithm>
|
|
11
|
+
#include <memory>
|
|
12
|
+
#include <chrono>
|
|
13
|
+
#if defined(GEA_VIDEO_SIMD_ESP32S3) && GEA_VIDEO_SIMD_ESP32S3 && defined(ESP_PLATFORM)
|
|
14
|
+
#include "esp_heap_caps.h"
|
|
15
|
+
#include "vp8_s3.h"
|
|
16
|
+
#include "vp8_profile_s3.h"
|
|
17
|
+
#endif
|
|
18
|
+
|
|
19
|
+
namespace gea::host::rtc::vp8 {
|
|
20
|
+
|
|
21
|
+
// Decode every reference frame on one worker; only decoded display frames may
|
|
22
|
+
// be coalesced. Dropping encoded interframes would corrupt following pictures.
|
|
23
|
+
class Decoder {
|
|
24
|
+
public:
|
|
25
|
+
static constexpr size_t maxPixels = 512 * 768;
|
|
26
|
+
Decoder() {
|
|
27
|
+
#if defined(GEA_VIDEO_SIMD_ESP32S3) && GEA_VIDEO_SIMD_ESP32S3 && defined(ESP_PLATFORM)
|
|
28
|
+
// Keep only the hot SIMD intermediates internal. Reference planes and the
|
|
29
|
+
// decoder stack remain external; this bounded allocation is 672 bytes.
|
|
30
|
+
workspace_ = heap_caps_aligned_alloc(16, GEA_VP8_WORKSPACE_BYTES,
|
|
31
|
+
MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
|
|
32
|
+
#endif
|
|
33
|
+
}
|
|
34
|
+
Decoder(const Decoder&) = delete;
|
|
35
|
+
Decoder& operator=(const Decoder&) = delete;
|
|
36
|
+
~Decoder() {
|
|
37
|
+
reset();
|
|
38
|
+
#if defined(GEA_VIDEO_SIMD_ESP32S3) && GEA_VIDEO_SIMD_ESP32S3 && defined(ESP_PLATFORM)
|
|
39
|
+
heap_caps_free(workspace_);
|
|
40
|
+
#endif
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
void reset() {
|
|
44
|
+
if (open_) vpx_codec_destroy(&codec_);
|
|
45
|
+
codec_ = {};
|
|
46
|
+
open_ = false;
|
|
47
|
+
frames_.reset();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
bool needsKeyframe() const { return !open_; }
|
|
51
|
+
struct Timing { uint64_t setupUs = 0, decodeUs = 0, allocateUs = 0, convertUs = 0;
|
|
52
|
+
uint64_t tokenUs = 0, macroblockUs = 0, filterUs = 0;
|
|
53
|
+
unsigned macroblocks = 0, tokenBlocks = 0, filterRows = 0;
|
|
54
|
+
unsigned allocationFailedWidth = 0, allocationFailedHeight = 0; };
|
|
55
|
+
Timing timing() const { return timing_; }
|
|
56
|
+
size_t internalWorkspaceBytes() const {
|
|
57
|
+
#if defined(GEA_VIDEO_SIMD_ESP32S3) && GEA_VIDEO_SIMD_ESP32S3 && defined(ESP_PLATFORM)
|
|
58
|
+
return workspace_ ? GEA_VP8_WORKSPACE_BYTES : 0;
|
|
59
|
+
#else
|
|
60
|
+
return 0;
|
|
61
|
+
#endif
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
std::shared_ptr<media::VideoFrame> decode(std::span<const uint8_t> bytes, uint32_t timestampMs,
|
|
65
|
+
bool present = true, uint64_t presentationDelayUs = 0) {
|
|
66
|
+
timing_ = {};
|
|
67
|
+
const auto started = nowUs();
|
|
68
|
+
if (bytes.empty() || bytes.size() > Assembler::maxFrameBytes) return fail();
|
|
69
|
+
if (!(bytes[0] & 1)) {
|
|
70
|
+
vpx_codec_stream_info_t info{};
|
|
71
|
+
info.sz = sizeof(info);
|
|
72
|
+
if (vpx_codec_peek_stream_info(vpx_codec_vp8_dx(), bytes.data(),
|
|
73
|
+
static_cast<unsigned>(bytes.size()), &info) != VPX_CODEC_OK ||
|
|
74
|
+
!info.is_kf || !validSize(info.w, info.h)) return fail();
|
|
75
|
+
if (!open_) {
|
|
76
|
+
vpx_codec_dec_cfg_t config{};
|
|
77
|
+
config.threads = 1;
|
|
78
|
+
config.w = info.w;
|
|
79
|
+
config.h = info.h;
|
|
80
|
+
if (vpx_codec_dec_init(&codec_, vpx_codec_vp8_dx(), &config, 0) != VPX_CODEC_OK) return fail();
|
|
81
|
+
open_ = true;
|
|
82
|
+
}
|
|
83
|
+
} else if (!open_) return nullptr;
|
|
84
|
+
|
|
85
|
+
const auto decodeStarted = nowUs();
|
|
86
|
+
timing_.setupUs = decodeStarted - started;
|
|
87
|
+
vpx_codec_err_t result;
|
|
88
|
+
{
|
|
89
|
+
#if defined(GEA_VIDEO_SIMD_ESP32S3) && GEA_VIDEO_SIMD_ESP32S3 && defined(ESP_PLATFORM)
|
|
90
|
+
struct WorkspaceScope {
|
|
91
|
+
void* previous;
|
|
92
|
+
~WorkspaceScope() { gea_vp8_bind_workspace_s3(previous); }
|
|
93
|
+
} workspaceScope{gea_vp8_bind_workspace_s3(workspace_)};
|
|
94
|
+
struct ProfileScope {
|
|
95
|
+
gea_vp8_profile_s3 data{};
|
|
96
|
+
gea_vp8_profile_s3* previous;
|
|
97
|
+
ProfileScope() : previous(gea_vp8_profile_bind_s3(
|
|
98
|
+
gea_vp8_profile_requested_s3() ? &data : nullptr)) {}
|
|
99
|
+
~ProfileScope() { gea_vp8_profile_bind_s3(previous); }
|
|
100
|
+
} profileScope;
|
|
101
|
+
#endif
|
|
102
|
+
result = vpx_codec_decode(&codec_, bytes.data(), static_cast<unsigned>(bytes.size()), nullptr, 0);
|
|
103
|
+
#if defined(GEA_VIDEO_SIMD_ESP32S3) && GEA_VIDEO_SIMD_ESP32S3 && defined(ESP_PLATFORM)
|
|
104
|
+
timing_.tokenUs = profileScope.data.us[GEA_VP8_PROFILE_TOKENS];
|
|
105
|
+
timing_.macroblockUs = profileScope.data.us[GEA_VP8_PROFILE_MACROBLOCK];
|
|
106
|
+
timing_.filterUs = profileScope.data.us[GEA_VP8_PROFILE_LOOPFILTER];
|
|
107
|
+
timing_.macroblocks = profileScope.data.calls[GEA_VP8_PROFILE_MACROBLOCK];
|
|
108
|
+
timing_.tokenBlocks = profileScope.data.calls[GEA_VP8_PROFILE_TOKENS];
|
|
109
|
+
timing_.filterRows = profileScope.data.calls[GEA_VP8_PROFILE_LOOPFILTER];
|
|
110
|
+
#endif
|
|
111
|
+
}
|
|
112
|
+
const auto convertStarted = nowUs();
|
|
113
|
+
timing_.decodeUs = convertStarted - decodeStarted;
|
|
114
|
+
if (result != VPX_CODEC_OK)
|
|
115
|
+
return fail();
|
|
116
|
+
std::shared_ptr<media::VideoFrame> latest;
|
|
117
|
+
vpx_codec_iter_t iterator = nullptr;
|
|
118
|
+
while (const auto* image = vpx_codec_get_frame(&codec_, &iterator)) {
|
|
119
|
+
if (image->fmt != VPX_IMG_FMT_I420 || !validSize(image->d_w, image->d_h)) return fail();
|
|
120
|
+
// Preserve every encoded reference, but avoid allocating/converting a
|
|
121
|
+
// display image already superseded by a newer queued picture.
|
|
122
|
+
// Rate limiting is evaluated after the expensive codec work. Decoding
|
|
123
|
+
// can itself consume the remaining interval; deciding before it hides
|
|
124
|
+
// every other frame when the decoder is slower than the display limit.
|
|
125
|
+
if (!present || (presentationDelayUs && nowUs() - started < presentationDelayUs)) continue;
|
|
126
|
+
const auto allocateStarted = nowUs();
|
|
127
|
+
latest = frames_.acquire(image->d_w, image->d_h);
|
|
128
|
+
if (!latest) {
|
|
129
|
+
timing_.allocationFailedWidth = image->d_w;
|
|
130
|
+
timing_.allocationFailedHeight = image->d_h;
|
|
131
|
+
latest.reset();
|
|
132
|
+
// The encoded picture decoded successfully. Retain its codec references;
|
|
133
|
+
// memory pressure is not a bitstream error and must not force a keyframe.
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
timing_.allocateUs += nowUs() - allocateStarted;
|
|
137
|
+
latest->timestampMs = timestampMs;
|
|
138
|
+
#if defined(GEA_EMBEDDED_PIXEL_PANEL_ENDIAN) && GEA_EMBEDDED_PIXEL_PANEL_ENDIAN
|
|
139
|
+
latest->panelEndian = true;
|
|
140
|
+
#endif
|
|
141
|
+
media::i420ToRgb565(image->planes[0], image->stride[0],
|
|
142
|
+
image->planes[1], image->stride[1],
|
|
143
|
+
image->planes[2], image->stride[2],
|
|
144
|
+
image->d_w, image->d_h, latest->rgb565.data(), latest->panelEndian);
|
|
145
|
+
}
|
|
146
|
+
timing_.convertUs = nowUs() - convertStarted;
|
|
147
|
+
return latest;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
private:
|
|
151
|
+
static uint64_t nowUs() {
|
|
152
|
+
return std::chrono::duration_cast<std::chrono::microseconds>(
|
|
153
|
+
std::chrono::steady_clock::now().time_since_epoch()).count();
|
|
154
|
+
}
|
|
155
|
+
static bool validSize(unsigned width, unsigned height) {
|
|
156
|
+
return width && height && width <= 1024 && height <= 1024 && size_t(width) * height <= maxPixels;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
std::shared_ptr<media::VideoFrame> fail() { reset(); return nullptr; }
|
|
160
|
+
vpx_codec_ctx_t codec_{};
|
|
161
|
+
bool open_ = false;
|
|
162
|
+
Timing timing_;
|
|
163
|
+
media::VideoFramePool frames_;
|
|
164
|
+
#if defined(GEA_VIDEO_SIMD_ESP32S3) && GEA_VIDEO_SIMD_ESP32S3 && defined(ESP_PLATFORM)
|
|
165
|
+
void* workspace_ = nullptr;
|
|
166
|
+
#endif
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
} // namespace gea::host::rtc::vp8
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
#include "host/rtc_rtp.h"
|
|
5
|
+
#include "host/rtc_vp8.h"
|
|
6
|
+
|
|
7
|
+
namespace gea::host::rtc::vp8 {
|
|
8
|
+
|
|
9
|
+
// One negotiated VP8 SSRC, after SRTP authentication. Audio, RTCP, RTX and
|
|
10
|
+
// unrelated SSRCs never enter the video sequence space. No RTX is advertised.
|
|
11
|
+
class Receiver {
|
|
12
|
+
public:
|
|
13
|
+
Receiver(uint8_t payloadType, uint32_t ssrc) : payloadType_(payloadType), ssrc_(ssrc) {}
|
|
14
|
+
|
|
15
|
+
template<class Deliver>
|
|
16
|
+
void push(std::span<const uint8_t> bytes, uint64_t nowMs, Deliver&& deliver) {
|
|
17
|
+
const auto packet = rtp::parse(bytes);
|
|
18
|
+
if (!packet || packet->payloadType != payloadType_ || packet->ssrc != ssrc_) return;
|
|
19
|
+
reorder_.push(bytes, nowMs, [&](const rtp::Packet& ordered) { assemble(ordered, deliver); });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
template<class Deliver>
|
|
23
|
+
void poll(uint64_t nowMs, Deliver&& deliver) {
|
|
24
|
+
reorder_.poll(nowMs, [&](const rtp::Packet& ordered) { assemble(ordered, deliver); });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Decoder failure/overflow invalidates references, even if RTP had no loss.
|
|
28
|
+
void requireKeyframe() { assembler_.reset(); }
|
|
29
|
+
|
|
30
|
+
bool needsKeyframe() const { return assembler_.needsKeyframe(); }
|
|
31
|
+
|
|
32
|
+
// Call on a timer too, so a lost initial PLI cannot leave the picture frozen.
|
|
33
|
+
bool takeKeyframeRequest(uint64_t nowMs) {
|
|
34
|
+
if (!needsKeyframe() || (requested_ && nowMs - lastRequestMs_ < 500)) return false;
|
|
35
|
+
requested_ = true;
|
|
36
|
+
lastRequestMs_ = nowMs;
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
private:
|
|
41
|
+
template<class Deliver>
|
|
42
|
+
void assemble(const rtp::Packet& packet, Deliver&& deliver) {
|
|
43
|
+
auto frame = assembler_.push(packet.sequence, packet.timestamp, packet.marker, packet.payload);
|
|
44
|
+
if (frame) deliver(std::move(*frame));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
rtp::Reorder reorder_;
|
|
48
|
+
Assembler assembler_;
|
|
49
|
+
uint8_t payloadType_;
|
|
50
|
+
uint32_t ssrc_;
|
|
51
|
+
uint64_t lastRequestMs_ = 0;
|
|
52
|
+
bool requested_ = false;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
} // namespace gea::host::rtc::vp8
|
package/include/host/timers.h
CHANGED
|
@@ -28,12 +28,13 @@ struct AnimationFramePerfStats {
|
|
|
28
28
|
|
|
29
29
|
double setTimeout(TimerCallback callback, double delayMs);
|
|
30
30
|
double setInterval(TimerCallback callback, double delayMs);
|
|
31
|
-
void clearTimeout(double id);
|
|
32
|
-
void clearInterval(double id);
|
|
31
|
+
void clearTimeout(double id = 0);
|
|
32
|
+
void clearInterval(double id = 0);
|
|
33
33
|
void resetScheduledTimers();
|
|
34
34
|
|
|
35
35
|
double requestAnimationFrame(AnimationFrameCallback callback);
|
|
36
36
|
void runAnimationFrameCallbacks(AnimationFrameTimestamp timestampMs);
|
|
37
|
+
const char *animationFrameCallbackStage();
|
|
37
38
|
void resetAnimationFrameCallbacks();
|
|
38
39
|
void animationFramePerfStatsReset();
|
|
39
40
|
AnimationFramePerfStats animationFramePerfStatsRead();
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
#include "host/media.h"
|
|
4
|
+
#include <functional>
|
|
5
|
+
#include <memory>
|
|
6
|
+
|
|
7
|
+
namespace gea::embedded::ui { class NodeHandle; }
|
|
8
|
+
namespace gea::host {
|
|
9
|
+
class HTMLVideoElement {
|
|
10
|
+
public:
|
|
11
|
+
HTMLVideoElement() = default;
|
|
12
|
+
explicit HTMLVideoElement(const gea::embedded::ui::NodeHandle& node);
|
|
13
|
+
bool play() const;
|
|
14
|
+
void pause() const;
|
|
15
|
+
bool paused() const;
|
|
16
|
+
bool autoplay() const;
|
|
17
|
+
void setAutoplay(bool value) const;
|
|
18
|
+
std::string src() const;
|
|
19
|
+
void setSrc(const std::string& url) const;
|
|
20
|
+
MediaStream srcObject() const;
|
|
21
|
+
void setSrcObject(MediaStream source) const;
|
|
22
|
+
void setSrcObject(std::nullptr_t) const { setSrcObject(MediaStream{}); }
|
|
23
|
+
double videoWidth() const;
|
|
24
|
+
double videoHeight() const;
|
|
25
|
+
void setOnPlaying(std::nullptr_t) const { setOnPlayingHandler({}); }
|
|
26
|
+
template<class Callback> void setOnPlaying(Callback callback) const {
|
|
27
|
+
setOnPlayingHandler([callback] { callback(); });
|
|
28
|
+
}
|
|
29
|
+
bool operator==(std::nullptr_t) const { return !state_; }
|
|
30
|
+
bool operator==(const HTMLVideoElement& other) const { return state_ == other.state_; }
|
|
31
|
+
explicit operator bool() const { return static_cast<bool>(state_); }
|
|
32
|
+
static void presentFrames();
|
|
33
|
+
private:
|
|
34
|
+
void setOnPlayingHandler(std::function<void()> callback) const;
|
|
35
|
+
struct State;
|
|
36
|
+
std::shared_ptr<State> state_;
|
|
37
|
+
};
|
|
38
|
+
namespace video { void presentFrames(); }
|
|
39
|
+
} // namespace gea::host
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
#include <algorithm>
|
|
5
|
+
#include <cstddef>
|
|
6
|
+
#include <cstdint>
|
|
7
|
+
|
|
8
|
+
namespace gea::host::media {
|
|
9
|
+
|
|
10
|
+
#if defined(GEA_VIDEO_SIMD_ESP32S3) && GEA_VIDEO_SIMD_ESP32S3
|
|
11
|
+
extern "C" void gea_i420_rgb565_row_s3(const uint8_t*, const uint8_t*, const uint8_t*,
|
|
12
|
+
uint16_t*, unsigned);
|
|
13
|
+
extern "C" void gea_i420_rgb565_row_be_s3(const uint8_t*, const uint8_t*, const uint8_t*,
|
|
14
|
+
uint16_t*, unsigned);
|
|
15
|
+
struct I420SecondRow { const uint8_t* y; uint16_t* output; };
|
|
16
|
+
extern "C" void gea_i420_rgb565_rows_s3(const uint8_t*, const uint8_t*, const uint8_t*,
|
|
17
|
+
uint16_t*, unsigned, const I420SecondRow*);
|
|
18
|
+
extern "C" void gea_i420_rgb565_rows_be_s3(const uint8_t*, const uint8_t*, const uint8_t*,
|
|
19
|
+
uint16_t*, unsigned, const I420SecondRow*);
|
|
20
|
+
extern "C" void gea_rgb565_swap_s3(uint16_t*, unsigned);
|
|
21
|
+
#endif
|
|
22
|
+
|
|
23
|
+
// Convert in place before publication, never through another full-frame image.
|
|
24
|
+
inline void rgb565SwapBytes(uint16_t* pixels, size_t count) {
|
|
25
|
+
size_t firstScalar = 0;
|
|
26
|
+
#if defined(GEA_VIDEO_SIMD_ESP32S3) && GEA_VIDEO_SIMD_ESP32S3
|
|
27
|
+
if (!(uintptr_t(pixels) & 15) && count >= 8) {
|
|
28
|
+
firstScalar = count & ~size_t(7);
|
|
29
|
+
gea_rgb565_swap_s3(pixels, unsigned(firstScalar));
|
|
30
|
+
}
|
|
31
|
+
#endif
|
|
32
|
+
for (size_t i = firstScalar; i < count; ++i)
|
|
33
|
+
pixels[i] = uint16_t((pixels[i] << 8) | (pixels[i] >> 8));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Limited-range BT.601, matching the VP8 decoder's existing conversion. Each
|
|
37
|
+
// I420 chroma sample belongs to a 2x2 luma block. Reuse its three products for
|
|
38
|
+
// all four pixels, including the odd-width/height tails and padded strides.
|
|
39
|
+
inline void i420ToRgb565(const uint8_t* yPlane, ptrdiff_t yStride,
|
|
40
|
+
const uint8_t* uPlane, ptrdiff_t uStride,
|
|
41
|
+
const uint8_t* vPlane, ptrdiff_t vStride,
|
|
42
|
+
unsigned width, unsigned height, uint16_t* output,
|
|
43
|
+
bool panelEndian = false) {
|
|
44
|
+
for (unsigned y = 0; y < height; y += 2) {
|
|
45
|
+
const auto* yy = yPlane + y * yStride;
|
|
46
|
+
const auto* uu = uPlane + (y / 2) * uStride;
|
|
47
|
+
const auto* vv = vPlane + (y / 2) * vStride;
|
|
48
|
+
auto* row = output + size_t(y) * width;
|
|
49
|
+
unsigned firstScalar = 0;
|
|
50
|
+
#if defined(GEA_VIDEO_SIMD_ESP32S3) && GEA_VIDEO_SIMD_ESP32S3
|
|
51
|
+
// VP8 planes are aligned with padded strides. Keep the portable path for
|
|
52
|
+
// arbitrary callers and short tails; never round a caller's pointer down.
|
|
53
|
+
if (width >= 8 && !(uintptr_t(yy) & 7) && !(uintptr_t(uu) & 3) &&
|
|
54
|
+
!(uintptr_t(vv) & 3) && !(uintptr_t(row) & 7) &&
|
|
55
|
+
(y + 1 == height || (!(yStride & 7) && !(width & 3)))) {
|
|
56
|
+
firstScalar = width & ~7u;
|
|
57
|
+
if (y + 1 < height) {
|
|
58
|
+
const I420SecondRow second{yy + yStride, row + width};
|
|
59
|
+
const auto convert = panelEndian ? gea_i420_rgb565_rows_be_s3 : gea_i420_rgb565_rows_s3;
|
|
60
|
+
convert(yy, uu, vv, row, firstScalar, &second);
|
|
61
|
+
} else {
|
|
62
|
+
const auto convert = panelEndian ? gea_i420_rgb565_row_be_s3 : gea_i420_rgb565_row_s3;
|
|
63
|
+
convert(yy, uu, vv, row, firstScalar);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
#endif
|
|
67
|
+
for (unsigned x = firstScalar; x < width; x += 2) {
|
|
68
|
+
const int d = int(uu[x / 2]) - 128, e = int(vv[x / 2]) - 128;
|
|
69
|
+
const int red = 409 * e + 128;
|
|
70
|
+
const int green = -100 * d - 208 * e + 128;
|
|
71
|
+
const int blue = 516 * d + 128;
|
|
72
|
+
const auto pixel = [=](uint8_t luma) {
|
|
73
|
+
const int c = 298 * (int(luma) - 16);
|
|
74
|
+
const int r = std::clamp((c + red) >> 8, 0, 255);
|
|
75
|
+
const int g = std::clamp((c + green) >> 8, 0, 255);
|
|
76
|
+
const int b = std::clamp((c + blue) >> 8, 0, 255);
|
|
77
|
+
const auto value = uint16_t((r >> 3) << 11 | (g >> 2) << 5 | (b >> 3));
|
|
78
|
+
return panelEndian ? uint16_t((value << 8) | (value >> 8)) : value;
|
|
79
|
+
};
|
|
80
|
+
row[x] = pixel(yy[x]);
|
|
81
|
+
if (x + 1 < width) row[x + 1] = pixel(yy[x + 1]);
|
|
82
|
+
if (y + 1 < height) {
|
|
83
|
+
row[width + x] = pixel(yy[yStride + x]);
|
|
84
|
+
if (x + 1 < width) row[width + x + 1] = pixel(yy[yStride + x + 1]);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
} // namespace gea::host::media
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
#include "host/media.h"
|
|
5
|
+
#include <array>
|
|
6
|
+
#include <limits>
|
|
7
|
+
|
|
8
|
+
namespace gea::host::media {
|
|
9
|
+
|
|
10
|
+
// One decoder, newest published picture and an older renderer-held picture
|
|
11
|
+
// can coexist. Reserve their large pixel buffers together, then reuse only
|
|
12
|
+
// storage with no reader. Each published frame gets a fresh control block so
|
|
13
|
+
// weak frame references expire normally. Slow/additional sinks skip presentation,
|
|
14
|
+
// never receive a buffer being overwritten and never grow this bounded pool.
|
|
15
|
+
class VideoFramePool {
|
|
16
|
+
public:
|
|
17
|
+
std::shared_ptr<VideoFrame> acquire(unsigned width, unsigned height) {
|
|
18
|
+
if (!width || !height || size_t(width) > std::numeric_limits<size_t>::max() / height)
|
|
19
|
+
return nullptr;
|
|
20
|
+
const size_t count = size_t(width) * height;
|
|
21
|
+
std::shared_ptr<VideoPixels::Storage> selected;
|
|
22
|
+
for (auto& slot : slots_) {
|
|
23
|
+
if (slot && slot.use_count() != 1) continue;
|
|
24
|
+
if (!slot || slot->capacity < count) {
|
|
25
|
+
auto replacement = std::make_shared<VideoPixels::Storage>();
|
|
26
|
+
replacement->pixels = VideoPixels::allocate(count);
|
|
27
|
+
if (!replacement->pixels) continue;
|
|
28
|
+
replacement->capacity = count;
|
|
29
|
+
slot = std::move(replacement);
|
|
30
|
+
}
|
|
31
|
+
if (!selected) selected = slot;
|
|
32
|
+
}
|
|
33
|
+
if (!selected) return nullptr;
|
|
34
|
+
auto result = std::make_shared<VideoFrame>();
|
|
35
|
+
result->width = width;
|
|
36
|
+
result->height = height;
|
|
37
|
+
result->rgb565.pixels_ = selected->pixels;
|
|
38
|
+
result->rgb565.size_ = count;
|
|
39
|
+
result->rgb565.capacity_ = selected->capacity;
|
|
40
|
+
result->rgb565.pooled_ = std::move(selected);
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
void reset() { slots_.fill(nullptr); }
|
|
45
|
+
|
|
46
|
+
private:
|
|
47
|
+
std::array<std::shared_ptr<VideoPixels::Storage>, 3> slots_{};
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
} // namespace gea::host::media
|
|
@@ -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);
|