@geastack/host 0.1.27 → 0.1.29
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/README.md +23 -0
- package/audio_runtime.cpp +131 -29
- package/bluetooth.cpp +22 -0
- package/host/audio.cpp +132 -4
- package/host/ble.cpp +44 -0
- package/host/bluetooth_central_apple.mm +412 -0
- package/host/crypto.cpp +318 -0
- package/host/crypto_apple.mm +69 -0
- package/host/fetch.cpp +47 -2
- package/host/files.cpp +194 -0
- package/host/files_apple.mm +25 -0
- package/host/image.cpp +17 -6
- package/host/media.cpp +8 -7
- package/host/menu.cpp +49 -0
- package/host/menu_apple.mm +249 -0
- package/host/menu_bar.cpp +31 -0
- package/host/menu_bar_apple.mm +220 -0
- package/host/opus.cpp +31 -0
- package/host/opus_apple.mm +134 -0
- package/host/tile_loader.cpp +5 -3
- package/host/websocket.cpp +29 -2
- package/host/websocket_apple.mm +30 -9
- package/host/websocket_curl.cpp +15 -4
- package/include/gea/embedded-host.h +5 -0
- package/include/host/audio.h +12 -0
- package/include/host/backends.h +25 -0
- package/include/host/crypto.h +46 -0
- package/include/host/files.h +42 -0
- package/include/host/menu.h +93 -0
- package/include/host/menu_bar.h +48 -0
- package/include/host/navigator.h +67 -1
- package/include/host/opus.h +35 -0
- package/include/host/websocket.h +2 -0
- package/package.json +1 -1
package/host/opus.cpp
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// The `Opus` host API (host/opus.h). Decoding itself is the platform's
|
|
3
|
+
// (host/opus_apple.mm); hosts without a decoder report supported() == false.
|
|
4
|
+
#include "host/opus.h"
|
|
5
|
+
|
|
6
|
+
namespace gea::host {
|
|
7
|
+
|
|
8
|
+
bool OpusFacade::supported() const { return opus_platform::supported(); }
|
|
9
|
+
|
|
10
|
+
double OpusFacade::createDecoder(double sampleRate, double channels) const {
|
|
11
|
+
const int rate = static_cast<int>(sampleRate);
|
|
12
|
+
const int count = static_cast<int>(channels);
|
|
13
|
+
if (rate < 8000 || rate > 48000 || count < 1 || count > 2) return 0;
|
|
14
|
+
return opus_platform::createDecoder(rate, count);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
std::vector<std::uint8_t> OpusFacade::decode(double decoder, const std::vector<std::uint8_t> &packets) const {
|
|
18
|
+
return opus_platform::decode(static_cast<int>(decoder), packets);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
void OpusFacade::destroyDecoder(double decoder) const { opus_platform::destroyDecoder(static_cast<int>(decoder)); }
|
|
22
|
+
|
|
23
|
+
namespace opus_platform {
|
|
24
|
+
|
|
25
|
+
__attribute__((weak)) bool supported() { return false; }
|
|
26
|
+
__attribute__((weak)) int createDecoder(int, int) { return 0; }
|
|
27
|
+
__attribute__((weak)) std::vector<std::uint8_t> decode(int, const std::vector<std::uint8_t> &) { return {}; }
|
|
28
|
+
__attribute__((weak)) void destroyDecoder(int) {}
|
|
29
|
+
|
|
30
|
+
} // namespace opus_platform
|
|
31
|
+
} // namespace gea::host
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// AudioToolbox Opus decoder for the `Opus` host API (host/opus.h) on macOS and
|
|
3
|
+
// iOS: an AVAudioConverter from 48 kHz Opus to interleaved 16-bit PCM. Each
|
|
4
|
+
// decode() call hands the converter one batch and tells it no more data is
|
|
5
|
+
// available *yet*, so the converter keeps its state for the next batch.
|
|
6
|
+
#if defined(__APPLE__)
|
|
7
|
+
|
|
8
|
+
#import <AVFoundation/AVFoundation.h>
|
|
9
|
+
|
|
10
|
+
#include <algorithm>
|
|
11
|
+
#include <cstring>
|
|
12
|
+
#include <map>
|
|
13
|
+
#include <utility>
|
|
14
|
+
|
|
15
|
+
#include "host/opus.h"
|
|
16
|
+
|
|
17
|
+
__asm__(".linker_option \"-framework\", \"AVFoundation\"");
|
|
18
|
+
__asm__(".linker_option \"-framework\", \"AudioToolbox\"");
|
|
19
|
+
|
|
20
|
+
@interface GeaOpusDecoder : NSObject
|
|
21
|
+
@property(nonatomic, strong) AVAudioConverter *converter;
|
|
22
|
+
@property(nonatomic, strong) AVAudioFormat *input;
|
|
23
|
+
@property(nonatomic, strong) AVAudioFormat *output;
|
|
24
|
+
@end
|
|
25
|
+
|
|
26
|
+
@implementation GeaOpusDecoder
|
|
27
|
+
|
|
28
|
+
- (instancetype)initWithRate:(int)rate channels:(int)channels {
|
|
29
|
+
if ((self = [super init])) {
|
|
30
|
+
AudioStreamBasicDescription description = {};
|
|
31
|
+
description.mSampleRate = 48000;
|
|
32
|
+
description.mFormatID = kAudioFormatOpus;
|
|
33
|
+
description.mFramesPerPacket = 960;
|
|
34
|
+
description.mChannelsPerFrame = static_cast<UInt32>(channels);
|
|
35
|
+
_input = [[AVAudioFormat alloc] initWithStreamDescription:&description];
|
|
36
|
+
_output = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatInt16
|
|
37
|
+
sampleRate:rate
|
|
38
|
+
channels:static_cast<AVAudioChannelCount>(channels)
|
|
39
|
+
interleaved:YES];
|
|
40
|
+
_converter = _input && _output ? [[AVAudioConverter alloc] initFromFormat:_input toFormat:_output] : nil;
|
|
41
|
+
if (!_converter) return nil;
|
|
42
|
+
}
|
|
43
|
+
return self;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
- (std::vector<std::uint8_t>)decode:(const std::vector<std::uint8_t> &)packets {
|
|
47
|
+
std::vector<std::pair<std::size_t, std::size_t>> spans;
|
|
48
|
+
std::size_t largest = 1;
|
|
49
|
+
for (std::size_t offset = 0; offset + 2 <= packets.size();) {
|
|
50
|
+
const std::size_t length = packets[offset] | (packets[offset + 1] << 8);
|
|
51
|
+
offset += 2;
|
|
52
|
+
if (offset + length > packets.size()) break;
|
|
53
|
+
spans.emplace_back(offset, length);
|
|
54
|
+
largest = std::max(largest, length);
|
|
55
|
+
offset += length;
|
|
56
|
+
}
|
|
57
|
+
if (spans.empty()) return {};
|
|
58
|
+
|
|
59
|
+
AVAudioCompressedBuffer *buffer = [[AVAudioCompressedBuffer alloc] initWithFormat:self.input
|
|
60
|
+
packetCapacity:(AVAudioPacketCount)spans.size()
|
|
61
|
+
maximumPacketSize:(NSInteger)largest];
|
|
62
|
+
std::size_t written = 0;
|
|
63
|
+
for (std::size_t index = 0; index < spans.size(); index++) {
|
|
64
|
+
std::memcpy(static_cast<std::uint8_t *>(buffer.data) + written, packets.data() + spans[index].first,
|
|
65
|
+
spans[index].second);
|
|
66
|
+
buffer.packetDescriptions[index].mStartOffset = (SInt64)written;
|
|
67
|
+
buffer.packetDescriptions[index].mVariableFramesInPacket = 0;
|
|
68
|
+
buffer.packetDescriptions[index].mDataByteSize = (UInt32)spans[index].second;
|
|
69
|
+
written += spans[index].second;
|
|
70
|
+
}
|
|
71
|
+
buffer.packetCount = (AVAudioPacketCount)spans.size();
|
|
72
|
+
buffer.byteLength = (UInt32)written;
|
|
73
|
+
|
|
74
|
+
std::vector<std::uint8_t> pcm;
|
|
75
|
+
const std::size_t frameBytes = 2 * self.output.channelCount;
|
|
76
|
+
AVAudioPCMBuffer *chunk = [[AVAudioPCMBuffer alloc] initWithPCMFormat:self.output frameCapacity:16384];
|
|
77
|
+
__block BOOL given = NO;
|
|
78
|
+
while (true) {
|
|
79
|
+
chunk.frameLength = 0;
|
|
80
|
+
NSError *error = nil;
|
|
81
|
+
const AVAudioConverterOutputStatus status =
|
|
82
|
+
[self.converter convertToBuffer:chunk
|
|
83
|
+
error:&error
|
|
84
|
+
withInputFromBlock:^AVAudioBuffer *(AVAudioPacketCount, AVAudioConverterInputStatus *state) {
|
|
85
|
+
if (given) {
|
|
86
|
+
*state = AVAudioConverterInputStatus_NoDataNow;
|
|
87
|
+
return nil;
|
|
88
|
+
}
|
|
89
|
+
given = YES;
|
|
90
|
+
*state = AVAudioConverterInputStatus_HaveData;
|
|
91
|
+
return buffer;
|
|
92
|
+
}];
|
|
93
|
+
if (error) return pcm;
|
|
94
|
+
if (chunk.frameLength > 0) {
|
|
95
|
+
const auto *samples = reinterpret_cast<const std::uint8_t *>(chunk.int16ChannelData[0]);
|
|
96
|
+
pcm.insert(pcm.end(), samples, samples + chunk.frameLength * frameBytes);
|
|
97
|
+
}
|
|
98
|
+
if (status != AVAudioConverterOutputStatus_HaveData) return pcm;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
@end
|
|
103
|
+
|
|
104
|
+
namespace gea::host::opus_platform {
|
|
105
|
+
namespace {
|
|
106
|
+
|
|
107
|
+
std::map<int, GeaOpusDecoder *> &decoders() {
|
|
108
|
+
static std::map<int, GeaOpusDecoder *> instances;
|
|
109
|
+
return instances;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
} // namespace
|
|
113
|
+
|
|
114
|
+
bool supported() { return true; }
|
|
115
|
+
|
|
116
|
+
int createDecoder(int sampleRate, int channels) {
|
|
117
|
+
static int next = 0;
|
|
118
|
+
GeaOpusDecoder *decoder = [[GeaOpusDecoder alloc] initWithRate:sampleRate channels:channels];
|
|
119
|
+
if (!decoder) return 0;
|
|
120
|
+
decoders()[++next] = decoder;
|
|
121
|
+
return next;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
std::vector<std::uint8_t> decode(int decoder, const std::vector<std::uint8_t> &packets) {
|
|
125
|
+
auto found = decoders().find(decoder);
|
|
126
|
+
if (found == decoders().end()) return {};
|
|
127
|
+
return [found->second decode:packets];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
void destroyDecoder(int decoder) { decoders().erase(decoder); }
|
|
131
|
+
|
|
132
|
+
} // namespace gea::host::opus_platform
|
|
133
|
+
|
|
134
|
+
#endif
|
package/host/tile_loader.cpp
CHANGED
|
@@ -69,7 +69,7 @@ std::int64_t nowUs() {
|
|
|
69
69
|
|
|
70
70
|
std::vector<std::uint8_t> readWholeFile(const std::string &path) {
|
|
71
71
|
std::vector<std::uint8_t> out;
|
|
72
|
-
std::FILE *f = std::fopen(path.c_str(), "rb");
|
|
72
|
+
std::FILE *f = std::fopen(gea::platform::storage::hostPath(path).c_str(), "rb");
|
|
73
73
|
if (!f) return out;
|
|
74
74
|
std::fseek(f, 0, SEEK_END);
|
|
75
75
|
const long size = std::ftell(f);
|
|
@@ -193,7 +193,8 @@ volatile bool g_prune_pending = false;
|
|
|
193
193
|
|
|
194
194
|
// Delete every tiles/<z> level directory whose z is NOT in the keep CSV.
|
|
195
195
|
// Depth-2 tree remove (z/x/y.png) over FAT — worker core only.
|
|
196
|
-
void pruneTileLevels(const std::string &
|
|
196
|
+
void pruneTileLevels(const std::string &logicalRoot, const std::string &keepCsv) {
|
|
197
|
+
const std::string root = gea::platform::storage::hostPath(logicalRoot);
|
|
197
198
|
std::unordered_set<std::string> keep;
|
|
198
199
|
std::string token;
|
|
199
200
|
for (char c : keepCsv + ",") {
|
|
@@ -244,7 +245,8 @@ void pruneTileLevels(const std::string &root, const std::string &keepCsv) {
|
|
|
244
245
|
// One-time census of the persistent tile cache: logs how many tiles each zoom
|
|
245
246
|
// level holds (tiles/<z>/<x>/<y>.png). Runs once on the worker core before the
|
|
246
247
|
// first job — readdir over FAT is slow, so it must never touch the frame task.
|
|
247
|
-
void logTileCensus(const std::string &
|
|
248
|
+
void logTileCensus(const std::string &logicalRoot) {
|
|
249
|
+
const std::string root = gea::platform::storage::hostPath(logicalRoot);
|
|
248
250
|
DIR *zDir = opendir(root.c_str());
|
|
249
251
|
if (!zDir) return;
|
|
250
252
|
int totalFiles = 0;
|
package/host/websocket.cpp
CHANGED
|
@@ -150,6 +150,7 @@ static CallbackTable detachCallbacks(NativeWebSocketHandle handle) {
|
|
|
150
150
|
void platform_open(NativeWebSocketHandle, const std::string &, const std::string &, const std::string &);
|
|
151
151
|
std::size_t platform_buffered(NativeWebSocketHandle);
|
|
152
152
|
void platform_send(NativeWebSocketHandle, const std::string &);
|
|
153
|
+
void platform_send_binary(NativeWebSocketHandle, const std::string &);
|
|
153
154
|
void platform_close(NativeWebSocketHandle);
|
|
154
155
|
void platform_destroy(NativeWebSocketHandle);
|
|
155
156
|
void platform_reap();
|
|
@@ -158,6 +159,7 @@ void platform_set_producer(NativeWebSocketHandle, TextProducer);
|
|
|
158
159
|
__attribute__((weak)) void platform_open(NativeWebSocketHandle, const std::string &, const std::string &, const std::string &) {}
|
|
159
160
|
__attribute__((weak)) std::size_t platform_buffered(NativeWebSocketHandle) { return 0; }
|
|
160
161
|
__attribute__((weak)) void platform_send(NativeWebSocketHandle, const std::string &) {}
|
|
162
|
+
__attribute__((weak)) void platform_send_binary(NativeWebSocketHandle, const std::string &) {}
|
|
161
163
|
__attribute__((weak)) void platform_close(NativeWebSocketHandle) {}
|
|
162
164
|
__attribute__((weak)) void platform_destroy(NativeWebSocketHandle) {}
|
|
163
165
|
__attribute__((weak)) void platform_reap() {}
|
|
@@ -395,6 +397,10 @@ void WebSocket::send(const std::string &data) const {
|
|
|
395
397
|
websocket::platform_send(nativeHandle, data);
|
|
396
398
|
}
|
|
397
399
|
|
|
400
|
+
void WebSocket::send(const std::vector<std::uint8_t> &data) const {
|
|
401
|
+
websocket::platform_send_binary(nativeHandle, std::string(data.begin(), data.end()));
|
|
402
|
+
}
|
|
403
|
+
|
|
398
404
|
void WebSocket::close() const {
|
|
399
405
|
websocket::platform_close(nativeHandle);
|
|
400
406
|
}
|
|
@@ -487,6 +493,8 @@ struct EspWsState {
|
|
|
487
493
|
}
|
|
488
494
|
std::mutex mutex;
|
|
489
495
|
std::deque<std::string> outgoing;
|
|
496
|
+
// Parallel to `outgoing`: true where the entry goes out as a binary frame.
|
|
497
|
+
std::deque<bool> outgoingBinary;
|
|
490
498
|
TextProducer producer;
|
|
491
499
|
std::size_t bytes = 0;
|
|
492
500
|
std::atomic<bool> closing{false};
|
|
@@ -748,11 +756,16 @@ void sendWorker(void *arg) {
|
|
|
748
756
|
std::string message;
|
|
749
757
|
TextProducer producer;
|
|
750
758
|
bool queued = false;
|
|
759
|
+
bool binary = false;
|
|
751
760
|
{
|
|
752
761
|
std::lock_guard<std::mutex> lock(state->mutex);
|
|
753
762
|
if (!state->outgoing.empty()) {
|
|
754
763
|
message = std::move(state->outgoing.front());
|
|
755
764
|
state->outgoing.pop_front();
|
|
765
|
+
if (!state->outgoingBinary.empty()) {
|
|
766
|
+
binary = state->outgoingBinary.front();
|
|
767
|
+
state->outgoingBinary.pop_front();
|
|
768
|
+
}
|
|
756
769
|
queued = true;
|
|
757
770
|
} else {
|
|
758
771
|
producer = state->producer;
|
|
@@ -785,8 +798,11 @@ void sendWorker(void *arg) {
|
|
|
785
798
|
#endif
|
|
786
799
|
// TCP's initial retransmit timeout is 1.5 s. A 1 s application deadline
|
|
787
800
|
// aborted healthy connections before TCP could recover a single lost packet.
|
|
788
|
-
const auto sent =
|
|
789
|
-
|
|
801
|
+
const auto sent = binary
|
|
802
|
+
? esp_websocket_client_send_bin(state->client, message.data(),
|
|
803
|
+
static_cast<int>(message.size()), pdMS_TO_TICKS(5000))
|
|
804
|
+
: esp_websocket_client_send_text(state->client, message.data(),
|
|
805
|
+
static_cast<int>(message.size()), pdMS_TO_TICKS(5000));
|
|
790
806
|
const auto sendMs = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
791
807
|
std::chrono::steady_clock::now() - sendStarted).count();
|
|
792
808
|
#if CONFIG_FREERTOS_USE_TRACE_FACILITY && CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS && CONFIG_FREERTOS_RUN_TIME_STATS_USING_ESP_TIMER
|
|
@@ -958,7 +974,17 @@ void platform_set_producer(NativeWebSocketHandle handle, TextProducer producer)
|
|
|
958
974
|
vTaskPrioritySet(it->second->worker, configMAX_PRIORITIES - 2);
|
|
959
975
|
}
|
|
960
976
|
|
|
977
|
+
static void platformEnqueue(NativeWebSocketHandle handle, const std::string &data, bool binary);
|
|
978
|
+
|
|
961
979
|
void platform_send(NativeWebSocketHandle handle, const std::string &data) {
|
|
980
|
+
platformEnqueue(handle, data, false);
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
void platform_send_binary(NativeWebSocketHandle handle, const std::string &data) {
|
|
984
|
+
platformEnqueue(handle, data, true);
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
static void platformEnqueue(NativeWebSocketHandle handle, const std::string &data, bool binary) {
|
|
962
988
|
std::lock_guard platformLock(platformMutex);
|
|
963
989
|
const auto it = espStateTable().find(handle);
|
|
964
990
|
if (it == espStateTable().end() || it->second->closing.load()) return;
|
|
@@ -970,6 +996,7 @@ void platform_send(NativeWebSocketHandle handle, const std::string &data) {
|
|
|
970
996
|
return;
|
|
971
997
|
}
|
|
972
998
|
state.outgoing.push_back(data);
|
|
999
|
+
state.outgoingBinary.push_back(binary);
|
|
973
1000
|
state.bytes += data.size();
|
|
974
1001
|
}
|
|
975
1002
|
|
package/host/websocket_apple.mm
CHANGED
|
@@ -23,6 +23,8 @@ struct State : std::enable_shared_from_this<State> {
|
|
|
23
23
|
NativeWebSocketHandle handle;
|
|
24
24
|
std::mutex mutex;
|
|
25
25
|
std::deque<std::string> outgoing;
|
|
26
|
+
// Parallel to `outgoing`: true where the entry goes out as a binary frame.
|
|
27
|
+
std::deque<bool> outgoingBinary;
|
|
26
28
|
std::size_t bytes = 0;
|
|
27
29
|
bool sending = false;
|
|
28
30
|
bool opened = false;
|
|
@@ -45,6 +47,7 @@ struct State : std::enable_shared_from_this<State> {
|
|
|
45
47
|
stopped = true;
|
|
46
48
|
retired = std::move(producer);
|
|
47
49
|
outgoing.clear();
|
|
50
|
+
outgoingBinary.clear();
|
|
48
51
|
bytes = 0;
|
|
49
52
|
oldTimer = timer;
|
|
50
53
|
timer = nil;
|
|
@@ -65,6 +68,7 @@ struct State : std::enable_shared_from_this<State> {
|
|
|
65
68
|
stopped = true;
|
|
66
69
|
retired = std::move(producer);
|
|
67
70
|
outgoing.clear();
|
|
71
|
+
outgoingBinary.clear();
|
|
68
72
|
bytes = 0;
|
|
69
73
|
}
|
|
70
74
|
retired = {};
|
|
@@ -85,6 +89,7 @@ struct State : std::enable_shared_from_this<State> {
|
|
|
85
89
|
stopped = true;
|
|
86
90
|
retired = std::move(producer);
|
|
87
91
|
outgoing.clear();
|
|
92
|
+
outgoingBinary.clear();
|
|
88
93
|
bytes = 0;
|
|
89
94
|
}
|
|
90
95
|
retired = {};
|
|
@@ -100,7 +105,7 @@ struct State : std::enable_shared_from_this<State> {
|
|
|
100
105
|
transport_close(handle, 1006, "WebSocket transport failed");
|
|
101
106
|
shutdown();
|
|
102
107
|
}
|
|
103
|
-
bool enqueue(std::string text) {
|
|
108
|
+
bool enqueue(std::string text, bool binary = false) {
|
|
104
109
|
bool overflow = false;
|
|
105
110
|
{
|
|
106
111
|
std::lock_guard lock(mutex);
|
|
@@ -111,6 +116,7 @@ struct State : std::enable_shared_from_this<State> {
|
|
|
111
116
|
if (!overflow) {
|
|
112
117
|
bytes += text.size();
|
|
113
118
|
outgoing.push_back(std::move(text));
|
|
119
|
+
outgoingBinary.push_back(binary);
|
|
114
120
|
}
|
|
115
121
|
}
|
|
116
122
|
if (overflow) {
|
|
@@ -122,6 +128,7 @@ struct State : std::enable_shared_from_this<State> {
|
|
|
122
128
|
}
|
|
123
129
|
void pump() {
|
|
124
130
|
std::string text;
|
|
131
|
+
bool binary = false;
|
|
125
132
|
{
|
|
126
133
|
std::lock_guard lock(mutex);
|
|
127
134
|
if (stopped || sending || !opened || outgoing.empty())
|
|
@@ -129,18 +136,26 @@ struct State : std::enable_shared_from_this<State> {
|
|
|
129
136
|
sending = true;
|
|
130
137
|
text = std::move(outgoing.front());
|
|
131
138
|
outgoing.pop_front();
|
|
139
|
+
binary = outgoingBinary.front();
|
|
140
|
+
outgoingBinary.pop_front();
|
|
132
141
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
142
|
+
NSURLSessionWebSocketMessage *message = nil;
|
|
143
|
+
if (binary) {
|
|
144
|
+
message = [[NSURLSessionWebSocketMessage alloc]
|
|
145
|
+
initWithData:[NSData dataWithBytes:text.data() length:text.size()]];
|
|
146
|
+
} else {
|
|
147
|
+
NSString *value = [[NSString alloc] initWithBytes:text.data()
|
|
148
|
+
length:text.size()
|
|
149
|
+
encoding:NSUTF8StringEncoding];
|
|
150
|
+
if (!value) {
|
|
151
|
+
failed("WebSocket text must be valid UTF-8");
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
message = [[NSURLSessionWebSocketMessage alloc] initWithString:value];
|
|
139
155
|
}
|
|
140
156
|
const auto length = text.size();
|
|
141
157
|
std::weak_ptr<State> weak = shared_from_this();
|
|
142
|
-
[task sendMessage:
|
|
143
|
-
initWithString:value]
|
|
158
|
+
[task sendMessage:message
|
|
144
159
|
completionHandler:^(NSError *error) {
|
|
145
160
|
if (auto state = weak.lock()) {
|
|
146
161
|
if (error) {
|
|
@@ -246,6 +261,7 @@ std::shared_ptr<State> find(NativeWebSocketHandle handle) {
|
|
|
246
261
|
state->stopped = true;
|
|
247
262
|
retired = std::move(state->producer);
|
|
248
263
|
state->outgoing.clear();
|
|
264
|
+
state->outgoingBinary.clear();
|
|
249
265
|
state->bytes = 0;
|
|
250
266
|
}
|
|
251
267
|
retired = {};
|
|
@@ -372,6 +388,11 @@ void platform_send(NativeWebSocketHandle handle, const std::string &text) {
|
|
|
372
388
|
if (!state || !state->enqueue(text))
|
|
373
389
|
throw std::runtime_error("WebSocket is not open or send queue is full");
|
|
374
390
|
}
|
|
391
|
+
void platform_send_binary(NativeWebSocketHandle handle, const std::string &bytes) {
|
|
392
|
+
auto state = apple::find(handle);
|
|
393
|
+
if (!state || !state->enqueue(bytes, true))
|
|
394
|
+
throw std::runtime_error("WebSocket is not open or send queue is full");
|
|
395
|
+
}
|
|
375
396
|
void platform_close(NativeWebSocketHandle handle) {
|
|
376
397
|
auto state = apple::find(handle);
|
|
377
398
|
if (!state)
|
package/host/websocket_curl.cpp
CHANGED
|
@@ -70,6 +70,8 @@ struct State {
|
|
|
70
70
|
std::string url, protocols, headers;
|
|
71
71
|
std::mutex mutex;
|
|
72
72
|
std::deque<std::string> outgoing;
|
|
73
|
+
// Parallel to `outgoing`: true where the entry goes out as a binary frame.
|
|
74
|
+
std::deque<bool> outgoingBinary;
|
|
73
75
|
std::size_t bytes = 0, entries = 0;
|
|
74
76
|
TextProducer producer;
|
|
75
77
|
std::atomic<bool> stop{false}, closing{false}, opened{false}, finished{false};
|
|
@@ -87,7 +89,7 @@ struct State {
|
|
|
87
89
|
transport_close(handle, 1006, "WebSocket transport failed");
|
|
88
90
|
}
|
|
89
91
|
}
|
|
90
|
-
bool enqueue(std::string text) {
|
|
92
|
+
bool enqueue(std::string text, bool binary = false) {
|
|
91
93
|
std::lock_guard lock(mutex);
|
|
92
94
|
if (!opened.load() || closing.load() || stop.load())
|
|
93
95
|
return false;
|
|
@@ -96,6 +98,7 @@ struct State {
|
|
|
96
98
|
bytes += text.size();
|
|
97
99
|
++entries;
|
|
98
100
|
outgoing.push_back(std::move(text));
|
|
101
|
+
outgoingBinary.push_back(binary);
|
|
99
102
|
return true;
|
|
100
103
|
}
|
|
101
104
|
void run();
|
|
@@ -140,6 +143,7 @@ void State::run() {
|
|
|
140
143
|
{
|
|
141
144
|
std::lock_guard lock(s.mutex);
|
|
142
145
|
s.outgoing.clear();
|
|
146
|
+
s.outgoingBinary.clear();
|
|
143
147
|
retired = std::move(s.producer);
|
|
144
148
|
s.bytes = s.entries = 0;
|
|
145
149
|
}
|
|
@@ -240,7 +244,7 @@ void State::run() {
|
|
|
240
244
|
}
|
|
241
245
|
std::string active, message, control;
|
|
242
246
|
std::size_t sentAt = 0, frameAt = 0;
|
|
243
|
-
bool haveActive = false, messageOpen = false;
|
|
247
|
+
bool haveActive = false, messageOpen = false, activeBinary = false;
|
|
244
248
|
int messageType = 0;
|
|
245
249
|
bool closeSent = false;
|
|
246
250
|
auto sendDeadline = Clock::now(), closeDeadline = Clock::time_point::max();
|
|
@@ -297,20 +301,22 @@ void State::run() {
|
|
|
297
301
|
if (!outgoing.empty()) {
|
|
298
302
|
active = std::move(outgoing.front());
|
|
299
303
|
outgoing.pop_front();
|
|
304
|
+
activeBinary = outgoingBinary.front();
|
|
305
|
+
outgoingBinary.pop_front();
|
|
300
306
|
sentAt = 0;
|
|
301
307
|
haveActive = true;
|
|
302
308
|
sendDeadline = Clock::now() + std::chrono::seconds(10);
|
|
303
309
|
}
|
|
304
310
|
}
|
|
305
311
|
if (haveActive) {
|
|
306
|
-
if (!utf8(active)) {
|
|
312
|
+
if (!activeBinary && !utf8(active)) {
|
|
307
313
|
fail("WebSocket text must be valid UTF-8");
|
|
308
314
|
return;
|
|
309
315
|
}
|
|
310
316
|
std::size_t sent = 0;
|
|
311
317
|
const auto rc =
|
|
312
318
|
curl_ws_send(c.easy, active.data() + sentAt, active.size() - sentAt,
|
|
313
|
-
&sent, 0, CURLWS_TEXT);
|
|
319
|
+
&sent, 0, activeBinary ? CURLWS_BINARY : CURLWS_TEXT);
|
|
314
320
|
if (rc != CURLE_OK && rc != CURLE_AGAIN) {
|
|
315
321
|
fail("WebSocket send failed");
|
|
316
322
|
return;
|
|
@@ -519,6 +525,11 @@ void platform_send(NativeWebSocketHandle h, const std::string &text) {
|
|
|
519
525
|
if (!s || !s->enqueue(text))
|
|
520
526
|
throw std::runtime_error("WebSocket is not open");
|
|
521
527
|
}
|
|
528
|
+
void platform_send_binary(NativeWebSocketHandle h, const std::string &bytes) {
|
|
529
|
+
auto s = curl_transport::find(h);
|
|
530
|
+
if (!s || !s->enqueue(bytes, true))
|
|
531
|
+
throw std::runtime_error("WebSocket is not open");
|
|
532
|
+
}
|
|
522
533
|
void platform_close(NativeWebSocketHandle h) {
|
|
523
534
|
auto s = curl_transport::find(h);
|
|
524
535
|
if (!s)
|
|
@@ -34,6 +34,11 @@
|
|
|
34
34
|
#include "host/profiler.h"
|
|
35
35
|
#include "host/storage.h"
|
|
36
36
|
#include "host/battery.h"
|
|
37
|
+
#include "host/crypto.h"
|
|
38
|
+
#include "host/opus.h"
|
|
39
|
+
#include "host/files.h"
|
|
40
|
+
#include "host/menu.h"
|
|
41
|
+
#include "host/menu_bar.h"
|
|
37
42
|
#include "host/notify.h"
|
|
38
43
|
#include "host/device_control.h"
|
|
39
44
|
#include <input.h>
|
package/include/host/audio.h
CHANGED
|
@@ -265,6 +265,18 @@ class HTMLAudioElement {
|
|
|
265
265
|
bool autoplay() const;
|
|
266
266
|
void setAutoplay(bool enabled) const;
|
|
267
267
|
bool paused() const;
|
|
268
|
+
// File playback position in seconds (`src` only; a srcObject stream reads 0).
|
|
269
|
+
// Live while playing, held while paused, 0 before the first play().
|
|
270
|
+
double currentTime() const;
|
|
271
|
+
// Seek: clamped to [0, duration]. While playing, playback continues from
|
|
272
|
+
// there; while paused, the next play() starts there. At or past the end the
|
|
273
|
+
// element ends (paused and ended turn true).
|
|
274
|
+
void setCurrentTime(double seconds) const;
|
|
275
|
+
// Length of `src` in seconds, read from the file on first use. NaN while no
|
|
276
|
+
// src is set or the file cannot be opened; +Infinity for a srcObject stream.
|
|
277
|
+
double duration() const;
|
|
278
|
+
// The file played (or was seeked) to its end. play() then restarts at 0.
|
|
279
|
+
bool ended() const;
|
|
268
280
|
explicit operator bool() const { return static_cast<bool>(state_); }
|
|
269
281
|
bool operator==(std::nullptr_t) const { return !state_; }
|
|
270
282
|
bool operator==(const HTMLAudioElement& other) const { return state_ == other.state_; }
|
package/include/host/backends.h
CHANGED
|
@@ -341,6 +341,31 @@ public:
|
|
|
341
341
|
static void configPushActivity(double index);
|
|
342
342
|
};
|
|
343
343
|
|
|
344
|
+
class CentralBackend {
|
|
345
|
+
public:
|
|
346
|
+
static std::string state();
|
|
347
|
+
static void startScan(const std::string &services);
|
|
348
|
+
static void stopScan();
|
|
349
|
+
static bool scanning();
|
|
350
|
+
static double scanCount();
|
|
351
|
+
static std::string scanIdAt(double index);
|
|
352
|
+
static std::string scanNameAt(double index);
|
|
353
|
+
static double scanRssiAt(double index);
|
|
354
|
+
static std::string scanServicesAt(double index);
|
|
355
|
+
static std::vector<std::uint8_t> scanManufacturerDataAt(double index);
|
|
356
|
+
static void clearScan();
|
|
357
|
+
static void connect(const std::string &id, const std::string &services);
|
|
358
|
+
static std::string link();
|
|
359
|
+
static std::string linkError();
|
|
360
|
+
static void subscribe(const std::string &service, const std::string &characteristic);
|
|
361
|
+
static bool subscribed(const std::string &service, const std::string &characteristic);
|
|
362
|
+
static bool write(const std::string &service, const std::string &characteristic,
|
|
363
|
+
const std::vector<std::uint8_t> &bytes);
|
|
364
|
+
static std::vector<std::uint8_t> read(const std::string &service, const std::string &characteristic);
|
|
365
|
+
static void requestRead(const std::string &service, const std::string &characteristic);
|
|
366
|
+
static void disconnect();
|
|
367
|
+
};
|
|
368
|
+
|
|
344
369
|
} // namespace gea::framework::bluetooth
|
|
345
370
|
|
|
346
371
|
namespace gea::framework::audio {
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
#include <cstdint>
|
|
5
|
+
#include <vector>
|
|
6
|
+
|
|
7
|
+
namespace gea::host {
|
|
8
|
+
|
|
9
|
+
using CryptoBytes = std::vector<std::uint8_t>;
|
|
10
|
+
|
|
11
|
+
// Cryptography for app code (TS `Cryptography`, core index.d.ts): the
|
|
12
|
+
// primitives a device protocol or an end-to-end link needs. Hashing, HMAC,
|
|
13
|
+
// HKDF, AES-CTR and random bytes are portable (host/crypto.cpp); P-256 key
|
|
14
|
+
// agreement comes from the platform (Security.framework on Apple hosts) and
|
|
15
|
+
// returns empty bytes where there is none.
|
|
16
|
+
struct CryptographyFacade {
|
|
17
|
+
CryptoBytes sha256(const CryptoBytes &data) const;
|
|
18
|
+
CryptoBytes hmacSha256(const CryptoBytes &key, const CryptoBytes &data) const;
|
|
19
|
+
// RFC 5869 extract-then-expand; `length` is at most 8160 bytes.
|
|
20
|
+
CryptoBytes hkdfSha256(const CryptoBytes &keyMaterial, const CryptoBytes &salt, const CryptoBytes &info,
|
|
21
|
+
double length) const;
|
|
22
|
+
// AES-128/192/256 in counter mode with a 16-byte big-endian counter block.
|
|
23
|
+
// Encrypting and decrypting are the same operation. Empty on a bad key or counter.
|
|
24
|
+
CryptoBytes aesCtr(const CryptoBytes &key, const CryptoBytes &counter, const CryptoBytes &data) const;
|
|
25
|
+
CryptoBytes randomBytes(double length) const;
|
|
26
|
+
// A new P-256 key pair: the 65-byte uncompressed public key (0x04 || X || Y)
|
|
27
|
+
// followed by the 32-byte private scalar -- 97 bytes, ANSI X9.63. The first
|
|
28
|
+
// 65 bytes are what the peer needs.
|
|
29
|
+
CryptoBytes p256GenerateKey() const;
|
|
30
|
+
// ECDH: the 32-byte shared X coordinate for our 97-byte key and the peer's
|
|
31
|
+
// 65-byte public key. Empty when either key is malformed or P-256 is unsupported.
|
|
32
|
+
CryptoBytes p256SharedSecret(const CryptoBytes &privateKey, const CryptoBytes &peerPublicKey) const;
|
|
33
|
+
bool p256Supported() const;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
inline constexpr CryptographyFacade Cryptography{};
|
|
37
|
+
|
|
38
|
+
namespace crypto_platform {
|
|
39
|
+
// Platform hooks; host/crypto.cpp holds weak fallbacks.
|
|
40
|
+
bool randomFill(std::uint8_t *bytes, std::size_t length);
|
|
41
|
+
CryptoBytes p256GenerateKey();
|
|
42
|
+
CryptoBytes p256SharedSecret(const CryptoBytes &privateKey, const CryptoBytes &peerPublicKey);
|
|
43
|
+
bool p256Supported();
|
|
44
|
+
} // namespace crypto_platform
|
|
45
|
+
|
|
46
|
+
} // namespace gea::host
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
#include <cstdint>
|
|
5
|
+
#include <string>
|
|
6
|
+
#include <vector>
|
|
7
|
+
|
|
8
|
+
namespace gea::host {
|
|
9
|
+
|
|
10
|
+
// Byte-level file access for app code (TS `Files`, core index.d.ts): what a
|
|
11
|
+
// recorder spool, a resumable download or a WAV writer needs beyond the whole-
|
|
12
|
+
// file cache helpers (writeCacheFile/readCacheFile/readFileRange). Paths are
|
|
13
|
+
// plain filesystem paths; dataDirectory() is the app's own writable place.
|
|
14
|
+
struct FilesFacade {
|
|
15
|
+
// The app's writable data directory, created on first use: Application
|
|
16
|
+
// Support/<app name> on macOS, the app container on iOS, /sdcard on boards,
|
|
17
|
+
// $XDG_DATA_HOME (or ~/.local/share)/<app name> elsewhere.
|
|
18
|
+
std::string dataDirectory() const;
|
|
19
|
+
// mkdir -p; true when the directory exists afterwards.
|
|
20
|
+
bool makeDirectory(const std::string &path) const;
|
|
21
|
+
bool exists(const std::string &path) const;
|
|
22
|
+
// Size in bytes, or -1 when there is no such file.
|
|
23
|
+
double size(const std::string &path) const;
|
|
24
|
+
// Bytes from `offset`, at most `length`; fewer at the end of the file.
|
|
25
|
+
std::vector<std::uint8_t> read(const std::string &path, double offset, double length) const;
|
|
26
|
+
// Creates the file when missing.
|
|
27
|
+
bool append(const std::string &path, const std::vector<std::uint8_t> &bytes) const;
|
|
28
|
+
// Overwrites in place from `offset`, creating the file when missing.
|
|
29
|
+
bool writeAt(const std::string &path, double offset, const std::vector<std::uint8_t> &bytes) const;
|
|
30
|
+
bool truncate(const std::string &path, double size) const;
|
|
31
|
+
bool remove(const std::string &path) const;
|
|
32
|
+
bool rename(const std::string &from, const std::string &to) const;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
inline constexpr FilesFacade Files{};
|
|
36
|
+
|
|
37
|
+
namespace files_platform {
|
|
38
|
+
// Platform hook; host/files.cpp holds the fallback.
|
|
39
|
+
std::string dataDirectory();
|
|
40
|
+
} // namespace files_platform
|
|
41
|
+
|
|
42
|
+
} // namespace gea::host
|