@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,115 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
#include <cstddef>
|
|
5
|
+
#include <cstdint>
|
|
6
|
+
#include <optional>
|
|
7
|
+
#include <span>
|
|
8
|
+
#include <utility>
|
|
9
|
+
#include <vector>
|
|
10
|
+
#include "host/video_pixels.h"
|
|
11
|
+
|
|
12
|
+
namespace gea::host::rtc::vp8 {
|
|
13
|
+
|
|
14
|
+
struct Payload {
|
|
15
|
+
std::span<const uint8_t> bytes;
|
|
16
|
+
bool startsFrame = false;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// RFC 7741 section 4.2. RTP framing, authentication, retransmission and packet
|
|
20
|
+
// reordering belong to the transport; this consumes its ordered VP8 payloads.
|
|
21
|
+
inline std::optional<Payload> parsePayload(std::span<const uint8_t> packet) {
|
|
22
|
+
if (packet.empty()) return std::nullopt;
|
|
23
|
+
const auto first = packet[0];
|
|
24
|
+
size_t offset = 1;
|
|
25
|
+
if (first & 0x80) {
|
|
26
|
+
if (offset == packet.size()) return std::nullopt;
|
|
27
|
+
const auto extension = packet[offset++];
|
|
28
|
+
if (extension & 0x80) {
|
|
29
|
+
if (offset == packet.size()) return std::nullopt;
|
|
30
|
+
const bool longId = packet[offset++] & 0x80;
|
|
31
|
+
if (longId) ++offset;
|
|
32
|
+
}
|
|
33
|
+
if (extension & 0x40) ++offset;
|
|
34
|
+
if (extension & 0x30) ++offset; // T and K share one byte.
|
|
35
|
+
}
|
|
36
|
+
if (offset >= packet.size()) return std::nullopt;
|
|
37
|
+
// Reserved bits must be ignored, including bit 3 adjacent to PID.
|
|
38
|
+
return Payload{packet.subspan(offset), bool(first & 0x10) && !(first & 0x07)};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
struct EncodedFrame {
|
|
42
|
+
using Bytes = std::vector<uint8_t, media::VideoAllocator<uint8_t>>;
|
|
43
|
+
// The assembler and decoder queue share this owned PSRAM buffer. Moving
|
|
44
|
+
// a complete frame must not allocate or copy it at the worker boundary.
|
|
45
|
+
Bytes bytes;
|
|
46
|
+
uint32_t timestamp = 0; // Original 90 kHz RTP clock; do not lose A/V timing.
|
|
47
|
+
bool keyframe = false;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
class Assembler {
|
|
51
|
+
public:
|
|
52
|
+
static constexpr size_t maxFrameBytes = 256 * 1024;
|
|
53
|
+
|
|
54
|
+
void reset() {
|
|
55
|
+
current_ = {};
|
|
56
|
+
active_ = haveSequence_ = false;
|
|
57
|
+
needsKeyframe_ = true;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
bool needsKeyframe() const { return needsKeyframe_; }
|
|
61
|
+
|
|
62
|
+
std::optional<EncodedFrame> push(uint16_t sequence, uint32_t timestamp, bool marker,
|
|
63
|
+
std::span<const uint8_t> packet) {
|
|
64
|
+
const auto payload = parsePayload(packet);
|
|
65
|
+
if (!payload) { reset(); return std::nullopt; }
|
|
66
|
+
|
|
67
|
+
if (haveSequence_) {
|
|
68
|
+
const auto distance = uint16_t(sequence - nextSequence_);
|
|
69
|
+
// Late/duplicate packets cannot append a second copy or restart a frame.
|
|
70
|
+
if (distance >= 0x8000) return std::nullopt;
|
|
71
|
+
if (distance) { active_ = false; current_ = {}; needsKeyframe_ = true; }
|
|
72
|
+
}
|
|
73
|
+
haveSequence_ = true;
|
|
74
|
+
nextSequence_ = uint16_t(sequence + 1);
|
|
75
|
+
|
|
76
|
+
if (payload->startsFrame) {
|
|
77
|
+
if (active_) needsKeyframe_ = true; // Previous frame lost its marker/tail.
|
|
78
|
+
current_ = {};
|
|
79
|
+
current_.timestamp = timestamp;
|
|
80
|
+
current_.keyframe = !(payload->bytes[0] & 1);
|
|
81
|
+
active_ = !needsKeyframe_ || current_.keyframe;
|
|
82
|
+
} else if (active_ && timestamp != current_.timestamp) {
|
|
83
|
+
active_ = false;
|
|
84
|
+
current_ = {};
|
|
85
|
+
needsKeyframe_ = true;
|
|
86
|
+
}
|
|
87
|
+
if (!active_) {
|
|
88
|
+
needsKeyframe_ = true;
|
|
89
|
+
return std::nullopt;
|
|
90
|
+
}
|
|
91
|
+
if (payload->bytes.size() > maxFrameBytes - current_.bytes.size()) {
|
|
92
|
+
reset();
|
|
93
|
+
return std::nullopt;
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
current_.bytes.insert(current_.bytes.end(), payload->bytes.begin(), payload->bytes.end());
|
|
97
|
+
} catch (const std::bad_alloc&) {
|
|
98
|
+
reset();
|
|
99
|
+
return std::nullopt; // A lost reference requests a keyframe, not a disconnect.
|
|
100
|
+
}
|
|
101
|
+
if (!marker) return std::nullopt;
|
|
102
|
+
active_ = false;
|
|
103
|
+
needsKeyframe_ = false;
|
|
104
|
+
return std::exchange(current_, {});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private:
|
|
108
|
+
EncodedFrame current_;
|
|
109
|
+
uint16_t nextSequence_ = 0;
|
|
110
|
+
bool haveSequence_ = false;
|
|
111
|
+
bool active_ = false;
|
|
112
|
+
bool needsKeyframe_ = true;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
} // namespace gea::host::rtc::vp8
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
#include "host/media.h"
|
|
4
|
+
#include <array>
|
|
5
|
+
#include <atomic>
|
|
6
|
+
#include <cstdio>
|
|
7
|
+
#include <mutex>
|
|
8
|
+
#include <span>
|
|
9
|
+
|
|
10
|
+
namespace gea::host::rtc::vp8 {
|
|
11
|
+
|
|
12
|
+
// Explicit diagnostic only. Retain a bounded prefix of accepted encoded
|
|
13
|
+
// references by moving their existing PSRAM vectors, never copying a frame
|
|
14
|
+
// or writing flash from the live worker. Saving requires every worker stopped.
|
|
15
|
+
class Capture {
|
|
16
|
+
public:
|
|
17
|
+
using Bytes = std::vector<uint8_t, media::VideoAllocator<uint8_t>>;
|
|
18
|
+
static constexpr unsigned maxFrames = 12;
|
|
19
|
+
static constexpr size_t maxBytes = 128 * 1024;
|
|
20
|
+
struct Frame {
|
|
21
|
+
Bytes bytes;
|
|
22
|
+
uint32_t pts = 0;
|
|
23
|
+
uint64_t wallUs = 0, cpuUs = 0, codecUs = 0, convertUs = 0;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
void workerStarted() {
|
|
27
|
+
std::lock_guard lock(mutex_);
|
|
28
|
+
workers_.fetch_add(1, std::memory_order_relaxed);
|
|
29
|
+
}
|
|
30
|
+
void workerStopped() { workers_.fetch_sub(1, std::memory_order_relaxed); }
|
|
31
|
+
|
|
32
|
+
bool start() {
|
|
33
|
+
std::lock_guard lock(mutex_);
|
|
34
|
+
if (workers_.load(std::memory_order_relaxed)) return false;
|
|
35
|
+
for (auto& frame : frames_) frame = Frame{};
|
|
36
|
+
count_ = 0; size_ = 0; width_ = height_ = 0;
|
|
37
|
+
requested_.store(true, std::memory_order_release);
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
bool requested() const { return requested_.load(std::memory_order_acquire); }
|
|
42
|
+
|
|
43
|
+
void cancel() {
|
|
44
|
+
std::lock_guard lock(mutex_);
|
|
45
|
+
requested_.store(false, std::memory_order_release);
|
|
46
|
+
for (auto& frame : frames_) frame = Frame{};
|
|
47
|
+
count_ = 0; size_ = 0; width_ = height_ = 0;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
bool take(Bytes& bytes, uint32_t pts, uint64_t wallUs, uint64_t cpuUs,
|
|
51
|
+
uint64_t codecUs, uint64_t convertUs) {
|
|
52
|
+
if (!requested()) return false;
|
|
53
|
+
std::lock_guard lock(mutex_);
|
|
54
|
+
if (!requested()) return false;
|
|
55
|
+
if (!count_) {
|
|
56
|
+
if (bytes.size() < 10 || bytes[0] & 1 || bytes[3] != 0x9d ||
|
|
57
|
+
bytes[4] != 0x01 || bytes[5] != 0x2a) return false;
|
|
58
|
+
width_ = (unsigned(bytes[6]) | unsigned(bytes[7]) << 8) & 0x3fff;
|
|
59
|
+
height_ = (unsigned(bytes[8]) | unsigned(bytes[9]) << 8) & 0x3fff;
|
|
60
|
+
if (!width_ || !height_) return false;
|
|
61
|
+
}
|
|
62
|
+
if (count_ == maxFrames || bytes.empty() || bytes.size() > maxBytes - size_) {
|
|
63
|
+
requested_.store(false, std::memory_order_release);
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
size_ += bytes.size();
|
|
67
|
+
auto& frame = frames_[count_++];
|
|
68
|
+
frame = Frame{std::move(bytes), pts, wallUs, cpuUs, codecUs, convertUs};
|
|
69
|
+
if (count_ == maxFrames) requested_.store(false, std::memory_order_release);
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
bool save(const char* path) {
|
|
74
|
+
std::lock_guard lock(mutex_);
|
|
75
|
+
// In particular, don't write SPIFFS while a live decoder uses a PSRAM stack.
|
|
76
|
+
if (!path || !*path || workers_.load(std::memory_order_relaxed) || !count_) return false;
|
|
77
|
+
requested_.store(false, std::memory_order_release);
|
|
78
|
+
auto* file = std::fopen(path, "wb");
|
|
79
|
+
if (!file) return false;
|
|
80
|
+
bool ok = write(file);
|
|
81
|
+
if (std::fclose(file)) ok = false;
|
|
82
|
+
return ok;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
void report() {
|
|
86
|
+
std::lock_guard lock(mutex_);
|
|
87
|
+
for (unsigned i = 0; i < count_; ++i) {
|
|
88
|
+
const auto& f = frames_[i];
|
|
89
|
+
std::printf("GEADEV:VP8CAPTURE FRAME n=%u bytes=%u pts=%u wall_us=%llu cpu_us=%llu codec_us=%llu convert_us=%llu\n",
|
|
90
|
+
i, unsigned(f.bytes.size()), unsigned(f.pts), (unsigned long long)f.wallUs,
|
|
91
|
+
(unsigned long long)f.cpuUs, (unsigned long long)f.codecUs, (unsigned long long)f.convertUs);
|
|
92
|
+
}
|
|
93
|
+
std::printf("GEADEV:VP8CAPTURE END frames=%u bytes=%u width=%u height=%u workers=%u\n",
|
|
94
|
+
count_, unsigned(size_), width_, height_, workers_.load(std::memory_order_relaxed));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Also used by the portable format/ownership regression without a temp file.
|
|
98
|
+
bool write(std::FILE* file) const {
|
|
99
|
+
if (!file || !count_) return false;
|
|
100
|
+
std::array<uint8_t, 32> header{'D', 'K', 'I', 'F', 0, 0, 32, 0, 'V', 'P', '8', '0'};
|
|
101
|
+
put(header.data() + 12, width_, 2); put(header.data() + 14, height_, 2);
|
|
102
|
+
put(header.data() + 16, 1000, 4); put(header.data() + 20, 1, 4);
|
|
103
|
+
put(header.data() + 24, count_, 4);
|
|
104
|
+
if (std::fwrite(header.data(), 1, header.size(), file) != header.size()) return false;
|
|
105
|
+
for (unsigned i = 0; i < count_; ++i) {
|
|
106
|
+
const auto& f = frames_[i];
|
|
107
|
+
std::array<uint8_t, 12> frameHeader{};
|
|
108
|
+
put(frameHeader.data(), f.bytes.size(), 4);
|
|
109
|
+
put(frameHeader.data() + 4, uint32_t(f.pts - frames_[0].pts), 8);
|
|
110
|
+
if (std::fwrite(frameHeader.data(), 1, frameHeader.size(), file) != frameHeader.size() ||
|
|
111
|
+
std::fwrite(f.bytes.data(), 1, f.bytes.size(), file) != f.bytes.size()) return false;
|
|
112
|
+
}
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
private:
|
|
117
|
+
static void put(uint8_t* dst, uint64_t value, unsigned length) {
|
|
118
|
+
for (unsigned i = 0; i < length; ++i) { dst[i] = uint8_t(value); value >>= 8; }
|
|
119
|
+
}
|
|
120
|
+
mutable std::mutex mutex_;
|
|
121
|
+
std::atomic<bool> requested_{false};
|
|
122
|
+
std::atomic<unsigned> workers_{0};
|
|
123
|
+
std::array<Frame, maxFrames> frames_;
|
|
124
|
+
unsigned count_ = 0, width_ = 0, height_ = 0;
|
|
125
|
+
size_t size_ = 0;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
inline Capture& capture() { static Capture instance; return instance; }
|
|
129
|
+
} // namespace gea::host::rtc::vp8
|
|
@@ -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
|