@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,170 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
#include <array>
|
|
5
|
+
#include <cstdint>
|
|
6
|
+
#include <optional>
|
|
7
|
+
#include <span>
|
|
8
|
+
#include <cstring>
|
|
9
|
+
#include "host/video_pixels.h"
|
|
10
|
+
|
|
11
|
+
namespace gea::host::rtc::rtp {
|
|
12
|
+
|
|
13
|
+
inline uint16_t read16(const uint8_t* bytes) {
|
|
14
|
+
return (uint16_t(bytes[0]) << 8) | bytes[1];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
inline uint32_t read32(const uint8_t* bytes) {
|
|
18
|
+
return (uint32_t(read16(bytes)) << 16) | read16(bytes + 2);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
inline void write32(uint8_t* bytes, uint32_t value) {
|
|
22
|
+
bytes[0] = uint8_t(value >> 24);
|
|
23
|
+
bytes[1] = uint8_t(value >> 16);
|
|
24
|
+
bytes[2] = uint8_t(value >> 8);
|
|
25
|
+
bytes[3] = uint8_t(value);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
struct Packet {
|
|
29
|
+
std::span<const uint8_t> payload;
|
|
30
|
+
uint32_t ssrc = 0;
|
|
31
|
+
uint32_t timestamp = 0;
|
|
32
|
+
uint16_t sequence = 0;
|
|
33
|
+
uint8_t payloadType = 0;
|
|
34
|
+
bool marker = false;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// The caller MUST authenticate/decrypt SRTP first. Bounds cover RFC 3550's
|
|
38
|
+
// CSRC list, optional extension block and padding before exposing a payload.
|
|
39
|
+
inline std::optional<Packet> parse(std::span<const uint8_t> bytes) {
|
|
40
|
+
if (bytes.size() < 12 || bytes[0] >> 6 != 2) return std::nullopt;
|
|
41
|
+
// RFC 5761: this range is reserved for RTCP on a multiplexed transport.
|
|
42
|
+
if (bytes[1] >= 192 && bytes[1] <= 223) return std::nullopt;
|
|
43
|
+
size_t offset = 12 + 4 * (bytes[0] & 15);
|
|
44
|
+
if (offset > bytes.size()) return std::nullopt;
|
|
45
|
+
if (bytes[0] & 0x10) {
|
|
46
|
+
if (bytes.size() - offset < 4) return std::nullopt;
|
|
47
|
+
const size_t length = 4 + size_t(read16(bytes.data() + offset + 2)) * 4;
|
|
48
|
+
if (length > bytes.size() - offset) return std::nullopt;
|
|
49
|
+
offset += length;
|
|
50
|
+
}
|
|
51
|
+
size_t end = bytes.size();
|
|
52
|
+
if (bytes[0] & 0x20) {
|
|
53
|
+
const auto padding = bytes.back();
|
|
54
|
+
if (!padding || padding > end - offset) return std::nullopt;
|
|
55
|
+
end -= padding;
|
|
56
|
+
}
|
|
57
|
+
if (offset == end) return std::nullopt;
|
|
58
|
+
return Packet{bytes.subspan(offset, end - offset), read32(bytes.data() + 8),
|
|
59
|
+
read32(bytes.data() + 4), read16(bytes.data() + 2),
|
|
60
|
+
uint8_t(bytes[1] & 127), bool(bytes[1] & 128)};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// No delay for in-order traffic. Only a sequence gap starts a bounded wait.
|
|
64
|
+
// The callback runs synchronously; its packet spans must not escape it.
|
|
65
|
+
class Reorder {
|
|
66
|
+
public:
|
|
67
|
+
static constexpr size_t capacity = 64;
|
|
68
|
+
static constexpr size_t maxPacketBytes = 4096;
|
|
69
|
+
static constexpr uint64_t gapWaitMs = 30;
|
|
70
|
+
|
|
71
|
+
void reset() {
|
|
72
|
+
for (auto& slot : slots_) slot.clear();
|
|
73
|
+
queued_ = 0;
|
|
74
|
+
initialized_ = false;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
template<class Deliver>
|
|
78
|
+
void push(std::span<const uint8_t> bytes, uint64_t nowMs, Deliver&& deliver) {
|
|
79
|
+
if (bytes.size() > maxPacketBytes) return;
|
|
80
|
+
const auto packet = parse(bytes);
|
|
81
|
+
if (!packet) return;
|
|
82
|
+
if (!initialized_) {
|
|
83
|
+
next_ = packet->sequence;
|
|
84
|
+
initialized_ = true;
|
|
85
|
+
}
|
|
86
|
+
const auto distance = uint16_t(packet->sequence - next_);
|
|
87
|
+
if (distance >= 0x8000) return; // Duplicate or late, including sequence wrap.
|
|
88
|
+
if (distance >= capacity) {
|
|
89
|
+
// Decoder overload or a large network loss: discard stale queued packets.
|
|
90
|
+
// The downstream assembler sees the sequence gap and requires a keyframe.
|
|
91
|
+
for (auto& slot : slots_) slot.clear();
|
|
92
|
+
queued_ = 0;
|
|
93
|
+
next_ = packet->sequence;
|
|
94
|
+
}
|
|
95
|
+
if (packet->sequence == next_) {
|
|
96
|
+
// The normal path is already ordered: deliver the caller's authenticated
|
|
97
|
+
// span directly, without copying or reserving a full packet window.
|
|
98
|
+
++next_;
|
|
99
|
+
deliver(*packet);
|
|
100
|
+
poll(nowMs, deliver);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
auto& slot = slots_[packet->sequence % capacity];
|
|
104
|
+
if (!slot.bytes.empty()) return;
|
|
105
|
+
try { slot.bytes.assign(bytes.begin(), bytes.end()); }
|
|
106
|
+
catch (const std::bad_alloc&) { return; } // Packet loss, never a device abort.
|
|
107
|
+
slot.arrivedMs = nowMs;
|
|
108
|
+
++queued_;
|
|
109
|
+
poll(nowMs, deliver);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
template<class Deliver>
|
|
113
|
+
void poll(uint64_t nowMs, Deliver&& deliver) {
|
|
114
|
+
if (!initialized_) return;
|
|
115
|
+
// Almost all packets are already ordered. Do not scan 63 PSRAM-backed
|
|
116
|
+
// slots after every audio/video packet (or every transport timer tick)
|
|
117
|
+
// when there is no outstanding sequence gap.
|
|
118
|
+
while (queued_) {
|
|
119
|
+
auto& slot = slots_[next_ % capacity];
|
|
120
|
+
if (!slot.bytes.empty()) {
|
|
121
|
+
// Release the temporary PSRAM allocation after synchronous delivery;
|
|
122
|
+
// retaining capacity per sequence slot grows to half a megabyte across
|
|
123
|
+
// the two tracks even when no packets are currently out of order.
|
|
124
|
+
Slot::Bytes bytes;
|
|
125
|
+
bytes.swap(slot.bytes);
|
|
126
|
+
--queued_;
|
|
127
|
+
const auto packet = parse(std::span(bytes.data(), bytes.size()));
|
|
128
|
+
// The bounded window guarantees that this slot belongs to next_.
|
|
129
|
+
++next_;
|
|
130
|
+
if (packet) deliver(*packet);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
size_t nearest = capacity;
|
|
134
|
+
uint64_t earliest = nowMs;
|
|
135
|
+
for (size_t distance = 1; distance < capacity; ++distance) {
|
|
136
|
+
const auto& queued = slots_[uint16_t(next_ + distance) % capacity];
|
|
137
|
+
if (queued.bytes.empty()) continue;
|
|
138
|
+
if (nearest == capacity) nearest = distance;
|
|
139
|
+
if (queued.arrivedMs < earliest) earliest = queued.arrivedMs;
|
|
140
|
+
}
|
|
141
|
+
if (nearest == capacity || nowMs - earliest < gapWaitMs) return;
|
|
142
|
+
next_ = uint16_t(next_ + nearest);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
private:
|
|
147
|
+
struct Slot {
|
|
148
|
+
// Only genuinely reordered packets own a buffer. The allocator explicitly
|
|
149
|
+
// uses PSRAM on ESP32, including for small packets below IDF's threshold.
|
|
150
|
+
using Bytes = std::vector<uint8_t, media::VideoAllocator<uint8_t>>;
|
|
151
|
+
Bytes bytes;
|
|
152
|
+
uint64_t arrivedMs = 0;
|
|
153
|
+
void clear() { Bytes{}.swap(bytes); arrivedMs = 0; }
|
|
154
|
+
};
|
|
155
|
+
std::array<Slot, capacity> slots_;
|
|
156
|
+
size_t queued_ = 0;
|
|
157
|
+
uint16_t next_ = 0;
|
|
158
|
+
bool initialized_ = false;
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
// RFC 4585 picture loss indication. Caller protects this with SRTCP before
|
|
162
|
+
// transmission. Reduced-size RTCP must have been negotiated for standalone PLI.
|
|
163
|
+
inline std::array<uint8_t, 12> pictureLossIndication(uint32_t sender, uint32_t media) {
|
|
164
|
+
std::array<uint8_t, 12> bytes{0x81, 206, 0, 2};
|
|
165
|
+
write32(bytes.data() + 4, sender);
|
|
166
|
+
write32(bytes.data() + 8, media);
|
|
167
|
+
return bytes;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
} // namespace gea::host::rtc::rtp
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
#include <string>
|
|
4
|
+
#include <string_view>
|
|
5
|
+
#include <vector>
|
|
6
|
+
|
|
7
|
+
namespace gea::host::rtc {
|
|
8
|
+
// RFC 7587: the RTP clock stays 48000/2. These receiver hints describe the
|
|
9
|
+
// actual PCM sink, avoiding decoding bandwidth the device cannot reproduce.
|
|
10
|
+
inline std::string opusPcmCapabilities(const std::string &sdp, int rate, int channels) {
|
|
11
|
+
const std::string newline = sdp.find("\r\n") != std::string::npos ? "\r\n" : "\n";
|
|
12
|
+
const bool terminated = !sdp.empty() && sdp.back() == '\n';
|
|
13
|
+
std::vector<std::string> lines;
|
|
14
|
+
for (std::size_t at = 0; at < sdp.size();) {
|
|
15
|
+
auto end = sdp.find('\n', at);
|
|
16
|
+
auto line = sdp.substr(at, end == std::string::npos ? end : end - at);
|
|
17
|
+
if (!line.empty() && line.back() == '\r') line.pop_back();
|
|
18
|
+
lines.push_back(std::move(line));
|
|
19
|
+
if (end == std::string::npos) break;
|
|
20
|
+
at = end + 1;
|
|
21
|
+
}
|
|
22
|
+
for (std::size_t start = 0; start < lines.size();) {
|
|
23
|
+
if (lines[start].rfind("m=audio ", 0) != 0) { ++start; continue; }
|
|
24
|
+
std::size_t end = start + 1;
|
|
25
|
+
while (end < lines.size() && lines[end].rfind("m=", 0) != 0) ++end;
|
|
26
|
+
std::vector<std::string> payloads;
|
|
27
|
+
for (auto i = start + 1; i < end; ++i) {
|
|
28
|
+
if (lines[i].rfind("a=rtpmap:", 0) != 0) continue;
|
|
29
|
+
const auto space = lines[i].find(' ');
|
|
30
|
+
if (space != std::string::npos && lines[i].substr(space + 1) == "opus/48000/2")
|
|
31
|
+
payloads.push_back(lines[i].substr(9, space - 9));
|
|
32
|
+
}
|
|
33
|
+
for (const auto &payload : payloads) {
|
|
34
|
+
const auto prefix = "a=fmtp:" + payload + " ";
|
|
35
|
+
std::size_t index = start + 1;
|
|
36
|
+
while (index < end && lines[index].rfind(prefix, 0) != 0) ++index;
|
|
37
|
+
std::string parameters;
|
|
38
|
+
if (index < end) {
|
|
39
|
+
const auto existing = lines[index].substr(prefix.size());
|
|
40
|
+
for (std::size_t at = 0; at < existing.size();) {
|
|
41
|
+
const auto next = existing.find(';', at);
|
|
42
|
+
auto parameter = existing.substr(at, next == std::string::npos ? next : next - at);
|
|
43
|
+
const auto first = parameter.find_first_not_of(" \t");
|
|
44
|
+
if (first != std::string::npos) parameter.erase(0, first);
|
|
45
|
+
const auto equal = parameter.find('=');
|
|
46
|
+
auto key = parameter.substr(0, equal);
|
|
47
|
+
const auto last = key.find_last_not_of(" \t");
|
|
48
|
+
if (last != std::string::npos) key.resize(last + 1);
|
|
49
|
+
if (!parameter.empty() && key != "maxplaybackrate" && key != "sprop-maxcapturerate" &&
|
|
50
|
+
key != "stereo" && key != "sprop-stereo") parameters += parameter + ";";
|
|
51
|
+
if (next == std::string::npos) break;
|
|
52
|
+
at = next + 1;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
parameters += "maxplaybackrate=" + std::to_string(rate) +
|
|
56
|
+
";sprop-maxcapturerate=" + std::to_string(rate) +
|
|
57
|
+
";stereo=" + (channels == 1 ? "0" : "1") +
|
|
58
|
+
";sprop-stereo=" + (channels == 1 ? "0" : "1");
|
|
59
|
+
if (index < end) lines[index] = prefix + parameters;
|
|
60
|
+
else { lines.insert(lines.begin() + end, prefix + parameters); ++end; }
|
|
61
|
+
}
|
|
62
|
+
start = end;
|
|
63
|
+
}
|
|
64
|
+
std::string result;
|
|
65
|
+
for (std::size_t i = 0; i < lines.size(); ++i) {
|
|
66
|
+
result += lines[i];
|
|
67
|
+
if (i + 1 < lines.size() || terminated) result += newline;
|
|
68
|
+
}
|
|
69
|
+
return result;
|
|
70
|
+
}
|
|
71
|
+
} // namespace gea::host::rtc
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
#include "host/media.h"
|
|
4
|
+
#include "host/rtc_ice_candidate.h"
|
|
5
|
+
#include <cstdint>
|
|
6
|
+
#include <functional>
|
|
7
|
+
#include <memory>
|
|
8
|
+
#include <optional>
|
|
9
|
+
#include <stdexcept>
|
|
10
|
+
#include <string>
|
|
11
|
+
#include <utility>
|
|
12
|
+
#include <vector>
|
|
13
|
+
|
|
14
|
+
namespace gea::host {
|
|
15
|
+
namespace rtc {
|
|
16
|
+
struct PeerLifetime;
|
|
17
|
+
std::shared_ptr<PeerLifetime> retain_peer(uint32_t handle);
|
|
18
|
+
std::shared_ptr<PeerLifetime> retain_object(uint32_t handle);
|
|
19
|
+
}
|
|
20
|
+
struct RTCIceServer {
|
|
21
|
+
std::vector<std::string> urls;
|
|
22
|
+
std::string username;
|
|
23
|
+
std::string credential;
|
|
24
|
+
};
|
|
25
|
+
struct RTCConfiguration {
|
|
26
|
+
std::vector<RTCIceServer> iceServers;
|
|
27
|
+
std::string iceTransportPolicy = "all";
|
|
28
|
+
};
|
|
29
|
+
struct RTCSessionDescription {
|
|
30
|
+
std::string type;
|
|
31
|
+
std::string sdp;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// Description getters preserve object identity until the peer replaces its
|
|
35
|
+
// SDP snapshot. JSON dictionaries are separate, mutable copies.
|
|
36
|
+
class RTCSessionDescriptionObject {
|
|
37
|
+
public:
|
|
38
|
+
RTCSessionDescriptionObject() = default;
|
|
39
|
+
explicit RTCSessionDescriptionObject(RTCSessionDescription fields)
|
|
40
|
+
: fields_(std::make_shared<RTCSessionDescription>(std::move(fields))) { validateType(fields_->type); }
|
|
41
|
+
std::string type() const { return fields().type; }
|
|
42
|
+
std::string sdp() const { return fields().sdp; }
|
|
43
|
+
const RTCSessionDescription &fields() const {
|
|
44
|
+
if (!fields_) throw std::logic_error("null RTCSessionDescription");
|
|
45
|
+
return *fields_;
|
|
46
|
+
}
|
|
47
|
+
explicit operator bool() const { return static_cast<bool>(fields_); }
|
|
48
|
+
bool operator==(const RTCSessionDescriptionObject &other) const { return fields_ == other.fields_; }
|
|
49
|
+
bool operator==(std::nullptr_t) const { return !fields_; }
|
|
50
|
+
|
|
51
|
+
private:
|
|
52
|
+
static void validateType(const std::string &type) {
|
|
53
|
+
if (type != "offer" && type != "answer" && type != "pranswer" && type != "rollback")
|
|
54
|
+
throw std::invalid_argument("invalid RTCSessionDescription type");
|
|
55
|
+
}
|
|
56
|
+
std::shared_ptr<const RTCSessionDescription> fields_;
|
|
57
|
+
};
|
|
58
|
+
struct RTCDataChannelInit {
|
|
59
|
+
bool ordered = true;
|
|
60
|
+
std::optional<double> maxPacketLifeTime;
|
|
61
|
+
std::optional<double> maxRetransmits;
|
|
62
|
+
std::string protocol;
|
|
63
|
+
bool negotiated = false;
|
|
64
|
+
std::optional<double> id;
|
|
65
|
+
};
|
|
66
|
+
struct RTCRtpTransceiverInit {
|
|
67
|
+
std::string direction = "sendrecv";
|
|
68
|
+
};
|
|
69
|
+
class RTCDataChannel {
|
|
70
|
+
public:
|
|
71
|
+
uint32_t nativeHandle = 0;
|
|
72
|
+
RTCDataChannel() = default;
|
|
73
|
+
explicit RTCDataChannel(uint32_t handle) : nativeHandle(handle), lifetime_(rtc::retain_object(handle)) {}
|
|
74
|
+
explicit operator bool() const { return nativeHandle != 0; }
|
|
75
|
+
bool operator==(std::nullptr_t) const { return nativeHandle == 0; }
|
|
76
|
+
bool operator!=(std::nullptr_t) const { return nativeHandle != 0; }
|
|
77
|
+
bool operator==(const RTCDataChannel& other) const { return nativeHandle == other.nativeHandle; }
|
|
78
|
+
std::string label() const;
|
|
79
|
+
std::string protocol() const;
|
|
80
|
+
std::string readyState() const;
|
|
81
|
+
std::string binaryType() const;
|
|
82
|
+
void setBinaryType(const std::string&) const;
|
|
83
|
+
bool ordered() const;
|
|
84
|
+
std::optional<double> id() const;
|
|
85
|
+
std::optional<double> maxRetransmits() const;
|
|
86
|
+
std::optional<double> maxPacketLifeTime() const;
|
|
87
|
+
double bufferedAmount() const;
|
|
88
|
+
double bufferedAmountLowThreshold() const;
|
|
89
|
+
void setBufferedAmountLowThreshold(double) const;
|
|
90
|
+
void send(const std::string&) const;
|
|
91
|
+
void send(const std::vector<uint8_t>&) const;
|
|
92
|
+
void close() const;
|
|
93
|
+
private:
|
|
94
|
+
std::shared_ptr<rtc::PeerLifetime> lifetime_;
|
|
95
|
+
};
|
|
96
|
+
class RTCRtpSender {
|
|
97
|
+
public:
|
|
98
|
+
uint32_t nativeHandle = 0;
|
|
99
|
+
RTCRtpSender() = default;
|
|
100
|
+
explicit RTCRtpSender(uint32_t handle) : nativeHandle(handle), lifetime_(rtc::retain_object(handle)) {}
|
|
101
|
+
std::optional<MediaStreamTrack> track() const;
|
|
102
|
+
void replaceTrack(MediaStreamTrack) const;
|
|
103
|
+
void replaceTrack() const;
|
|
104
|
+
private:
|
|
105
|
+
std::shared_ptr<rtc::PeerLifetime> lifetime_;
|
|
106
|
+
};
|
|
107
|
+
class RTCRtpReceiver {
|
|
108
|
+
public:
|
|
109
|
+
uint32_t nativeHandle = 0;
|
|
110
|
+
RTCRtpReceiver() = default;
|
|
111
|
+
explicit RTCRtpReceiver(uint32_t handle) : nativeHandle(handle), lifetime_(rtc::retain_object(handle)) {}
|
|
112
|
+
std::optional<MediaStreamTrack> track() const;
|
|
113
|
+
private:
|
|
114
|
+
std::shared_ptr<rtc::PeerLifetime> lifetime_;
|
|
115
|
+
};
|
|
116
|
+
class RTCRtpTransceiver {
|
|
117
|
+
public:
|
|
118
|
+
uint32_t nativeHandle = 0;
|
|
119
|
+
RTCRtpTransceiver() = default;
|
|
120
|
+
explicit RTCRtpTransceiver(uint32_t handle) : nativeHandle(handle), lifetime_(rtc::retain_object(handle)) {}
|
|
121
|
+
RTCRtpSender sender() const { return RTCRtpSender(nativeHandle); }
|
|
122
|
+
RTCRtpReceiver receiver() const { return RTCRtpReceiver(nativeHandle); }
|
|
123
|
+
std::optional<std::string> mid() const;
|
|
124
|
+
std::string direction() const;
|
|
125
|
+
void setDirection(const std::string&) const;
|
|
126
|
+
std::optional<std::string> currentDirection() const;
|
|
127
|
+
bool stopped() const;
|
|
128
|
+
void stop() const;
|
|
129
|
+
private:
|
|
130
|
+
std::shared_ptr<rtc::PeerLifetime> lifetime_;
|
|
131
|
+
};
|
|
132
|
+
class RTCTrackEvent {
|
|
133
|
+
public:
|
|
134
|
+
RTCTrackEvent() = default;
|
|
135
|
+
RTCTrackEvent(MediaStreamTrack track, RTCRtpTransceiver transceiver, std::vector<MediaStream> streams)
|
|
136
|
+
: fields_(std::make_shared<const Fields>(Fields{track, transceiver, std::move(streams)})) {}
|
|
137
|
+
MediaStreamTrack track() const { return fields().track; }
|
|
138
|
+
RTCRtpTransceiver transceiver() const { return fields().transceiver; }
|
|
139
|
+
RTCRtpReceiver receiver() const { return transceiver().receiver(); }
|
|
140
|
+
const std::vector<MediaStream>& streams() const { return fields().streams; }
|
|
141
|
+
bool operator==(const RTCTrackEvent& other) const { return fields_ == other.fields_; }
|
|
142
|
+
private:
|
|
143
|
+
struct Fields {
|
|
144
|
+
MediaStreamTrack track;
|
|
145
|
+
RTCRtpTransceiver transceiver;
|
|
146
|
+
std::vector<MediaStream> streams;
|
|
147
|
+
};
|
|
148
|
+
const Fields& fields() const {
|
|
149
|
+
if (!fields_) throw std::logic_error("null RTCTrackEvent");
|
|
150
|
+
return *fields_;
|
|
151
|
+
}
|
|
152
|
+
std::shared_ptr<const Fields> fields_;
|
|
153
|
+
};
|
|
154
|
+
namespace rtc {
|
|
155
|
+
struct DataChannelCallbacks {
|
|
156
|
+
std::function<void()> on_open, on_close, on_buffered_amount_low;
|
|
157
|
+
std::function<void(const std::string&)> on_error;
|
|
158
|
+
std::function<void(const std::vector<uint8_t>&, bool)> on_message;
|
|
159
|
+
};
|
|
160
|
+
DataChannelCallbacks& channel_callbacks(uint32_t channel);
|
|
161
|
+
void enqueue_channel_open(uint32_t peer, const std::string& label, uint16_t stream);
|
|
162
|
+
void enqueue_channel_close(uint32_t peer, uint16_t stream);
|
|
163
|
+
void enqueue_channel_message(uint32_t peer, uint16_t stream, const uint8_t* bytes, size_t size, bool text);
|
|
164
|
+
RTCConfiguration configuration(uint32_t peer);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
@@ -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
|