@geastack/host 0.1.27 → 0.1.28
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 +12 -0
- package/bluetooth.cpp +22 -0
- 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/files.cpp +189 -0
- package/host/files_apple.mm +25 -0
- package/host/menu_bar.cpp +26 -0
- package/host/menu_bar_apple.mm +174 -0
- package/host/opus.cpp +31 -0
- package/host/opus_apple.mm +134 -0
- 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 +4 -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_bar.h +37 -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
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// macOS menu bar mode for the `MenuBar` host API (host/menu_bar.h): a status
|
|
3
|
+
// item with an SF Symbol, the app's window restyled as a borderless panel that
|
|
4
|
+
// drops down under it, accessory activation (no Dock icon), and a timer that
|
|
5
|
+
// keeps the app delegate's frames coming while the panel is hidden -- the
|
|
6
|
+
// window's display link stops with the window.
|
|
7
|
+
#if defined(__APPLE__)
|
|
8
|
+
#include <TargetConditionals.h>
|
|
9
|
+
#endif
|
|
10
|
+
#if defined(__APPLE__) && TARGET_OS_OSX
|
|
11
|
+
|
|
12
|
+
#import <AppKit/AppKit.h>
|
|
13
|
+
|
|
14
|
+
#include "host/menu_bar.h"
|
|
15
|
+
|
|
16
|
+
static const CGFloat kPanelGap = 6;
|
|
17
|
+
static const NSTimeInterval kHiddenTick = 1.0 / 20.0;
|
|
18
|
+
|
|
19
|
+
@interface GeaMenuBar : NSObject
|
|
20
|
+
@property(nonatomic, strong) NSStatusItem *item;
|
|
21
|
+
@property(nonatomic, weak) NSWindow *window;
|
|
22
|
+
@property(nonatomic, strong) NSTimer *timer;
|
|
23
|
+
@property(nonatomic, strong) NSMenu *menu;
|
|
24
|
+
@end
|
|
25
|
+
|
|
26
|
+
@implementation GeaMenuBar
|
|
27
|
+
|
|
28
|
+
- (NSWindow *)appWindow {
|
|
29
|
+
id delegate = NSApp.delegate;
|
|
30
|
+
if ([delegate respondsToSelector:@selector(window)]) {
|
|
31
|
+
id window = [delegate performSelector:@selector(window)];
|
|
32
|
+
if ([window isKindOfClass:[NSWindow class]]) return window;
|
|
33
|
+
}
|
|
34
|
+
for (NSWindow *window in NSApp.windows) {
|
|
35
|
+
if (window.canBecomeMainWindow) return window;
|
|
36
|
+
}
|
|
37
|
+
return NSApp.windows.firstObject;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
- (void)installWithSymbol:(NSString *)symbol {
|
|
41
|
+
[NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory];
|
|
42
|
+
|
|
43
|
+
NSWindow *window = [self appWindow];
|
|
44
|
+
self.window = window;
|
|
45
|
+
if (window) {
|
|
46
|
+
const NSSize size = window.contentView.bounds.size;
|
|
47
|
+
window.styleMask = NSWindowStyleMaskTitled | NSWindowStyleMaskFullSizeContentView;
|
|
48
|
+
window.titlebarAppearsTransparent = YES;
|
|
49
|
+
window.titleVisibility = NSWindowTitleHidden;
|
|
50
|
+
for (NSWindowButton button : {NSWindowCloseButton, NSWindowMiniaturizeButton, NSWindowZoomButton}) {
|
|
51
|
+
[window standardWindowButton:button].hidden = YES;
|
|
52
|
+
}
|
|
53
|
+
window.movable = NO;
|
|
54
|
+
window.level = NSPopUpMenuWindowLevel;
|
|
55
|
+
window.collectionBehavior = NSWindowCollectionBehaviorCanJoinAllSpaces |
|
|
56
|
+
NSWindowCollectionBehaviorFullScreenAuxiliary | NSWindowCollectionBehaviorTransient;
|
|
57
|
+
[window setContentSize:size];
|
|
58
|
+
[window orderOut:nil];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
self.item = [NSStatusBar.systemStatusBar statusItemWithLength:NSVariableStatusItemLength];
|
|
62
|
+
[self setSymbol:symbol];
|
|
63
|
+
self.item.button.target = self;
|
|
64
|
+
self.item.button.action = @selector(clicked:);
|
|
65
|
+
[self.item.button sendActionOn:NSEventMaskLeftMouseUp | NSEventMaskRightMouseUp];
|
|
66
|
+
|
|
67
|
+
NSString *name = [NSBundle.mainBundle objectForInfoDictionaryKey:@"CFBundleDisplayName"]
|
|
68
|
+
?: [NSBundle.mainBundle objectForInfoDictionaryKey:@"CFBundleName"]
|
|
69
|
+
?: NSProcessInfo.processInfo.processName;
|
|
70
|
+
self.menu = [[NSMenu alloc] init];
|
|
71
|
+
NSMenuItem *quit = [[NSMenuItem alloc] initWithTitle:[@"Quit " stringByAppendingString:name]
|
|
72
|
+
action:@selector(terminate:)
|
|
73
|
+
keyEquivalent:@"q"];
|
|
74
|
+
quit.target = NSApp;
|
|
75
|
+
[self.menu addItem:quit];
|
|
76
|
+
|
|
77
|
+
[NSNotificationCenter.defaultCenter addObserver:self
|
|
78
|
+
selector:@selector(resigned:)
|
|
79
|
+
name:NSApplicationDidResignActiveNotification
|
|
80
|
+
object:nil];
|
|
81
|
+
|
|
82
|
+
self.timer = [NSTimer timerWithTimeInterval:kHiddenTick
|
|
83
|
+
target:self
|
|
84
|
+
selector:@selector(tickWhileHidden:)
|
|
85
|
+
userInfo:nil
|
|
86
|
+
repeats:YES];
|
|
87
|
+
self.timer.tolerance = kHiddenTick / 4;
|
|
88
|
+
[NSRunLoop.mainRunLoop addTimer:self.timer forMode:NSRunLoopCommonModes];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
- (void)setSymbol:(NSString *)symbol {
|
|
92
|
+
NSImage *image = [NSImage imageWithSystemSymbolName:symbol accessibilityDescription:nil];
|
|
93
|
+
if (!image) image = [NSImage imageWithSystemSymbolName:@"circle" accessibilityDescription:nil];
|
|
94
|
+
[image setTemplate:YES];
|
|
95
|
+
self.item.button.image = image;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
- (BOOL)shown {
|
|
99
|
+
return self.window.isVisible;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
- (void)clicked:(id)sender {
|
|
103
|
+
NSEvent *event = NSApp.currentEvent;
|
|
104
|
+
const BOOL secondary = event.type == NSEventTypeRightMouseUp || (event.modifierFlags & NSEventModifierFlagControl);
|
|
105
|
+
if (secondary) {
|
|
106
|
+
self.item.menu = self.menu;
|
|
107
|
+
[self.item.button performClick:nil];
|
|
108
|
+
self.item.menu = nil;
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (self.shown) [self hide];
|
|
112
|
+
else [self show];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
- (void)show {
|
|
116
|
+
NSWindow *window = self.window;
|
|
117
|
+
NSWindow *bar = self.item.button.window;
|
|
118
|
+
if (!window || !bar) return;
|
|
119
|
+
const NSRect anchor = bar.frame;
|
|
120
|
+
const NSSize size = window.frame.size;
|
|
121
|
+
const NSRect visible = (bar.screen ?: NSScreen.mainScreen).visibleFrame;
|
|
122
|
+
CGFloat x = NSMidX(anchor) - size.width / 2;
|
|
123
|
+
x = MAX(NSMinX(visible) + kPanelGap, MIN(x, NSMaxX(visible) - size.width - kPanelGap));
|
|
124
|
+
const CGFloat y = NSMinY(anchor) - size.height - kPanelGap;
|
|
125
|
+
[window setFrameOrigin:NSMakePoint(x, y)];
|
|
126
|
+
[NSApp activateIgnoringOtherApps:YES];
|
|
127
|
+
[window makeKeyAndOrderFront:nil];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
- (void)hide {
|
|
131
|
+
[self.window orderOut:nil];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
- (void)resigned:(NSNotification *)notification {
|
|
135
|
+
[self hide];
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
- (void)tickWhileHidden:(NSTimer *)timer {
|
|
139
|
+
if (self.shown) return;
|
|
140
|
+
id delegate = NSApp.delegate;
|
|
141
|
+
if ([delegate respondsToSelector:@selector(tick)]) [delegate performSelector:@selector(tick)];
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
@end
|
|
145
|
+
|
|
146
|
+
namespace gea::host::menu_bar_platform {
|
|
147
|
+
namespace {
|
|
148
|
+
|
|
149
|
+
GeaMenuBar *installed = nil;
|
|
150
|
+
|
|
151
|
+
NSString *nsString(const std::string &text) { return [NSString stringWithUTF8String:text.c_str()] ?: @""; }
|
|
152
|
+
|
|
153
|
+
} // namespace
|
|
154
|
+
|
|
155
|
+
bool supported() { return true; }
|
|
156
|
+
|
|
157
|
+
bool install(const std::string &symbol) {
|
|
158
|
+
if (installed) return true;
|
|
159
|
+
installed = [[GeaMenuBar alloc] init];
|
|
160
|
+
[installed installWithSymbol:nsString(symbol)];
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
void setSymbol(const std::string &symbol) { [installed setSymbol:nsString(symbol)]; }
|
|
165
|
+
|
|
166
|
+
bool shown() { return installed ? installed.shown : true; }
|
|
167
|
+
|
|
168
|
+
void show() { [installed show]; }
|
|
169
|
+
|
|
170
|
+
void hide() { [installed hide]; }
|
|
171
|
+
|
|
172
|
+
} // namespace gea::host::menu_bar_platform
|
|
173
|
+
|
|
174
|
+
#endif
|
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/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,10 @@
|
|
|
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_bar.h"
|
|
37
41
|
#include "host/notify.h"
|
|
38
42
|
#include "host/device_control.h"
|
|
39
43
|
#include <input.h>
|
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 {
|