@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
package/include/host/rtc.h
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
#pragma once
|
|
3
3
|
|
|
4
4
|
#include "host/media.h"
|
|
5
|
+
#include "host/rtc_types.h"
|
|
5
6
|
|
|
7
|
+
#include <atomic>
|
|
6
8
|
#include <cstddef>
|
|
7
9
|
#include <cstdint>
|
|
8
10
|
#include <functional>
|
|
@@ -14,6 +16,15 @@
|
|
|
14
16
|
|
|
15
17
|
namespace gea::host {
|
|
16
18
|
|
|
19
|
+
namespace rtc {
|
|
20
|
+
struct DescriptionResult {
|
|
21
|
+
std::atomic<bool> ready{false};
|
|
22
|
+
std::string sdp, error;
|
|
23
|
+
};
|
|
24
|
+
using DescriptionCallback = std::function<void(const std::string&, const std::string&)>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
17
28
|
using NativeRtcPeerHandle = std::uint32_t;
|
|
18
29
|
|
|
19
30
|
class RTCPeerConnectionOnIceCandidateProperty {
|
|
@@ -54,39 +65,51 @@ class RTCPeerConnection {
|
|
|
54
65
|
mutable RTCPeerConnectionOnConnectionStateProperty onconnectionstatechange;
|
|
55
66
|
mutable RTCPeerConnectionOnConnectionStateProperty oniceconnectionstatechange;
|
|
56
67
|
|
|
57
|
-
|
|
68
|
+
RTCPeerConnection() = default;
|
|
58
69
|
explicit RTCPeerConnection(NativeRtcPeerHandle h)
|
|
59
70
|
: nativeHandle(h),
|
|
60
71
|
onicecandidate(h),
|
|
61
72
|
ontrack(h),
|
|
62
73
|
onconnectionstatechange(h),
|
|
63
|
-
oniceconnectionstatechange(h) {}
|
|
64
|
-
explicit RTCPeerConnection(double h)
|
|
65
|
-
: nativeHandle(static_cast<NativeRtcPeerHandle>(h)),
|
|
66
|
-
onicecandidate(nativeHandle),
|
|
67
|
-
ontrack(nativeHandle),
|
|
68
|
-
onconnectionstatechange(nativeHandle),
|
|
69
|
-
oniceconnectionstatechange(nativeHandle) {}
|
|
74
|
+
oniceconnectionstatechange(h), lifetime_(rtc::retain_peer(h)) {}
|
|
75
|
+
explicit RTCPeerConnection(double h) : RTCPeerConnection(static_cast<NativeRtcPeerHandle>(h)) {}
|
|
70
76
|
constexpr operator double() const { return static_cast<double>(nativeHandle); }
|
|
71
77
|
|
|
72
78
|
std::string connectionState() const;
|
|
73
79
|
std::string iceConnectionState() const;
|
|
74
|
-
|
|
80
|
+
RTCRtpSender addTrack(MediaStreamTrack track, MediaStream stream = {}) const;
|
|
81
|
+
RTCRtpSender addTrack(MediaStreamTrack track, const std::vector<MediaStream>& streams) const;
|
|
82
|
+
void removeTrack(RTCRtpSender sender) const;
|
|
83
|
+
RTCRtpTransceiver addTransceiver(MediaStreamTrack, const RTCRtpTransceiverInit& = {}) const;
|
|
84
|
+
RTCRtpTransceiver addTransceiver(const std::string& kind, const RTCRtpTransceiverInit& = {}) const;
|
|
85
|
+
std::vector<RTCRtpTransceiver> getTransceivers() const;
|
|
86
|
+
std::vector<RTCRtpSender> getSenders() const;
|
|
87
|
+
std::vector<RTCRtpReceiver> getReceivers() const;
|
|
88
|
+
RTCDataChannel createDataChannel(const std::string&, const RTCDataChannelInit& = {}) const;
|
|
89
|
+
RTCConfiguration getConfiguration() const;
|
|
90
|
+
std::string signalingState() const;
|
|
91
|
+
std::optional<RTCSessionDescriptionObject> localDescription() const;
|
|
92
|
+
std::optional<RTCSessionDescriptionObject> remoteDescription() const;
|
|
75
93
|
std::string createOffer() const;
|
|
76
94
|
std::string createAnswer() const;
|
|
95
|
+
void requestDescription(bool offer, rtc::DescriptionCallback callback) const;
|
|
77
96
|
void setLocalDescription(const std::string &sdp_type, const std::string &sdp) const;
|
|
78
97
|
void setRemoteDescription(const std::string &sdp_type, const std::string &sdp) const;
|
|
79
98
|
void addIceCandidate(const std::string &candidate, const std::string &sdp_mid, double sdp_mline_index) const;
|
|
80
99
|
void close() const;
|
|
100
|
+
private:
|
|
101
|
+
std::shared_ptr<rtc::PeerLifetime> lifetime_;
|
|
81
102
|
};
|
|
82
103
|
|
|
83
104
|
namespace rtc {
|
|
84
105
|
|
|
85
106
|
struct CallbackTable {
|
|
86
107
|
std::function<void(const std::string &, const std::string &, int)> on_ice_candidate;
|
|
87
|
-
std::function<void(
|
|
108
|
+
std::function<void(RTCTrackEvent)> on_track;
|
|
88
109
|
std::function<void()> on_connection_state_change;
|
|
89
110
|
std::function<void()> on_ice_connection_state_change;
|
|
111
|
+
std::function<void()> on_signaling_state_change, on_negotiation_needed;
|
|
112
|
+
std::function<void(RTCDataChannel)> on_data_channel;
|
|
90
113
|
};
|
|
91
114
|
|
|
92
115
|
std::unordered_map<NativeRtcPeerHandle, CallbackTable> &callbackTable();
|
|
@@ -94,20 +117,15 @@ std::unordered_map<NativeRtcPeerHandle, CallbackTable> &callbackTable();
|
|
|
94
117
|
NativeRtcPeerHandle create_handle();
|
|
95
118
|
NativeRtcPeerHandle create_handle_with_config(const std::string &ice_servers_json);
|
|
96
119
|
|
|
97
|
-
|
|
98
|
-
//
|
|
99
|
-
// call site passes a struct; for now we ignore the contents and fall back
|
|
100
|
-
// to the no-arg form. A later step can read iceServers off the record and
|
|
101
|
-
// route to create_handle_with_config.
|
|
102
|
-
template <typename Config>
|
|
103
|
-
NativeRtcPeerHandle create_handle(const Config &) { return create_handle(); }
|
|
120
|
+
NativeRtcPeerHandle create_handle(const RTCConfiguration& config);
|
|
121
|
+
// Generated record adaptation is supplied by the compiler's typed host adapter.
|
|
104
122
|
void destroy_handle(NativeRtcPeerHandle handle);
|
|
105
123
|
|
|
106
124
|
void runCallbacks();
|
|
107
125
|
|
|
108
126
|
// Producer hooks called from platform code (libpeer callbacks on ESP, tests on host).
|
|
109
127
|
void enqueue_ice_candidate(NativeRtcPeerHandle h, const std::string &candidate, const std::string &sdp_mid, int idx);
|
|
110
|
-
void enqueue_track(NativeRtcPeerHandle h, NativeMediaTrackHandle track);
|
|
128
|
+
void enqueue_track(NativeRtcPeerHandle h, NativeMediaTrackHandle track, std::vector<MediaStream> streams = {});
|
|
111
129
|
void enqueue_connection_state_change(NativeRtcPeerHandle h, const std::string &new_state);
|
|
112
130
|
void enqueue_ice_connection_state_change(NativeRtcPeerHandle h, const std::string &new_state);
|
|
113
131
|
|
|
@@ -147,11 +165,11 @@ inline const RTCPeerConnectionOnTrackProperty &RTCPeerConnectionOnTrackProperty:
|
|
|
147
165
|
return *this;
|
|
148
166
|
}
|
|
149
167
|
auto callable = cb.callable;
|
|
150
|
-
rtc::callbackTable()[handle].on_track = [callable](
|
|
168
|
+
rtc::callbackTable()[handle].on_track = [callable](RTCTrackEvent trackEvent) {
|
|
151
169
|
gea_cpp_value event;
|
|
152
170
|
event.kind = gea_cpp_value::kind_t::record;
|
|
153
171
|
event.entries = std::make_shared<std::vector<std::pair<std::string, gea_cpp_value>>>();
|
|
154
|
-
event.record_set_literal("track", gea_cpp_value(static_cast<double>(
|
|
172
|
+
event.record_set_literal("track", gea_cpp_value(static_cast<double>(trackEvent.track().nativeHandle)));
|
|
155
173
|
(*callable)(std::vector<gea_cpp_value>{event});
|
|
156
174
|
};
|
|
157
175
|
return *this;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
#include "host/h264_layout.h"
|
|
4
|
+
#include "host/media.h"
|
|
5
|
+
#include "esp_h264_dec_sw.h"
|
|
6
|
+
#include <memory>
|
|
7
|
+
#include <limits>
|
|
8
|
+
|
|
9
|
+
namespace gea::host::rtc::h264 {
|
|
10
|
+
|
|
11
|
+
// Owned by the decode worker, never by the RTP/audio callback thread.
|
|
12
|
+
class Decoder {
|
|
13
|
+
public:
|
|
14
|
+
Decoder() = default;
|
|
15
|
+
Decoder(const Decoder&) = delete;
|
|
16
|
+
Decoder& operator=(const Decoder&) = delete;
|
|
17
|
+
~Decoder() { reset(); }
|
|
18
|
+
|
|
19
|
+
void reset() {
|
|
20
|
+
if (decoder_) { esp_h264_dec_close(decoder_); esp_h264_dec_del(decoder_); }
|
|
21
|
+
decoder_ = nullptr;
|
|
22
|
+
parameters_ = nullptr;
|
|
23
|
+
metadata_ = {};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
std::unique_ptr<media::VideoFrame> decode(const uint8_t* bytes, size_t length, uint32_t timestamp) {
|
|
27
|
+
if (!length || length > std::numeric_limits<uint32_t>::max() ||
|
|
28
|
+
!inspectAccessUnit(bytes, length, metadata_) || !open()) {
|
|
29
|
+
reset(); return nullptr;
|
|
30
|
+
}
|
|
31
|
+
esp_h264_dec_in_frame_t input{};
|
|
32
|
+
input.raw_data.buffer = const_cast<uint8_t*>(bytes);
|
|
33
|
+
input.raw_data.len = static_cast<uint32_t>(length);
|
|
34
|
+
std::unique_ptr<media::VideoFrame> latest;
|
|
35
|
+
while (input.raw_data.len) {
|
|
36
|
+
input.consume = 0;
|
|
37
|
+
esp_h264_dec_out_frame_t decoded{};
|
|
38
|
+
const auto rc = esp_h264_dec_process(decoder_, &input, &decoded);
|
|
39
|
+
if (rc != ESP_H264_ERR_OK || !input.consume || input.consume > input.raw_data.len) {
|
|
40
|
+
reset(); return nullptr;
|
|
41
|
+
}
|
|
42
|
+
if (decoded.out_size) {
|
|
43
|
+
esp_h264_resolution_t resolution{};
|
|
44
|
+
const auto expected = size_t(metadata_.width) * metadata_.height * 3 / 2;
|
|
45
|
+
if (esp_h264_dec_get_resolution(parameters_, &resolution) != ESP_H264_ERR_OK || !decoded.outbuf ||
|
|
46
|
+
resolution.width != metadata_.width || resolution.height != metadata_.height || decoded.out_size != expected) {
|
|
47
|
+
reset(); return nullptr;
|
|
48
|
+
}
|
|
49
|
+
latest = std::make_unique<media::VideoFrame>();
|
|
50
|
+
latest->width = metadata_.visibleWidth;
|
|
51
|
+
latest->height = metadata_.visibleHeight;
|
|
52
|
+
latest->timestampMs = timestamp;
|
|
53
|
+
latest->rgb565.resize(size_t(latest->width) * latest->height);
|
|
54
|
+
convertI420(decoded.outbuf, metadata_, latest->rgb565.data());
|
|
55
|
+
}
|
|
56
|
+
input.raw_data.buffer += input.consume;
|
|
57
|
+
input.raw_data.len -= input.consume;
|
|
58
|
+
}
|
|
59
|
+
return latest;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private:
|
|
63
|
+
bool open() {
|
|
64
|
+
if (decoder_) return true;
|
|
65
|
+
esp_h264_dec_cfg_sw_t config{};
|
|
66
|
+
config.pic_type = ESP_H264_RAW_FMT_I420;
|
|
67
|
+
auto rc = esp_h264_dec_sw_new(&config, &decoder_);
|
|
68
|
+
if (rc == ESP_H264_ERR_OK) rc = esp_h264_dec_open(decoder_);
|
|
69
|
+
if (rc == ESP_H264_ERR_OK) rc = esp_h264_dec_sw_get_param_hd(decoder_, ¶meters_);
|
|
70
|
+
return rc == ESP_H264_ERR_OK;
|
|
71
|
+
}
|
|
72
|
+
esp_h264_dec_handle_t decoder_ = nullptr;
|
|
73
|
+
esp_h264_dec_param_sw_handle_t parameters_ = nullptr;
|
|
74
|
+
Metadata metadata_;
|
|
75
|
+
};
|
|
76
|
+
} // namespace gea::host::rtc::h264
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
#include <cstdint>
|
|
4
|
+
#include <memory>
|
|
5
|
+
#include <optional>
|
|
6
|
+
#include <stdexcept>
|
|
7
|
+
#include <string>
|
|
8
|
+
#include <string_view>
|
|
9
|
+
#include <utility>
|
|
10
|
+
#include <vector>
|
|
11
|
+
|
|
12
|
+
namespace gea::host {
|
|
13
|
+
struct RTCIceCandidateInit {
|
|
14
|
+
std::string candidate;
|
|
15
|
+
std::optional<std::string> sdpMid;
|
|
16
|
+
std::optional<double> sdpMLineIndex;
|
|
17
|
+
std::optional<std::string> usernameFragment;
|
|
18
|
+
std::optional<std::string> relayProtocol;
|
|
19
|
+
std::optional<std::string> url;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
class RTCIceCandidate {
|
|
23
|
+
struct Fields {
|
|
24
|
+
RTCIceCandidateInit init;
|
|
25
|
+
std::optional<std::string> foundation, component, address, protocol, type, tcpType, relatedAddress;
|
|
26
|
+
std::optional<double> priority, port, relatedPort;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
public:
|
|
30
|
+
RTCIceCandidate() = default;
|
|
31
|
+
explicit RTCIceCandidate(RTCIceCandidateInit init) {
|
|
32
|
+
if (!init.sdpMid && !init.sdpMLineIndex)
|
|
33
|
+
throw std::invalid_argument("RTCIceCandidate requires sdpMid or sdpMLineIndex");
|
|
34
|
+
auto fields = std::make_shared<Fields>();
|
|
35
|
+
fields->init = std::move(init);
|
|
36
|
+
// Malformed candidate text is retained verbatim; none of its derived
|
|
37
|
+
// attributes may leak out of a partially successful parse.
|
|
38
|
+
Fields parsed;
|
|
39
|
+
if (parse(fields->init.candidate, parsed)) {
|
|
40
|
+
parsed.init = std::move(fields->init);
|
|
41
|
+
*fields = std::move(parsed);
|
|
42
|
+
}
|
|
43
|
+
fields_ = std::move(fields);
|
|
44
|
+
}
|
|
45
|
+
const RTCIceCandidateInit& fields() const { return data().init; }
|
|
46
|
+
std::string candidate() const { return fields().candidate; }
|
|
47
|
+
std::optional<std::string> sdpMid() const { return fields().sdpMid; }
|
|
48
|
+
std::optional<double> sdpMLineIndex() const { return fields().sdpMLineIndex; }
|
|
49
|
+
std::optional<std::string> usernameFragment() const { return fields().usernameFragment; }
|
|
50
|
+
std::optional<std::string> relayProtocol() const { return fields().relayProtocol; }
|
|
51
|
+
std::optional<std::string> url() const { return fields().url; }
|
|
52
|
+
#define GEA_ICE_GETTER(Type, Name) std::optional<Type> Name() const { return data().Name; }
|
|
53
|
+
GEA_ICE_GETTER(std::string, foundation)
|
|
54
|
+
GEA_ICE_GETTER(std::string, component)
|
|
55
|
+
GEA_ICE_GETTER(std::string, address)
|
|
56
|
+
GEA_ICE_GETTER(std::string, protocol)
|
|
57
|
+
GEA_ICE_GETTER(std::string, type)
|
|
58
|
+
GEA_ICE_GETTER(std::string, tcpType)
|
|
59
|
+
GEA_ICE_GETTER(std::string, relatedAddress)
|
|
60
|
+
GEA_ICE_GETTER(double, priority)
|
|
61
|
+
GEA_ICE_GETTER(double, port)
|
|
62
|
+
GEA_ICE_GETTER(double, relatedPort)
|
|
63
|
+
#undef GEA_ICE_GETTER
|
|
64
|
+
explicit operator bool() const { return static_cast<bool>(fields_); }
|
|
65
|
+
bool operator==(std::nullptr_t) const { return !fields_; }
|
|
66
|
+
bool operator==(const RTCIceCandidate& other) const { return fields_ == other.fields_; }
|
|
67
|
+
|
|
68
|
+
private:
|
|
69
|
+
const Fields& data() const {
|
|
70
|
+
if (!fields_) throw std::logic_error("null RTCIceCandidate");
|
|
71
|
+
return *fields_;
|
|
72
|
+
}
|
|
73
|
+
static bool unsignedNumber(std::string_view text, uint64_t max, double& result) {
|
|
74
|
+
if (text.empty()) return false;
|
|
75
|
+
uint64_t value = 0;
|
|
76
|
+
for (const char ch : text) {
|
|
77
|
+
if (ch < '0' || ch > '9') return false;
|
|
78
|
+
const auto digit = static_cast<uint64_t>(ch - '0');
|
|
79
|
+
if (value > max / 10 || (value == max / 10 && digit > max % 10)) return false;
|
|
80
|
+
value = value * 10 + digit;
|
|
81
|
+
}
|
|
82
|
+
result = static_cast<double>(value);
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
static std::string lower(std::string_view text) {
|
|
86
|
+
std::string out(text);
|
|
87
|
+
for (char& ch : out) if (ch >= 'A' && ch <= 'Z') ch += 'a' - 'A';
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
static bool parse(const std::string& text, Fields& result) {
|
|
91
|
+
if (!text.starts_with("candidate:")) return false;
|
|
92
|
+
std::vector<std::string_view> tokens;
|
|
93
|
+
const std::string_view source(text);
|
|
94
|
+
size_t start = 10;
|
|
95
|
+
for (size_t i = start; i <= source.size(); ++i) {
|
|
96
|
+
if (i < source.size() && source[i] != ' ') {
|
|
97
|
+
if (static_cast<unsigned char>(source[i]) < 0x21 || static_cast<unsigned char>(source[i]) > 0x7e) return false;
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (i == start) return false;
|
|
101
|
+
tokens.push_back(source.substr(start, i - start));
|
|
102
|
+
start = i + 1;
|
|
103
|
+
}
|
|
104
|
+
if (tokens.size() < 8 || tokens.size() % 2 != 0 || tokens[0].size() > 32) return false;
|
|
105
|
+
for (char ch : tokens[0])
|
|
106
|
+
if (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
|
|
107
|
+
(ch >= '0' && ch <= '9') || ch == '+' || ch == '/')) return false;
|
|
108
|
+
double component, priority, port;
|
|
109
|
+
if (!unsignedNumber(tokens[1], 2, component) || component == 0 ||
|
|
110
|
+
!unsignedNumber(tokens[3], UINT32_MAX, priority) || !unsignedNumber(tokens[5], UINT16_MAX, port)) return false;
|
|
111
|
+
const auto protocol = lower(tokens[2]);
|
|
112
|
+
const auto type = lower(tokens[7]);
|
|
113
|
+
if ((protocol != "udp" && protocol != "tcp") || lower(tokens[6]) != "typ" ||
|
|
114
|
+
(type != "host" && type != "srflx" && type != "prflx" && type != "relay")) return false;
|
|
115
|
+
result.foundation = tokens[0];
|
|
116
|
+
result.component = component == 1 ? "rtp" : "rtcp";
|
|
117
|
+
result.priority = priority;
|
|
118
|
+
result.address = tokens[4];
|
|
119
|
+
result.protocol = protocol;
|
|
120
|
+
result.port = port;
|
|
121
|
+
result.type = type;
|
|
122
|
+
for (size_t i = 8; i < tokens.size(); i += 2) {
|
|
123
|
+
const auto key = lower(tokens[i]);
|
|
124
|
+
if (key == "raddr") {
|
|
125
|
+
if (result.relatedAddress) return false;
|
|
126
|
+
result.relatedAddress = tokens[i + 1];
|
|
127
|
+
} else if (key == "rport") {
|
|
128
|
+
double relatedPort;
|
|
129
|
+
if (result.relatedPort || !unsignedNumber(tokens[i + 1], UINT16_MAX, relatedPort)) return false;
|
|
130
|
+
result.relatedPort = relatedPort;
|
|
131
|
+
} else if (key == "tcptype") {
|
|
132
|
+
const auto tcpType = lower(tokens[i + 1]);
|
|
133
|
+
if (result.tcpType || protocol != "tcp" || (tcpType != "active" && tcpType != "passive" && tcpType != "so")) return false;
|
|
134
|
+
result.tcpType = tcpType;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return protocol != "tcp" || result.tcpType.has_value();
|
|
138
|
+
}
|
|
139
|
+
std::shared_ptr<const Fields> fields_;
|
|
140
|
+
};
|
|
141
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
#if defined(ESP_PLATFORM) && __has_include("esp_opus_enc.h")
|
|
4
|
+
#define GEA_RTC_ESP_OPUS 1
|
|
5
|
+
#include "esp_opus_enc.h"
|
|
6
|
+
#include "esp_opus_dec.h"
|
|
7
|
+
#else
|
|
8
|
+
#include <opus.h>
|
|
9
|
+
#endif
|
|
10
|
+
#include <array>
|
|
11
|
+
#include <cstdint>
|
|
12
|
+
#include <stdexcept>
|
|
13
|
+
|
|
14
|
+
namespace gea::host::rtc {
|
|
15
|
+
// esp_peer transports encoded RTP payloads. The media-track boundary is PCM.
|
|
16
|
+
// Decode at the track's 16 kHz rate; Opus's RTP clock remains 48 kHz.
|
|
17
|
+
class OpusAudio {
|
|
18
|
+
public:
|
|
19
|
+
// Amortize SRTP/socket work across two 20 ms Opus frames. At 50 sends/sec
|
|
20
|
+
// the S3 spent ~11 ms encoding plus ~10 ms sending each 20 ms of capture,
|
|
21
|
+
// accumulating old microphone audio. This adds only 20 ms packetization.
|
|
22
|
+
static constexpr int sampleRate = 16000, frameDurationMs = 40;
|
|
23
|
+
static constexpr int frameSamples = sampleRate * frameDurationMs / 1000;
|
|
24
|
+
static constexpr int maxDecodeSamples = sampleRate * 120 / 1000;
|
|
25
|
+
enum class Application { LowDelay, Voice };
|
|
26
|
+
enum class Direction { Both, Encode, Decode };
|
|
27
|
+
explicit OpusAudio(Application application = Application::LowDelay,
|
|
28
|
+
Direction direction = Direction::Both) {
|
|
29
|
+
#if GEA_RTC_ESP_OPUS
|
|
30
|
+
if (direction != Direction::Decode) {
|
|
31
|
+
esp_opus_enc_config_t config{};
|
|
32
|
+
config.sample_rate = sampleRate;
|
|
33
|
+
config.channel = 1;
|
|
34
|
+
config.bits_per_sample = 16;
|
|
35
|
+
config.bitrate = 24000;
|
|
36
|
+
config.frame_duration = ESP_OPUS_ENC_FRAME_DURATION_40_MS;
|
|
37
|
+
// CELT-only low-delay mode avoids SILK's analysis work competing with
|
|
38
|
+
// AEC on the S3. The RTP codec, PCM rate and 24 kbps bitrate are unchanged.
|
|
39
|
+
config.application_mode = application == Application::LowDelay
|
|
40
|
+
? ESP_OPUS_ENC_APPLICATION_LOWDELAY : ESP_OPUS_ENC_APPLICATION_VOIP;
|
|
41
|
+
config.complexity = 0;
|
|
42
|
+
config.enable_vbr = true;
|
|
43
|
+
if (esp_opus_enc_open(&config, sizeof(config), &encoder) != ESP_AUDIO_ERR_OK)
|
|
44
|
+
throw std::runtime_error("RTC Opus encoder allocation failed");
|
|
45
|
+
}
|
|
46
|
+
if (direction != Direction::Encode) {
|
|
47
|
+
esp_opus_dec_cfg_t receive{};
|
|
48
|
+
receive.sample_rate = sampleRate;
|
|
49
|
+
receive.channel = 1;
|
|
50
|
+
receive.frame_duration = ESP_OPUS_DEC_FRAME_DURATION_120_MS;
|
|
51
|
+
receive.self_delimited = false;
|
|
52
|
+
if (esp_opus_dec_open(&receive, sizeof(receive), &decoder) != ESP_AUDIO_ERR_OK) {
|
|
53
|
+
if (encoder) esp_opus_enc_close(encoder);
|
|
54
|
+
throw std::runtime_error("RTC Opus decoder allocation failed");
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
#else
|
|
58
|
+
int error;
|
|
59
|
+
if (direction != Direction::Decode) {
|
|
60
|
+
encoder = opus_encoder_create(sampleRate, 1, application == Application::LowDelay
|
|
61
|
+
? OPUS_APPLICATION_RESTRICTED_LOWDELAY : OPUS_APPLICATION_VOIP, &error);
|
|
62
|
+
if (!encoder) throw std::runtime_error("RTC Opus encoder allocation failed");
|
|
63
|
+
}
|
|
64
|
+
if (direction != Direction::Encode) {
|
|
65
|
+
decoder = opus_decoder_create(sampleRate, 1, &error);
|
|
66
|
+
if (!decoder) {
|
|
67
|
+
if (encoder) opus_encoder_destroy(encoder);
|
|
68
|
+
throw std::runtime_error("RTC Opus decoder allocation failed");
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (encoder) {
|
|
72
|
+
opus_encoder_ctl(encoder, OPUS_SET_BITRATE(24000));
|
|
73
|
+
// On S3 the complexity-3 encoder missed its 20 ms deadline alongside
|
|
74
|
+
// incoming RTP. Keep the 24 kbps wire format, with real-time CPU cost.
|
|
75
|
+
opus_encoder_ctl(encoder, OPUS_SET_COMPLEXITY(0));
|
|
76
|
+
opus_encoder_ctl(encoder, OPUS_SET_DTX(0));
|
|
77
|
+
}
|
|
78
|
+
#endif
|
|
79
|
+
}
|
|
80
|
+
~OpusAudio() {
|
|
81
|
+
#if GEA_RTC_ESP_OPUS
|
|
82
|
+
if (encoder) esp_opus_enc_close(encoder);
|
|
83
|
+
if (decoder) esp_opus_dec_close(decoder);
|
|
84
|
+
#else
|
|
85
|
+
if (encoder) opus_encoder_destroy(encoder);
|
|
86
|
+
if (decoder) opus_decoder_destroy(decoder);
|
|
87
|
+
#endif
|
|
88
|
+
}
|
|
89
|
+
OpusAudio(const OpusAudio&) = delete;
|
|
90
|
+
OpusAudio& operator=(const OpusAudio&) = delete;
|
|
91
|
+
int encode(const int16_t* pcm) {
|
|
92
|
+
if (!encoder || !pcm) return -1;
|
|
93
|
+
#if GEA_RTC_ESP_OPUS
|
|
94
|
+
esp_audio_enc_in_frame_t input{};
|
|
95
|
+
input.buffer = reinterpret_cast<uint8_t*>(const_cast<int16_t*>(pcm));
|
|
96
|
+
input.len = frameSamples * sizeof(int16_t);
|
|
97
|
+
esp_audio_enc_out_frame_t output{};
|
|
98
|
+
output.buffer = packet.data();
|
|
99
|
+
output.len = packet.size();
|
|
100
|
+
return esp_opus_enc_process(encoder, &input, &output) == ESP_AUDIO_ERR_OK
|
|
101
|
+
? int(output.encoded_bytes) : -1;
|
|
102
|
+
#else
|
|
103
|
+
return opus_encode(encoder, pcm, frameSamples, packet.data(), packet.size());
|
|
104
|
+
#endif
|
|
105
|
+
}
|
|
106
|
+
int decode(const uint8_t* bytes, int size) {
|
|
107
|
+
if (!decoder || !bytes || size <= 0) return -1;
|
|
108
|
+
#if GEA_RTC_ESP_OPUS
|
|
109
|
+
esp_audio_dec_in_raw_t input{};
|
|
110
|
+
input.buffer = const_cast<uint8_t*>(bytes);
|
|
111
|
+
input.len = size;
|
|
112
|
+
esp_audio_dec_out_frame_t output{};
|
|
113
|
+
output.buffer = reinterpret_cast<uint8_t*>(decoded.data());
|
|
114
|
+
output.len = sizeof(decoded);
|
|
115
|
+
esp_audio_dec_info_t info{};
|
|
116
|
+
const auto status = esp_opus_dec_decode(decoder, &input, &output, &info);
|
|
117
|
+
if (status != ESP_AUDIO_ERR_OK || input.consumed != unsigned(size) ||
|
|
118
|
+
output.decoded_size > sizeof(decoded) || info.channel != 1 || info.sample_rate != sampleRate)
|
|
119
|
+
return -1;
|
|
120
|
+
return int(output.decoded_size / sizeof(int16_t));
|
|
121
|
+
#else
|
|
122
|
+
return opus_decode(decoder, bytes, size, decoded.data(), decoded.size(), 0);
|
|
123
|
+
#endif
|
|
124
|
+
}
|
|
125
|
+
alignas(16) std::array<uint8_t, 1275> packet{};
|
|
126
|
+
alignas(16) std::array<int16_t, maxDecodeSamples> decoded{};
|
|
127
|
+
private:
|
|
128
|
+
#if GEA_RTC_ESP_OPUS
|
|
129
|
+
void* encoder = nullptr;
|
|
130
|
+
void* decoder = nullptr;
|
|
131
|
+
#else
|
|
132
|
+
OpusEncoder* encoder = nullptr;
|
|
133
|
+
OpusDecoder* decoder = nullptr;
|
|
134
|
+
#endif
|
|
135
|
+
};
|
|
136
|
+
}
|