@geastack/host 0.1.8
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/LICENSE +202 -0
- package/README.md +25 -0
- package/audio_runtime.cpp +807 -0
- package/bluetooth.cpp +125 -0
- package/geolocation.cpp +43 -0
- package/host/apps.cpp +20 -0
- package/host/audio.cpp +240 -0
- package/host/ble.cpp +239 -0
- package/host/camera.cpp +193 -0
- package/host/display.cpp +230 -0
- package/host/fetch.cpp +1060 -0
- package/host/geolocation.cpp +31 -0
- package/host/gpio.cpp +55 -0
- package/host/http.cpp +349 -0
- package/host/image.cpp +335 -0
- package/host/imu.cpp +51 -0
- package/host/input.cpp +12 -0
- package/host/media.cpp +579 -0
- package/host/memory.cpp +91 -0
- package/host/rtc.cpp +212 -0
- package/host/rtc_esp.cpp +291 -0
- package/host/tile_loader.cpp +478 -0
- package/host/timers.cpp +327 -0
- package/host/touch.cpp +25 -0
- package/host/websocket.cpp +279 -0
- package/host/wifi.cpp +99 -0
- package/include/gea/embedded-host.h +38 -0
- package/include/host/apps.h +15 -0
- package/include/host/audio.h +158 -0
- package/include/host/backends.h +455 -0
- package/include/host/battery.h +25 -0
- package/include/host/camera.h +236 -0
- package/include/host/clock.h +45 -0
- package/include/host/device_control.h +23 -0
- package/include/host/display.h +266 -0
- package/include/host/display_orientation.h +291 -0
- package/include/host/fetch.h +310 -0
- package/include/host/gpio.h +36 -0
- package/include/host/http.h +161 -0
- package/include/host/image.h +144 -0
- package/include/host/input.h +14 -0
- package/include/host/media.h +165 -0
- package/include/host/memory.h +44 -0
- package/include/host/navigator.h +283 -0
- package/include/host/notify.h +41 -0
- package/include/host/profiler.h +49 -0
- package/include/host/rtc.h +174 -0
- package/include/host/storage.h +141 -0
- package/include/host/tile_loader.h +59 -0
- package/include/host/timers.h +41 -0
- package/include/host/touch.h +20 -0
- package/include/host/websocket.h +179 -0
- package/include/host/window.h +31 -0
- package/input.cpp +68 -0
- package/package.json +27 -0
- package/services/app_state.cpp +19 -0
- package/services/battery_service.cpp +19 -0
- package/services/bluetooth_service.cpp +37 -0
- package/services/diagnostics.cpp +233 -0
- package/services/diagnostics_connection.cpp +231 -0
- package/services/diagnostics_internal.h +15 -0
- package/services/diagnostics_server.cpp +135 -0
- package/services/mirror.cpp +364 -0
- package/services/network_services.cpp +62 -0
- package/services/runtime_log.cpp +23 -0
- package/services/settings_surface.h +221 -0
- package/wifi.cpp +185 -0
package/host/media.cpp
ADDED
|
@@ -0,0 +1,579 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#include "host/media.h"
|
|
3
|
+
|
|
4
|
+
#include <algorithm>
|
|
5
|
+
#include <atomic>
|
|
6
|
+
#include <cerrno>
|
|
7
|
+
#include <cstdint>
|
|
8
|
+
#include <cstdio>
|
|
9
|
+
#include <deque>
|
|
10
|
+
#include <mutex>
|
|
11
|
+
#include <string>
|
|
12
|
+
#include <unordered_map>
|
|
13
|
+
#include <vector>
|
|
14
|
+
|
|
15
|
+
#include "platform/file_cache.h"
|
|
16
|
+
|
|
17
|
+
#ifdef ESP_PLATFORM
|
|
18
|
+
#include "esp_log.h"
|
|
19
|
+
#endif
|
|
20
|
+
|
|
21
|
+
#if defined(_WIN32)
|
|
22
|
+
#include <direct.h>
|
|
23
|
+
#else
|
|
24
|
+
#include <sys/stat.h>
|
|
25
|
+
#include <sys/types.h>
|
|
26
|
+
#endif
|
|
27
|
+
|
|
28
|
+
namespace gea::host {
|
|
29
|
+
|
|
30
|
+
namespace {
|
|
31
|
+
|
|
32
|
+
struct TrackState {
|
|
33
|
+
std::string id;
|
|
34
|
+
std::string kind = "audio";
|
|
35
|
+
std::string readyState = "live";
|
|
36
|
+
bool enabled = true;
|
|
37
|
+
std::deque<std::int16_t> ringBuffer;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
struct StreamState {
|
|
41
|
+
std::string id;
|
|
42
|
+
NativeMediaTrackHandle audioTrack = 0;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
struct RecorderState {
|
|
46
|
+
std::string id;
|
|
47
|
+
NativeMediaStreamHandle stream = 0;
|
|
48
|
+
NativeMediaTrackHandle track = 0;
|
|
49
|
+
std::string state = "inactive";
|
|
50
|
+
std::string path;
|
|
51
|
+
std::string mimeType = "audio/wav";
|
|
52
|
+
std::vector<std::int16_t> pcm;
|
|
53
|
+
double sampleRate = 16000.0;
|
|
54
|
+
double channels = 1.0;
|
|
55
|
+
FILE *file = nullptr;
|
|
56
|
+
std::size_t samplesWritten = 0;
|
|
57
|
+
bool writeFailed = false;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
std::unordered_map<NativeMediaStreamHandle, StreamState> &streamTable() {
|
|
61
|
+
static std::unordered_map<NativeMediaStreamHandle, StreamState> table;
|
|
62
|
+
return table;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
std::unordered_map<NativeMediaTrackHandle, TrackState> &trackTable() {
|
|
66
|
+
static std::unordered_map<NativeMediaTrackHandle, TrackState> table;
|
|
67
|
+
return table;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
std::unordered_map<NativeMediaRecorderHandle, RecorderState> &recorderTable() {
|
|
71
|
+
static std::unordered_map<NativeMediaRecorderHandle, RecorderState> table;
|
|
72
|
+
return table;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
std::mutex &mediaMutex() {
|
|
76
|
+
static std::mutex m;
|
|
77
|
+
return m;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
std::atomic<NativeMediaStreamHandle> &nextStreamHandle() {
|
|
81
|
+
static std::atomic<NativeMediaStreamHandle> id{1};
|
|
82
|
+
return id;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
std::atomic<NativeMediaTrackHandle> &nextTrackHandle() {
|
|
86
|
+
static std::atomic<NativeMediaTrackHandle> id{1};
|
|
87
|
+
return id;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
std::atomic<NativeMediaRecorderHandle> &nextRecorderHandle() {
|
|
91
|
+
static std::atomic<NativeMediaRecorderHandle> id{1};
|
|
92
|
+
return id;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
std::string formatHandleId(const char *prefix, std::uint32_t handle) {
|
|
96
|
+
char buf[64];
|
|
97
|
+
std::snprintf(buf, sizeof(buf), "%s-%u", prefix, static_cast<unsigned>(handle));
|
|
98
|
+
return buf;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
constexpr std::size_t kCaptureChunkSamples = 2048;
|
|
102
|
+
constexpr std::size_t kMaxRingBufferSamples = 16 * kCaptureChunkSamples;
|
|
103
|
+
constexpr std::size_t kRecorderFlushSamples = kCaptureChunkSamples;
|
|
104
|
+
constexpr double kDefaultRecorderSampleRate = 16000.0;
|
|
105
|
+
|
|
106
|
+
#ifdef ESP_PLATFORM
|
|
107
|
+
constexpr const char *kMediaLogTag = "gea_media";
|
|
108
|
+
#endif
|
|
109
|
+
|
|
110
|
+
void makeParentDirs(const std::string &path) {
|
|
111
|
+
std::size_t pos = 1;
|
|
112
|
+
while ((pos = path.find('/', pos)) != std::string::npos) {
|
|
113
|
+
const std::string dir = path.substr(0, pos);
|
|
114
|
+
if (!dir.empty()) {
|
|
115
|
+
#if defined(_WIN32)
|
|
116
|
+
_mkdir(dir.c_str());
|
|
117
|
+
#else
|
|
118
|
+
mkdir(dir.c_str(), 0775);
|
|
119
|
+
#endif
|
|
120
|
+
}
|
|
121
|
+
++pos;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
void writeLe16(FILE *file, std::uint16_t value) {
|
|
126
|
+
std::uint8_t bytes[2] = {
|
|
127
|
+
static_cast<std::uint8_t>(value & 0xffu),
|
|
128
|
+
static_cast<std::uint8_t>((value >> 8) & 0xffu),
|
|
129
|
+
};
|
|
130
|
+
std::fwrite(bytes, 1, sizeof(bytes), file);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
void writeLe32(FILE *file, std::uint32_t value) {
|
|
134
|
+
std::uint8_t bytes[4] = {
|
|
135
|
+
static_cast<std::uint8_t>(value & 0xffu),
|
|
136
|
+
static_cast<std::uint8_t>((value >> 8) & 0xffu),
|
|
137
|
+
static_cast<std::uint8_t>((value >> 16) & 0xffu),
|
|
138
|
+
static_cast<std::uint8_t>((value >> 24) & 0xffu),
|
|
139
|
+
};
|
|
140
|
+
std::fwrite(bytes, 1, sizeof(bytes), file);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
std::uint32_t wavDataBytesForSamples(std::size_t sampleCount) {
|
|
144
|
+
const auto bytes = sampleCount * sizeof(std::int16_t);
|
|
145
|
+
return static_cast<std::uint32_t>(bytes);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
void writeWavHeader(FILE *file, std::uint32_t dataBytes, int sampleRate, int channels) {
|
|
149
|
+
const std::uint32_t riffBytes = 36u + dataBytes;
|
|
150
|
+
const std::uint16_t channelCount = static_cast<std::uint16_t>(channels <= 0 ? 1 : channels);
|
|
151
|
+
const std::uint32_t rate = static_cast<std::uint32_t>(sampleRate <= 0 ? kDefaultRecorderSampleRate : sampleRate);
|
|
152
|
+
const std::uint16_t bitsPerSample = 16;
|
|
153
|
+
const std::uint16_t blockAlign = static_cast<std::uint16_t>(channelCount * (bitsPerSample / 8));
|
|
154
|
+
const std::uint32_t byteRate = rate * blockAlign;
|
|
155
|
+
|
|
156
|
+
std::fwrite("RIFF", 1, 4, file);
|
|
157
|
+
writeLe32(file, riffBytes);
|
|
158
|
+
std::fwrite("WAVE", 1, 4, file);
|
|
159
|
+
std::fwrite("fmt ", 1, 4, file);
|
|
160
|
+
writeLe32(file, 16);
|
|
161
|
+
writeLe16(file, 1);
|
|
162
|
+
writeLe16(file, channelCount);
|
|
163
|
+
writeLe32(file, rate);
|
|
164
|
+
writeLe32(file, byteRate);
|
|
165
|
+
writeLe16(file, blockAlign);
|
|
166
|
+
writeLe16(file, bitsPerSample);
|
|
167
|
+
std::fwrite("data", 1, 4, file);
|
|
168
|
+
writeLe32(file, dataBytes);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
bool patchWavHeader(FILE *file, std::uint32_t dataBytes) {
|
|
172
|
+
if (!file) return false;
|
|
173
|
+
if (std::fflush(file) != 0) return false;
|
|
174
|
+
const std::uint32_t riffBytes = 36u + dataBytes;
|
|
175
|
+
if (std::fseek(file, 4, SEEK_SET) != 0) return false;
|
|
176
|
+
writeLe32(file, riffBytes);
|
|
177
|
+
if (std::fseek(file, 40, SEEK_SET) != 0) return false;
|
|
178
|
+
writeLe32(file, dataBytes);
|
|
179
|
+
return std::fflush(file) == 0;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
double writeWavFile(const std::string &path, const std::vector<std::int16_t> &samples, int sampleRate, int channels) {
|
|
183
|
+
if (path.empty()) return 0.0;
|
|
184
|
+
gea::platform::storage::ensureMounted();
|
|
185
|
+
makeParentDirs(path);
|
|
186
|
+
|
|
187
|
+
FILE *file = std::fopen(path.c_str(), "wb");
|
|
188
|
+
if (!file) {
|
|
189
|
+
#ifdef ESP_PLATFORM
|
|
190
|
+
ESP_LOGE(kMediaLogTag, "failed to open WAV for write path=%s errno=%d", path.c_str(), errno);
|
|
191
|
+
#endif
|
|
192
|
+
return 0.0;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const std::uint32_t dataBytes = wavDataBytesForSamples(samples.size());
|
|
196
|
+
writeWavHeader(file, dataBytes, sampleRate, channels);
|
|
197
|
+
std::size_t writtenSamples = 0;
|
|
198
|
+
while (writtenSamples < samples.size()) {
|
|
199
|
+
const std::size_t remaining = samples.size() - writtenSamples;
|
|
200
|
+
const std::size_t chunkSamples = std::min<std::size_t>(remaining, 2048);
|
|
201
|
+
const std::size_t written = std::fwrite(samples.data() + writtenSamples, sizeof(std::int16_t), chunkSamples, file);
|
|
202
|
+
writtenSamples += written;
|
|
203
|
+
if (written != chunkSamples) break;
|
|
204
|
+
}
|
|
205
|
+
const std::uint32_t actualDataBytes = static_cast<std::uint32_t>(writtenSamples * sizeof(std::int16_t));
|
|
206
|
+
if (actualDataBytes != dataBytes) {
|
|
207
|
+
patchWavHeader(file, actualDataBytes);
|
|
208
|
+
#ifdef ESP_PLATFORM
|
|
209
|
+
ESP_LOGW(kMediaLogTag,
|
|
210
|
+
"partial WAV write path=%s samples=%u/%u errno=%d",
|
|
211
|
+
path.c_str(),
|
|
212
|
+
static_cast<unsigned>(writtenSamples),
|
|
213
|
+
static_cast<unsigned>(samples.size()),
|
|
214
|
+
errno);
|
|
215
|
+
#endif
|
|
216
|
+
}
|
|
217
|
+
const bool ok = std::fclose(file) == 0;
|
|
218
|
+
if (!ok) {
|
|
219
|
+
#ifdef ESP_PLATFORM
|
|
220
|
+
ESP_LOGE(kMediaLogTag, "failed to close WAV path=%s errno=%d", path.c_str(), errno);
|
|
221
|
+
#endif
|
|
222
|
+
return 0.0;
|
|
223
|
+
}
|
|
224
|
+
return static_cast<double>(44u + actualDataBytes);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
bool openRecorderFile(RecorderState &recorder) {
|
|
228
|
+
if (recorder.path.empty()) return false;
|
|
229
|
+
gea::platform::storage::ensureMounted();
|
|
230
|
+
makeParentDirs(recorder.path);
|
|
231
|
+
recorder.file = std::fopen(recorder.path.c_str(), "wb");
|
|
232
|
+
recorder.samplesWritten = 0;
|
|
233
|
+
recorder.writeFailed = false;
|
|
234
|
+
if (!recorder.file) {
|
|
235
|
+
recorder.writeFailed = true;
|
|
236
|
+
#ifdef ESP_PLATFORM
|
|
237
|
+
ESP_LOGE(kMediaLogTag, "failed to open recorder WAV path=%s errno=%d", recorder.path.c_str(), errno);
|
|
238
|
+
#endif
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
writeWavHeader(recorder.file, 0, static_cast<int>(recorder.sampleRate), static_cast<int>(recorder.channels));
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
void closeRecorderFile(RecorderState &recorder) {
|
|
246
|
+
if (!recorder.file) return;
|
|
247
|
+
std::fclose(recorder.file);
|
|
248
|
+
recorder.file = nullptr;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
bool flushRecorderPcm(RecorderState &recorder) {
|
|
252
|
+
if (!recorder.file || recorder.pcm.empty() || recorder.writeFailed) return !recorder.writeFailed;
|
|
253
|
+
std::size_t writtenSamples = 0;
|
|
254
|
+
while (writtenSamples < recorder.pcm.size()) {
|
|
255
|
+
const std::size_t remaining = recorder.pcm.size() - writtenSamples;
|
|
256
|
+
const std::size_t chunkSamples = std::min<std::size_t>(remaining, 2048);
|
|
257
|
+
const std::size_t written =
|
|
258
|
+
std::fwrite(recorder.pcm.data() + writtenSamples, sizeof(std::int16_t), chunkSamples, recorder.file);
|
|
259
|
+
recorder.samplesWritten += written;
|
|
260
|
+
writtenSamples += written;
|
|
261
|
+
if (written != chunkSamples) {
|
|
262
|
+
recorder.writeFailed = true;
|
|
263
|
+
#ifdef ESP_PLATFORM
|
|
264
|
+
ESP_LOGW(kMediaLogTag,
|
|
265
|
+
"recorder WAV write stopped path=%s samples=%u errno=%d",
|
|
266
|
+
recorder.path.c_str(),
|
|
267
|
+
static_cast<unsigned>(recorder.samplesWritten),
|
|
268
|
+
errno);
|
|
269
|
+
#endif
|
|
270
|
+
recorder.pcm.clear();
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
recorder.pcm.clear();
|
|
275
|
+
return true;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
void appendRecorderPcm(RecorderState &recorder, const std::int16_t *samples, std::size_t count) {
|
|
279
|
+
if (!recorder.file || recorder.writeFailed || !samples || count == 0) return;
|
|
280
|
+
recorder.pcm.insert(recorder.pcm.end(), samples, samples + count);
|
|
281
|
+
if (recorder.pcm.size() >= kRecorderFlushSamples) flushRecorderPcm(recorder);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
double finalizeRecorderFile(RecorderState &recorder) {
|
|
285
|
+
flushRecorderPcm(recorder);
|
|
286
|
+
if (!recorder.file) return 0.0;
|
|
287
|
+
const auto dataBytes = wavDataBytesForSamples(recorder.samplesWritten);
|
|
288
|
+
const bool patched = patchWavHeader(recorder.file, dataBytes);
|
|
289
|
+
const bool closed = std::fclose(recorder.file) == 0;
|
|
290
|
+
recorder.file = nullptr;
|
|
291
|
+
if (!patched || !closed) {
|
|
292
|
+
recorder.writeFailed = true;
|
|
293
|
+
#ifdef ESP_PLATFORM
|
|
294
|
+
ESP_LOGE(kMediaLogTag,
|
|
295
|
+
"failed to finalize recorder WAV path=%s patched=%d closed=%d errno=%d",
|
|
296
|
+
recorder.path.c_str(),
|
|
297
|
+
patched ? 1 : 0,
|
|
298
|
+
closed ? 1 : 0,
|
|
299
|
+
errno);
|
|
300
|
+
#endif
|
|
301
|
+
return 0.0;
|
|
302
|
+
}
|
|
303
|
+
return static_cast<double>(44u + dataBytes);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
std::vector<std::uint8_t> readFileBytes(const std::string &path) {
|
|
307
|
+
if (path.empty()) return {};
|
|
308
|
+
gea::platform::storage::ensureMounted();
|
|
309
|
+
FILE *file = std::fopen(path.c_str(), "rb");
|
|
310
|
+
if (!file) return {};
|
|
311
|
+
std::fseek(file, 0, SEEK_END);
|
|
312
|
+
const long size = std::ftell(file);
|
|
313
|
+
if (size <= 0) {
|
|
314
|
+
std::fclose(file);
|
|
315
|
+
return {};
|
|
316
|
+
}
|
|
317
|
+
std::fseek(file, 0, SEEK_SET);
|
|
318
|
+
std::vector<std::uint8_t> out(static_cast<std::size_t>(size));
|
|
319
|
+
const std::size_t read = std::fread(out.data(), 1, out.size(), file);
|
|
320
|
+
std::fclose(file);
|
|
321
|
+
out.resize(read);
|
|
322
|
+
return out;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
} // namespace
|
|
326
|
+
|
|
327
|
+
namespace media {
|
|
328
|
+
|
|
329
|
+
#ifdef ESP_PLATFORM
|
|
330
|
+
void platform_attach_track(NativeMediaTrackHandle handle);
|
|
331
|
+
void platform_detach_track(NativeMediaTrackHandle handle);
|
|
332
|
+
#else
|
|
333
|
+
__attribute__((weak)) void platform_attach_track(NativeMediaTrackHandle) {}
|
|
334
|
+
__attribute__((weak)) void platform_detach_track(NativeMediaTrackHandle) {}
|
|
335
|
+
#endif
|
|
336
|
+
|
|
337
|
+
NativeMediaStreamHandle create_stream() {
|
|
338
|
+
auto streamHandle = nextStreamHandle()++;
|
|
339
|
+
auto trackHandle = nextTrackHandle()++;
|
|
340
|
+
{
|
|
341
|
+
std::lock_guard<std::mutex> lock(mediaMutex());
|
|
342
|
+
trackTable()[trackHandle] = TrackState{
|
|
343
|
+
formatHandleId("track", trackHandle), "audio", "live", true, {}};
|
|
344
|
+
streamTable()[streamHandle] = StreamState{
|
|
345
|
+
formatHandleId("stream", streamHandle), trackHandle};
|
|
346
|
+
}
|
|
347
|
+
platform_attach_track(trackHandle);
|
|
348
|
+
return streamHandle;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
NativeMediaTrackHandle stream_audio_track(NativeMediaStreamHandle handle) {
|
|
352
|
+
std::lock_guard<std::mutex> lock(mediaMutex());
|
|
353
|
+
auto it = streamTable().find(handle);
|
|
354
|
+
return it == streamTable().end() ? 0 : it->second.audioTrack;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
void destroy_stream(NativeMediaStreamHandle handle) {
|
|
358
|
+
NativeMediaTrackHandle trackHandle = 0;
|
|
359
|
+
{
|
|
360
|
+
std::lock_guard<std::mutex> lock(mediaMutex());
|
|
361
|
+
auto it = streamTable().find(handle);
|
|
362
|
+
if (it == streamTable().end()) return;
|
|
363
|
+
trackHandle = it->second.audioTrack;
|
|
364
|
+
for (auto &entry : recorderTable()) {
|
|
365
|
+
if (entry.second.track == trackHandle && entry.second.state == "recording") {
|
|
366
|
+
finalizeRecorderFile(entry.second);
|
|
367
|
+
entry.second.state = "inactive";
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
trackTable().erase(trackHandle);
|
|
371
|
+
streamTable().erase(it);
|
|
372
|
+
}
|
|
373
|
+
if (trackHandle) platform_detach_track(trackHandle);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
void track_inject_pcm(NativeMediaTrackHandle handle, const std::int16_t *samples, std::size_t count) {
|
|
377
|
+
std::lock_guard<std::mutex> lock(mediaMutex());
|
|
378
|
+
auto it = trackTable().find(handle);
|
|
379
|
+
if (it == trackTable().end() || !it->second.enabled) return;
|
|
380
|
+
auto &buffer = it->second.ringBuffer;
|
|
381
|
+
for (std::size_t i = 0; i < count; ++i) {
|
|
382
|
+
if (buffer.size() >= kMaxRingBufferSamples) buffer.pop_front();
|
|
383
|
+
buffer.push_back(samples[i]);
|
|
384
|
+
}
|
|
385
|
+
for (auto &entry : recorderTable()) {
|
|
386
|
+
auto &recorder = entry.second;
|
|
387
|
+
if (recorder.track == handle && recorder.state == "recording") {
|
|
388
|
+
appendRecorderPcm(recorder, samples, count);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
std::size_t track_read_pcm(NativeMediaTrackHandle handle, std::int16_t *samples, std::size_t max_samples) {
|
|
394
|
+
std::lock_guard<std::mutex> lock(mediaMutex());
|
|
395
|
+
auto it = trackTable().find(handle);
|
|
396
|
+
if (it == trackTable().end()) return 0;
|
|
397
|
+
auto &buffer = it->second.ringBuffer;
|
|
398
|
+
std::size_t copied = 0;
|
|
399
|
+
while (copied < max_samples && !buffer.empty()) {
|
|
400
|
+
samples[copied++] = buffer.front();
|
|
401
|
+
buffer.pop_front();
|
|
402
|
+
}
|
|
403
|
+
return copied;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
NativeMediaStreamHandle get_user_media_audio() {
|
|
407
|
+
return create_stream();
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
NativeMediaRecorderHandle create_recorder(NativeMediaStreamHandle stream, const std::string &path, const std::string &mimeType) {
|
|
411
|
+
const auto recorderHandle = nextRecorderHandle()++;
|
|
412
|
+
const auto trackHandle = stream_audio_track(stream);
|
|
413
|
+
std::string effectivePath = path;
|
|
414
|
+
if (effectivePath.empty()) {
|
|
415
|
+
char buf[96];
|
|
416
|
+
std::snprintf(buf, sizeof(buf), "/sdcard/recordings/recording_%03u.wav", static_cast<unsigned>(recorderHandle));
|
|
417
|
+
effectivePath = buf;
|
|
418
|
+
}
|
|
419
|
+
{
|
|
420
|
+
std::lock_guard<std::mutex> lock(mediaMutex());
|
|
421
|
+
recorderTable()[recorderHandle] = RecorderState{
|
|
422
|
+
formatHandleId("recorder", recorderHandle),
|
|
423
|
+
stream,
|
|
424
|
+
trackHandle,
|
|
425
|
+
"inactive",
|
|
426
|
+
effectivePath,
|
|
427
|
+
mimeType.empty() ? std::string("audio/wav") : mimeType,
|
|
428
|
+
{},
|
|
429
|
+
kDefaultRecorderSampleRate,
|
|
430
|
+
1.0,
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
return recorderHandle;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
std::string recorder_state(NativeMediaRecorderHandle handle) {
|
|
437
|
+
std::lock_guard<std::mutex> lock(mediaMutex());
|
|
438
|
+
auto it = recorderTable().find(handle);
|
|
439
|
+
return it == recorderTable().end() ? std::string("inactive") : it->second.state;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
std::string recorder_mime_type(NativeMediaRecorderHandle handle) {
|
|
443
|
+
std::lock_guard<std::mutex> lock(mediaMutex());
|
|
444
|
+
auto it = recorderTable().find(handle);
|
|
445
|
+
return it == recorderTable().end() ? std::string("audio/wav") : it->second.mimeType;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
void recorder_start(NativeMediaRecorderHandle handle) {
|
|
449
|
+
std::lock_guard<std::mutex> lock(mediaMutex());
|
|
450
|
+
auto it = recorderTable().find(handle);
|
|
451
|
+
if (it == recorderTable().end()) return;
|
|
452
|
+
closeRecorderFile(it->second);
|
|
453
|
+
it->second.pcm.clear();
|
|
454
|
+
it->second.samplesWritten = 0;
|
|
455
|
+
it->second.writeFailed = false;
|
|
456
|
+
if (openRecorderFile(it->second)) {
|
|
457
|
+
it->second.state = "recording";
|
|
458
|
+
} else {
|
|
459
|
+
it->second.state = "inactive";
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
GeaAudioBlob recorder_stop(NativeMediaRecorderHandle handle) {
|
|
464
|
+
GeaAudioBlob blob;
|
|
465
|
+
std::lock_guard<std::mutex> lock(mediaMutex());
|
|
466
|
+
auto it = recorderTable().find(handle);
|
|
467
|
+
if (it == recorderTable().end()) return {};
|
|
468
|
+
it->second.state = "inactive";
|
|
469
|
+
blob.path = it->second.path;
|
|
470
|
+
blob.type = it->second.mimeType.empty() ? std::string("audio/wav") : it->second.mimeType;
|
|
471
|
+
blob.size = finalizeRecorderFile(it->second);
|
|
472
|
+
return blob;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
} // namespace media
|
|
476
|
+
|
|
477
|
+
std::vector<std::uint8_t> GeaAudioBlob::arrayBuffer() const {
|
|
478
|
+
return readFileBytes(path);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
std::string GeaAudioBlob::text() const {
|
|
482
|
+
auto bytes = readFileBytes(path);
|
|
483
|
+
return std::string(bytes.begin(), bytes.end());
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
MediaRecorderStateProperty::operator std::string() const {
|
|
487
|
+
return media::recorder_state(nativeHandle);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
MediaRecorder::MediaRecorder(MediaStream mediaStream)
|
|
491
|
+
: MediaRecorder(mediaStream, std::string{}, std::string("audio/wav")) {}
|
|
492
|
+
|
|
493
|
+
MediaRecorder::MediaRecorder(MediaStream mediaStream, const std::string &path, const std::string &mimeType)
|
|
494
|
+
: nativeHandle(media::create_recorder(mediaStream.nativeHandle, path, mimeType)),
|
|
495
|
+
stream(mediaStream),
|
|
496
|
+
state(nativeHandle) {}
|
|
497
|
+
|
|
498
|
+
std::string MediaRecorder::mimeType() const {
|
|
499
|
+
return media::recorder_mime_type(nativeHandle);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
void MediaRecorder::start(double timesliceMs) const {
|
|
503
|
+
(void)timesliceMs;
|
|
504
|
+
media::recorder_start(nativeHandle);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
void MediaRecorder::stop() const {
|
|
508
|
+
const auto blob = media::recorder_stop(nativeHandle);
|
|
509
|
+
if (ondataavailable) ondataavailable(MediaRecorderDataEvent{blob});
|
|
510
|
+
if (onstop) onstop();
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
std::string MediaStreamTrack::id() const {
|
|
514
|
+
std::lock_guard<std::mutex> lock(mediaMutex());
|
|
515
|
+
auto it = trackTable().find(nativeHandle);
|
|
516
|
+
return it == trackTable().end() ? std::string{} : it->second.id;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
std::string MediaStreamTrack::kind() const {
|
|
520
|
+
std::lock_guard<std::mutex> lock(mediaMutex());
|
|
521
|
+
auto it = trackTable().find(nativeHandle);
|
|
522
|
+
return it == trackTable().end() ? std::string{} : it->second.kind;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
std::string MediaStreamTrack::readyState() const {
|
|
526
|
+
std::lock_guard<std::mutex> lock(mediaMutex());
|
|
527
|
+
auto it = trackTable().find(nativeHandle);
|
|
528
|
+
return it == trackTable().end() ? std::string("ended") : it->second.readyState;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
bool MediaStreamTrack::enabled() const {
|
|
532
|
+
std::lock_guard<std::mutex> lock(mediaMutex());
|
|
533
|
+
auto it = trackTable().find(nativeHandle);
|
|
534
|
+
return it == trackTable().end() ? false : it->second.enabled;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
void MediaStreamTrack::setEnabled(bool value) const {
|
|
538
|
+
std::lock_guard<std::mutex> lock(mediaMutex());
|
|
539
|
+
auto it = trackTable().find(nativeHandle);
|
|
540
|
+
if (it != trackTable().end()) it->second.enabled = value;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
void MediaStreamTrack::stop() const {
|
|
544
|
+
bool shouldDetach = false;
|
|
545
|
+
{
|
|
546
|
+
std::lock_guard<std::mutex> lock(mediaMutex());
|
|
547
|
+
auto it = trackTable().find(nativeHandle);
|
|
548
|
+
if (it != trackTable().end()) {
|
|
549
|
+
shouldDetach = it->second.readyState != "ended";
|
|
550
|
+
it->second.readyState = "ended";
|
|
551
|
+
it->second.enabled = false;
|
|
552
|
+
for (auto &entry : recorderTable()) {
|
|
553
|
+
if (entry.second.track == nativeHandle && entry.second.state == "recording") {
|
|
554
|
+
entry.second.state = "inactive";
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
if (shouldDetach) media::platform_detach_track(nativeHandle);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
std::string MediaStream::id() const {
|
|
563
|
+
std::lock_guard<std::mutex> lock(mediaMutex());
|
|
564
|
+
auto it = streamTable().find(nativeHandle);
|
|
565
|
+
return it == streamTable().end() ? std::string{} : it->second.id;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
std::vector<MediaStreamTrack> MediaStream::getAudioTracks() const {
|
|
569
|
+
std::lock_guard<std::mutex> lock(mediaMutex());
|
|
570
|
+
auto it = streamTable().find(nativeHandle);
|
|
571
|
+
if (it == streamTable().end() || it->second.audioTrack == 0) return {};
|
|
572
|
+
return {MediaStreamTrack(it->second.audioTrack)};
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
std::vector<MediaStreamTrack> MediaStream::getTracks() const {
|
|
576
|
+
return getAudioTracks();
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
} // namespace gea::host
|
package/host/memory.cpp
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
#include "gea/embedded-host.h"
|
|
3
|
+
#include "memory.h"
|
|
4
|
+
|
|
5
|
+
namespace gea::framework::memory {
|
|
6
|
+
|
|
7
|
+
double MemoryBackend::internalFree() {
|
|
8
|
+
return static_cast<double>(gea::platform::memory::Memory::internalFree());
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
double MemoryBackend::internalLargestFreeBlock() {
|
|
12
|
+
return static_cast<double>(gea::platform::memory::Memory::internalLargestFreeBlock());
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
double MemoryBackend::internalMinimumFree() {
|
|
16
|
+
return static_cast<double>(gea::platform::memory::Memory::internalMinimumFree());
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
double MemoryBackend::psramFree() {
|
|
20
|
+
return static_cast<double>(gea::platform::memory::Memory::psramFree());
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
double MemoryBackend::currentTaskStackHighWaterMark() {
|
|
24
|
+
return static_cast<double>(gea::platform::memory::Memory::currentTaskStackHighWaterMark());
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
double MemoryBackend::geaMainStackBytes() {
|
|
28
|
+
return static_cast<double>(gea::platform::memory::Memory::geaMainStackBytes());
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
double MemoryBackend::geaInitStackBytes() {
|
|
32
|
+
return static_cast<double>(gea::platform::memory::Memory::geaInitStackBytes());
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
double MemoryBackend::appFrameStackWords() {
|
|
36
|
+
return static_cast<double>(gea::platform::memory::Memory::appFrameStackWords());
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
double MemoryBackend::appFrameStackBytes() {
|
|
40
|
+
return static_cast<double>(gea::platform::memory::Memory::appFrameStackBytes());
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
double MemoryBackend::displayFlushConfiguredRows() {
|
|
44
|
+
return static_cast<double>(gea::platform::memory::Memory::displayFlushConfiguredRows());
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
double MemoryBackend::displayFlushConfiguredDepth() {
|
|
48
|
+
return static_cast<double>(gea::platform::memory::Memory::displayFlushConfiguredDepth());
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
double MemoryBackend::displayFlushBufferMaxBytes() {
|
|
52
|
+
return static_cast<double>(gea::platform::memory::Memory::displayFlushBufferMaxBytes());
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
double MemoryBackend::displayFlushRows() {
|
|
56
|
+
return static_cast<double>(gea::platform::memory::Memory::displayFlushRows());
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
double MemoryBackend::displayFlushDepth() {
|
|
60
|
+
return static_cast<double>(gea::platform::memory::Memory::displayFlushDepth());
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
double MemoryBackend::displayFlushBufferBytes() {
|
|
64
|
+
return static_cast<double>(gea::platform::memory::Memory::displayFlushBufferBytes());
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
double MemoryBackend::allocationSramCount() {
|
|
68
|
+
return 0.0;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
double MemoryBackend::allocationPsramCount() {
|
|
72
|
+
return 0.0;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
double MemoryBackend::allocationSramBytes() {
|
|
76
|
+
return 0.0;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
double MemoryBackend::allocationPsramBytes() {
|
|
80
|
+
return 0.0;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
double MemoryBackend::allocationSramPeakBytes() {
|
|
84
|
+
return 0.0;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
double MemoryBackend::allocationPsramPeakBytes() {
|
|
88
|
+
return 0.0;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
} // namespace gea::framework::memory
|