@geastack/host 0.1.11 → 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/host/mjpeg.cpp +31 -25
- package/include/gea/worker-runtime.h +9 -4
- package/include/host/mjpeg.h +2 -1
- package/include/host/mjpeg_parser.h +60 -18
- package/package.json +1 -1
package/host/mjpeg.cpp
CHANGED
|
@@ -94,15 +94,13 @@ struct MjpegStream::State {
|
|
|
94
94
|
config.output_type = GEA_EMBEDDED_PIXEL_PANEL_ENDIAN ?
|
|
95
95
|
JPEG_PIXEL_FORMAT_RGB565_BE : JPEG_PIXEL_FORMAT_RGB565_LE;
|
|
96
96
|
jpeg_dec_handle_t decoder = nullptr;
|
|
97
|
-
if (jpeg_dec_open(&config, &decoder) != JPEG_ERR_OK)
|
|
98
|
-
throw std::runtime_error("MJPEG SIMD decoder allocation failed");
|
|
99
97
|
struct Cleanup {
|
|
100
|
-
jpeg_dec_handle_t decoder;
|
|
101
|
-
~Cleanup() { jpeg_dec_close(decoder); }
|
|
98
|
+
jpeg_dec_handle_t& decoder;
|
|
99
|
+
~Cleanup() { if (decoder) jpeg_dec_close(decoder); }
|
|
102
100
|
} cleanup{decoder};
|
|
103
101
|
media::VideoFramePool frames;
|
|
104
102
|
const auto track = source.getVideoTracks().front().nativeHandle;
|
|
105
|
-
unsigned decoded = 0;
|
|
103
|
+
unsigned decoded = 0, raw = 0;
|
|
106
104
|
int64_t report = esp_timer_get_time(), totalDecode = 0;
|
|
107
105
|
while (running) {
|
|
108
106
|
xSemaphoreTake(wake, pdMS_TO_TICKS(100));
|
|
@@ -111,32 +109,40 @@ struct MjpegStream::State {
|
|
|
111
109
|
{ std::lock_guard lock(mutex); packet.swap(newest); }
|
|
112
110
|
if (!packet) continue;
|
|
113
111
|
const auto started = esp_timer_get_time();
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
112
|
+
auto frame = std::move(packet->rawFrame);
|
|
113
|
+
if (frame) {
|
|
114
|
+
++raw;
|
|
115
|
+
} else {
|
|
116
|
+
// RGB565 never instantiates a JPEG decoder or a second pixel buffer.
|
|
117
|
+
if (!decoder && jpeg_dec_open(&config, &decoder) != JPEG_ERR_OK)
|
|
118
|
+
throw std::runtime_error("MJPEG SIMD decoder allocation failed");
|
|
119
|
+
jpeg_dec_io_t io{};
|
|
120
|
+
io.inbuf = packet->bytes.data();
|
|
121
|
+
io.inbuf_len = int(packet->bytes.size());
|
|
122
|
+
jpeg_dec_header_info_t header{};
|
|
123
|
+
if (jpeg_dec_parse_header(decoder, &io, &header) != JPEG_ERR_OK ||
|
|
124
|
+
!header.width || !header.height || header.width > 512 || header.height > 512)
|
|
125
|
+
throw std::runtime_error("MJPEG rejected JPEG dimensions/header");
|
|
126
|
+
int outputBytes = 0;
|
|
127
|
+
if (jpeg_dec_get_outbuf_len(decoder, &outputBytes) != JPEG_ERR_OK ||
|
|
128
|
+
outputBytes != int(size_t(header.width) * header.height * sizeof(uint16_t)))
|
|
129
|
+
throw std::runtime_error("MJPEG unexpected decoder output size");
|
|
130
|
+
frame = frames.acquire(header.width, header.height);
|
|
131
|
+
if (!frame) continue;
|
|
132
|
+
io.outbuf = reinterpret_cast<uint8_t*>(frame->rgb565.data());
|
|
133
|
+
if (jpeg_dec_process(decoder, &io) != JPEG_ERR_OK || io.out_size != outputBytes)
|
|
134
|
+
throw std::runtime_error("MJPEG JPEG decode failed");
|
|
135
|
+
frame->panelEndian = bool(GEA_EMBEDDED_PIXEL_PANEL_ENDIAN);
|
|
136
|
+
frame->timestampMs = packet->timestampMs;
|
|
137
|
+
}
|
|
132
138
|
if (!running) break;
|
|
133
139
|
media::publish_video_frame(track, std::move(frame));
|
|
134
140
|
++decoded;
|
|
135
141
|
totalDecode += esp_timer_get_time() - started;
|
|
136
142
|
const auto now = esp_timer_get_time();
|
|
137
143
|
if (now - report >= 2000000) {
|
|
138
|
-
ESP_LOGI("gea_mjpeg", "decoded=%u received=%u replaced=%u decode_us=%lld",
|
|
139
|
-
decoded, received.load(), replaced.load(), (long long)(totalDecode / decoded));
|
|
144
|
+
ESP_LOGI("gea_mjpeg", "decoded=%u received=%u replaced=%u decode_us=%lld raw=%u",
|
|
145
|
+
decoded, received.load(), replaced.load(), (long long)(totalDecode / decoded), raw);
|
|
140
146
|
report = now;
|
|
141
147
|
}
|
|
142
148
|
vTaskDelay(1); // Keep raster and CPU1 idle work progressing under sustained video.
|
|
@@ -83,10 +83,11 @@ void present(const T &value, Fn &&fn) {
|
|
|
83
83
|
else if constexpr (requires { T::arity; value.index(); }) visit(value, [&](const auto &item) { present(item, fn); });
|
|
84
84
|
else fn(value);
|
|
85
85
|
}
|
|
86
|
-
struct
|
|
87
|
-
std::vector<gea::Ref<
|
|
86
|
+
template <typename Buffer> struct BufferTransferList {
|
|
87
|
+
std::vector<gea::Ref<Buffer>> buffers;
|
|
88
88
|
std::vector<native::MessagePort> ports;
|
|
89
89
|
};
|
|
90
|
+
using TransferList = BufferTransferList<gea::ArrayBuffer>;
|
|
90
91
|
template <typename List>
|
|
91
92
|
TransferList transfers(const List &input) {
|
|
92
93
|
TransferList result;
|
|
@@ -111,8 +112,12 @@ TransferList transfers(const List &input) {
|
|
|
111
112
|
else inspect(input);
|
|
112
113
|
return result;
|
|
113
114
|
}
|
|
114
|
-
|
|
115
|
-
|
|
115
|
+
// Keep buffer operations dependent on the instantiated bridge. Older compiler
|
|
116
|
+
// runtimes have no detachment API, but must still be able to include the host
|
|
117
|
+
// declarations in applications that do not use workers. An actual worker bridge
|
|
118
|
+
// still requires the real detachment API; no copy or no-op substitutes for it.
|
|
119
|
+
template <typename Data, typename Buffer>
|
|
120
|
+
native::MessageData cloneData(const Data &data, BufferTransferList<Buffer> &list) {
|
|
116
121
|
native::MessageData result;
|
|
117
122
|
visit(data, [&](const auto &value) {
|
|
118
123
|
using T = std::decay_t<decltype(value)>;
|
package/include/host/mjpeg.h
CHANGED
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
#include <string>
|
|
6
6
|
|
|
7
7
|
namespace gea::host::video {
|
|
8
|
-
// Network and JPEG decoding stay in native workers.
|
|
8
|
+
// Network and optional JPEG decoding stay in native workers. Multipart RGB565
|
|
9
|
+
// writes directly into pooled pixels. The UI receives only the
|
|
9
10
|
// newest immutable RGB565 frame through the same media sink as WebRTC.
|
|
10
11
|
class MjpegStream {
|
|
11
12
|
public:
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
2
|
#pragma once
|
|
3
|
-
#include "host/
|
|
3
|
+
#include "host/video_frame_pool.h"
|
|
4
|
+
#include <bit>
|
|
4
5
|
#include <charconv>
|
|
5
6
|
#include <functional>
|
|
6
7
|
#include <span>
|
|
@@ -10,28 +11,47 @@
|
|
|
10
11
|
|
|
11
12
|
namespace gea::host::video {
|
|
12
13
|
using JpegBytes = std::vector<uint8_t, media::VideoAllocator<uint8_t>>;
|
|
13
|
-
struct JpegPacket {
|
|
14
|
+
struct JpegPacket {
|
|
15
|
+
JpegBytes bytes;
|
|
16
|
+
uint32_t timestampMs = 0;
|
|
17
|
+
// Raw transport writes directly into the final pooled pixel allocation.
|
|
18
|
+
std::shared_ptr<media::VideoFrame> rawFrame;
|
|
19
|
+
};
|
|
14
20
|
|
|
15
21
|
// Length-delimited multipart parsing survives arbitrary TCP fragmentation.
|
|
16
22
|
// Never search the JPEG body for boundaries or allocate from untrusted sizes.
|
|
17
23
|
class MjpegParser {
|
|
18
24
|
public:
|
|
19
25
|
bool push(std::span<const uint8_t> bytes, const std::function<void(JpegPacket)>& emit) {
|
|
20
|
-
|
|
26
|
+
while (!bytes.empty()) {
|
|
21
27
|
if (remaining_) {
|
|
22
|
-
|
|
23
|
-
if (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
const auto count = std::min(remaining_, bytes.size());
|
|
29
|
+
if (format_ == Format::Jpeg) {
|
|
30
|
+
packet_.bytes.insert(packet_.bytes.end(), bytes.begin(), bytes.begin() + count);
|
|
31
|
+
} else if (packet_.rawFrame) {
|
|
32
|
+
std::memcpy(reinterpret_cast<uint8_t*>(packet_.rawFrame->rgb565.data()) + length_ - remaining_,
|
|
33
|
+
bytes.data(), count);
|
|
34
|
+
}
|
|
35
|
+
bytes = bytes.subspan(count);
|
|
36
|
+
remaining_ -= count;
|
|
37
|
+
if (!remaining_) {
|
|
38
|
+
if (format_ == Format::Jpeg) {
|
|
39
|
+
const auto& jpeg = packet_.bytes;
|
|
40
|
+
if (jpeg.size() < 4 || jpeg[0] != 0xff || jpeg[1] != 0xd8 ||
|
|
41
|
+
jpeg[jpeg.size() - 2] != 0xff || jpeg.back() != 0xd9) return false;
|
|
42
|
+
}
|
|
43
|
+
// Exhausted pool means a slow sink: consume/drop this whole picture,
|
|
44
|
+
// never overwrite a published buffer or grow a frame backlog.
|
|
45
|
+
if (format_ == Format::Jpeg || packet_.rawFrame) emit(std::move(packet_));
|
|
28
46
|
packet_ = {};
|
|
29
|
-
length_ = 0;
|
|
30
|
-
|
|
47
|
+
length_ = width_ = height_ = 0;
|
|
48
|
+
format_ = Format::None;
|
|
31
49
|
inHeaders_ = false;
|
|
32
50
|
}
|
|
33
51
|
continue;
|
|
34
52
|
}
|
|
53
|
+
const auto byte = bytes.front();
|
|
54
|
+
bytes = bytes.subspan(1);
|
|
35
55
|
if (line_.size() >= 1024 || ++headerBytes_ > 2048) return false;
|
|
36
56
|
line_ += char(byte);
|
|
37
57
|
if (byte != '\n') continue;
|
|
@@ -39,8 +59,19 @@ class MjpegParser {
|
|
|
39
59
|
line_.resize(line_.size() - 2);
|
|
40
60
|
if (line_.empty()) {
|
|
41
61
|
if (inHeaders_) {
|
|
42
|
-
if (!length_ ||
|
|
43
|
-
|
|
62
|
+
if (!length_ || format_ == Format::None) return false;
|
|
63
|
+
if (format_ == Format::Jpeg) {
|
|
64
|
+
if (length_ > 65536) return false;
|
|
65
|
+
packet_.bytes.reserve(length_);
|
|
66
|
+
} else {
|
|
67
|
+
if (!width_ || !height_ || length_ != size_t(width_) * height_ * 2) return false;
|
|
68
|
+
packet_.rawFrame = frames_.acquire(width_, height_);
|
|
69
|
+
if (packet_.rawFrame) {
|
|
70
|
+
packet_.rawFrame->timestampMs = packet_.timestampMs;
|
|
71
|
+
packet_.rawFrame->panelEndian = (format_ == Format::Rgb565Be) !=
|
|
72
|
+
(std::endian::native == std::endian::big);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
44
75
|
remaining_ = length_;
|
|
45
76
|
headerBytes_ = 0;
|
|
46
77
|
}
|
|
@@ -58,17 +89,24 @@ class MjpegParser {
|
|
|
58
89
|
for (auto& c : name) if (c >= 'A' && c <= 'Z') c += 'a' - 'A';
|
|
59
90
|
auto value = std::string_view(line_).substr(colon + 1);
|
|
60
91
|
while (!value.empty() && value.front() == ' ') value.remove_prefix(1);
|
|
61
|
-
if (name == "content-length" || name == "x-timestamp-ms") {
|
|
92
|
+
if (name == "content-length" || name == "x-timestamp-ms" || name == "x-width" || name == "x-height") {
|
|
62
93
|
uint32_t number = 0;
|
|
63
94
|
const auto parsed = std::from_chars(value.data(), value.data() + value.size(), number);
|
|
64
95
|
if (parsed.ec != std::errc{} || parsed.ptr != value.data() + value.size()) return false;
|
|
65
96
|
if (name == "content-length") {
|
|
66
|
-
if (length_ || number < 4 || number >
|
|
97
|
+
if (length_ || number < 4 || number > 512 * 512 * 2) return false;
|
|
67
98
|
length_ = number;
|
|
99
|
+
} else if (name == "x-width" || name == "x-height") {
|
|
100
|
+
auto& dimension = name == "x-width" ? width_ : height_;
|
|
101
|
+
if (dimension || !number || number > 512) return false;
|
|
102
|
+
dimension = number;
|
|
68
103
|
} else packet_.timestampMs = number;
|
|
69
104
|
} else if (name == "content-type") {
|
|
70
|
-
if (
|
|
71
|
-
|
|
105
|
+
if (format_ != Format::None) return false;
|
|
106
|
+
if (value == "image/jpeg") format_ = Format::Jpeg;
|
|
107
|
+
else if (value == "application/x-rgb565-be") format_ = Format::Rgb565Be;
|
|
108
|
+
else if (value == "application/x-rgb565-le") format_ = Format::Rgb565Le;
|
|
109
|
+
else return false;
|
|
72
110
|
}
|
|
73
111
|
}
|
|
74
112
|
line_.clear();
|
|
@@ -76,9 +114,13 @@ class MjpegParser {
|
|
|
76
114
|
return true;
|
|
77
115
|
}
|
|
78
116
|
private:
|
|
117
|
+
enum class Format { None, Jpeg, Rgb565Le, Rgb565Be };
|
|
79
118
|
std::string line_, boundary_;
|
|
80
119
|
JpegPacket packet_;
|
|
120
|
+
media::VideoFramePool frames_;
|
|
121
|
+
uint32_t width_ = 0, height_ = 0;
|
|
81
122
|
size_t remaining_ = 0, length_ = 0, headerBytes_ = 0;
|
|
82
|
-
bool inHeaders_ = false
|
|
123
|
+
bool inHeaders_ = false;
|
|
124
|
+
Format format_ = Format::None;
|
|
83
125
|
};
|
|
84
126
|
} // namespace gea::host::video
|