/
sceUsbMic.cpp
479 lines (415 loc) · 13.4 KB
/
sceUsbMic.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// Copyright (c) 2019- PPSSPP Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include <mutex>
#include "Common/Serialize/Serializer.h"
#include "Common/Serialize/SerializeFuncs.h"
#include "Common/System/System.h"
#include "Core/HLE/HLE.h"
#include "Core/HLE/FunctionWrappers.h"
#include "Core/HLE/sceKernelThread.h"
#include "Core/HLE/sceUsbMic.h"
#include "Core/CoreTiming.h"
#include "Core/MemMapHelpers.h"
#if defined(_WIN32) && !PPSSPP_PLATFORM(UWP) && !defined(__LIBRETRO__)
#define HAVE_WIN32_MICROPHONE
#endif
#ifdef HAVE_WIN32_MICROPHONE
#define NOMINMAX
#include "Windows/CaptureDevice.h"
#endif
enum {
SCE_USBMIC_ERROR_INVALID_MAX_SAMPLES = 0x80243806,
SCE_USBMIC_ERROR_INVALID_SAMPLERATE = 0x8024380A,
};
int eventMicBlockingResume = -1;
static QueueBuf *audioBuf = nullptr;
static u32 numNeedSamples;
static std::vector<MicWaitInfo> waitingThreads;
static bool isNeedInput;
static u32 curSampleRate;
static u32 curChannels;
static u32 readMicDataLength;
static u32 curTargetAddr;
static int micState; // 0 means stopped, 1 means started, for save state.
static void __MicBlockingResume(u64 userdata, int cyclesLate) {
SceUID threadID = (SceUID)userdata;
u32 error;
int count = 0;
for (auto waitingThread : waitingThreads) {
if (waitingThread.threadID == threadID) {
SceUID waitID = __KernelGetWaitID(threadID, WAITTYPE_MICINPUT, error);
if (waitID == 0)
continue;
if (Microphone::isHaveDevice()) {
if (Microphone::getReadMicDataLength() >= waitingThread.needSize) {
u32 ret = __KernelGetWaitValue(threadID, error);
DEBUG_LOG(HLE, "sceUsbMic: Waking up thread(%d)", (int)waitingThread.threadID);
__KernelResumeThreadFromWait(threadID, ret);
waitingThreads.erase(waitingThreads.begin() + count);
} else {
u64 waitTimeus = (waitingThread.needSize - Microphone::getReadMicDataLength()) * 1000000 / 2 / waitingThread.sampleRate;
if(eventMicBlockingResume == -1)
eventMicBlockingResume = CoreTiming::RegisterEvent("MicBlockingResume", &__MicBlockingResume);
CoreTiming::ScheduleEvent(usToCycles(waitTimeus), eventMicBlockingResume, userdata);
}
} else {
for (u32 i = 0; i < waitingThread.needSize; i++) {
if (Memory::IsValidAddress(waitingThread.addr + i)) {
Memory::Write_U8(i & 0xFF, waitingThread.addr + i);
}
}
u32 ret = __KernelGetWaitValue(threadID, error);
DEBUG_LOG(HLE, "sceUsbMic: Waking up thread(%d)", (int)waitingThread.threadID);
__KernelResumeThreadFromWait(threadID, ret);
waitingThreads.erase(waitingThreads.begin() + count);
readMicDataLength += waitingThread.needSize;
}
}
++count;
}
}
void __UsbMicInit() {
if (audioBuf) {
delete audioBuf;
audioBuf = nullptr;
}
numNeedSamples = 0;
waitingThreads.clear();
isNeedInput = true;
curSampleRate = 44100;
curChannels = 1;
curTargetAddr = 0;
readMicDataLength = 0;
micState = 0;
eventMicBlockingResume = CoreTiming::RegisterEvent("MicBlockingResume", &__MicBlockingResume);
}
void __UsbMicShutdown() {
if (audioBuf) {
delete audioBuf;
audioBuf = nullptr;
}
Microphone::stopMic();
}
void __UsbMicDoState(PointerWrap &p) {
auto s = p.Section("sceUsbMic", 0, 3);
if (!s) {
return;
}
bool isMicStartedNow = Microphone::isMicStarted();
Do(p, numNeedSamples);
Do(p, waitingThreads);
Do(p, isNeedInput);
Do(p, curSampleRate);
Do(p, curChannels);
Do(p, micState);
if (s > 1) {
Do(p, eventMicBlockingResume);
if (eventMicBlockingResume != -1) {
CoreTiming::RestoreRegisterEvent(eventMicBlockingResume, "MicBlockingResume", &__MicBlockingResume);
}
} else {
eventMicBlockingResume = -1;
}
if (s > 2) {
Do(p, curTargetAddr);
Do(p, readMicDataLength);
}
if (!audioBuf && numNeedSamples > 0) {
audioBuf = new QueueBuf(numNeedSamples << 1);
}
if (micState == 0) {
if (isMicStartedNow)
Microphone::stopMic();
} else if (micState == 1) {
if (isMicStartedNow) {
// Ok, started.
} else {
Microphone::startMic(new std::vector<u32>({ curSampleRate, curChannels }));
}
}
}
QueueBuf::QueueBuf(u32 size) : available(0), end(0), capacity(size) {
buf_ = new u8[size];
}
QueueBuf::~QueueBuf() {
delete[] buf_;
}
QueueBuf::QueueBuf(const QueueBuf &buf) {
buf_ = new u8[buf.capacity];
memcpy(buf_, buf.buf_, buf.capacity);
available = buf.available;
end = buf.end;
capacity = buf.capacity;
}
QueueBuf& QueueBuf::operator=(const QueueBuf &buf) {
if (capacity < buf.capacity) {
resize(buf.capacity);
}
std::unique_lock<std::mutex> lock(mutex);
memcpy(buf_, buf.buf_, buf.capacity);
available = buf.available;
end = buf.end;
lock.unlock();
return *this;
}
u32 QueueBuf::push(u8 *buf, u32 size) {
u32 addedSize = 0;
if (size > capacity)
resize(size);
// This will overwrite the old data if the size prepare to add more than remaining size.
std::unique_lock<std::mutex> lock(mutex);
while (end + size > capacity) {
memcpy(buf_ + end, buf + addedSize, capacity - end);
addedSize += capacity - end;
size -= capacity - end;
end = 0;
}
memcpy(buf_ + end, buf + addedSize, size);
addedSize += size;
end = (end + size) % capacity;
available = std::min(capacity, available + addedSize);
lock.unlock();
return addedSize;
}
u32 QueueBuf::pop(u8 *buf, u32 size) {
u32 ret = 0;
if (getAvailableSize() < size)
size = getAvailableSize();
ret = size;
std::unique_lock<std::mutex> lock(mutex);
if (getStartPos() + size <= capacity) {
memcpy(buf, buf_ + getStartPos(), size);
} else {
memcpy(buf, buf_ + getStartPos(), capacity - getStartPos());
memcpy(buf + capacity - getStartPos(), buf_, size - (capacity - getStartPos()));
}
available -= size;
lock.unlock();
return ret;
}
void QueueBuf::resize(u32 newSize) {
if (capacity >= newSize) {
return;
}
u32 availableSize = getAvailableSize();
u8 *oldbuf = buf_;
buf_ = new u8[newSize];
pop(buf_, availableSize);
available = availableSize;
end = availableSize;
capacity = newSize;
delete[] oldbuf;
}
void QueueBuf::flush() {
std::unique_lock<std::mutex> lock(mutex);
available = 0;
end = 0;
lock.unlock();
}
u32 QueueBuf::getAvailableSize() {
return available;
}
u32 QueueBuf::getRemainingSize() {
return capacity - getAvailableSize();
}
u32 QueueBuf::getStartPos() {
return end >= available ? end - available : capacity - available + end;
}
static int sceUsbMicPollInputEnd() {
ERROR_LOG(HLE, "UNIMPL sceUsbMicPollInputEnd");
return 0;
}
static int sceUsbMicInputBlocking(u32 maxSamples, u32 sampleRate, u32 bufAddr) {
if (!Memory::IsValidAddress(bufAddr)) {
ERROR_LOG(HLE, "sceUsbMicInputBlocking(%d, %d, %08x): invalid addresses", maxSamples, sampleRate, bufAddr);
return -1;
}
INFO_LOG(HLE, "sceUsbMicInputBlocking: maxSamples: %d, samplerate: %d, bufAddr: %08x", maxSamples, sampleRate, bufAddr);
if (maxSamples <= 0 || (maxSamples & 0x3F) != 0) {
return SCE_USBMIC_ERROR_INVALID_MAX_SAMPLES;
}
if (sampleRate != 44100 && sampleRate != 22050 && sampleRate != 11025) {
return SCE_USBMIC_ERROR_INVALID_SAMPLERATE;
}
return __MicInput(maxSamples, sampleRate, bufAddr, USBMIC);
}
static int sceUsbMicInputInitEx(u32 paramAddr) {
ERROR_LOG(HLE, "UNIMPL sceUsbMicInputInitEx: %08x", paramAddr);
return 0;
}
static int sceUsbMicInput(u32 maxSamples, u32 sampleRate, u32 bufAddr) {
if (!Memory::IsValidAddress(bufAddr)) {
ERROR_LOG(HLE, "sceUsbMicInput(%d, %d, %08x): invalid addresses", maxSamples, sampleRate, bufAddr);
return -1;
}
ERROR_LOG(HLE, "UNTEST sceUsbMicInput: maxSamples: %d, samplerate: %d, bufAddr: %08x", maxSamples, sampleRate, bufAddr);
if (maxSamples <= 0 || (maxSamples & 0x3F) != 0) {
return SCE_USBMIC_ERROR_INVALID_MAX_SAMPLES;
}
if (sampleRate != 44100 && sampleRate != 22050 && sampleRate != 11025) {
return SCE_USBMIC_ERROR_INVALID_SAMPLERATE;
}
return __MicInput(maxSamples, sampleRate, bufAddr, USBMIC, false);
}
static int sceUsbMicGetInputLength() {
int ret = Microphone::getReadMicDataLength() / 2;
ERROR_LOG(HLE, "UNTEST sceUsbMicGetInputLength(ret: %d)", ret);
return ret;
}
static int sceUsbMicInputInit(int unknown1, int inputVolume, int unknown2) {
ERROR_LOG(HLE, "UNIMPL sceUsbMicInputInit(unknown1: %d, inputVolume: %d, unknown2: %d)", unknown1, inputVolume, unknown2);
return 0;
}
static int sceUsbMicWaitInputEnd() {
ERROR_LOG(HLE, "UNIMPL sceUsbMicWaitInputEnd");
return 0;
}
int Microphone::startMic(void *param) {
#ifdef HAVE_WIN32_MICROPHONE
if (winMic)
winMic->sendMessage({ CAPTUREDEVIDE_COMMAND::START, param });
#elif PPSSPP_PLATFORM(ANDROID)
std::vector<u32> *micParam = static_cast<std::vector<u32>*>(param);
int sampleRate = micParam->at(0);
int channels = micParam->at(1);
INFO_LOG(HLE, "microphone_command : sr = %d", sampleRate);
System_SendMessage("microphone_command", ("startRecording:" + std::to_string(sampleRate)).c_str());
#endif
micState = 1;
return 0;
}
int Microphone::stopMic() {
#ifdef HAVE_WIN32_MICROPHONE
if (winMic)
winMic->sendMessage({ CAPTUREDEVIDE_COMMAND::STOP, nullptr });
#elif PPSSPP_PLATFORM(ANDROID)
System_SendMessage("microphone_command", "stopRecording");
#endif
micState = 0;
return 0;
}
bool Microphone::isHaveDevice() {
#ifdef HAVE_WIN32_MICROPHONE
return winMic->getDeviceCounts() >= 1;
#elif PPSSPP_PLATFORM(ANDROID)
return audioRecording_Available();
#endif
return false;
}
bool Microphone::isMicStarted() {
return micState == 1;
}
// Deprecated.
bool Microphone::isNeedInput() {
return ::isNeedInput;
}
u32 Microphone::numNeedSamples() {
return ::numNeedSamples;
}
u32 Microphone::availableAudioBufSize() {
return audioBuf->getAvailableSize();
}
u32 Microphone::getReadMicDataLength() {
return ::readMicDataLength;
}
int Microphone::addAudioData(u8 *buf, u32 size) {
if (audioBuf)
audioBuf->push(buf, size);
else
return 0;
if (Memory::IsValidAddress(curTargetAddr)) {
u32 addSize = std::min(audioBuf->getAvailableSize(), numNeedSamples() * 2 - getReadMicDataLength());
u8 *tempbuf8 = new u8[addSize];
getAudioData(tempbuf8, addSize);
Memory::Memcpy(curTargetAddr + readMicDataLength, tempbuf8, addSize);
delete[] tempbuf8;
readMicDataLength += addSize;
}
return size;
}
u32 Microphone::getAudioData(u8 *buf, u32 size) {
if(audioBuf)
return audioBuf->pop(buf, size);
return 0;
}
void Microphone::flushAudioData() {
audioBuf->flush();
}
std::vector<std::string> Microphone::getDeviceList() {
#ifdef HAVE_WIN32_MICROPHONE
if (winMic) {
return winMic->getDeviceList();
}
#endif
return std::vector<std::string>();
}
void Microphone::onMicDeviceChange() {
if (Microphone::isMicStarted()) {
Microphone::stopMic();
// Just use the last param.
Microphone::startMic(nullptr);
}
}
u32 __MicInput(u32 maxSamples, u32 sampleRate, u32 bufAddr, MICTYPE type, bool block) {
curSampleRate = sampleRate;
curChannels = 1;
curTargetAddr = bufAddr;
u32 size = maxSamples << 1;
if (!audioBuf) {
audioBuf = new QueueBuf(size);
} else {
audioBuf->resize(size);
}
if (!audioBuf)
return 0;
numNeedSamples = maxSamples;
readMicDataLength = 0;
if (!Microphone::isMicStarted()) {
std::vector<u32> *param = new std::vector<u32>({ sampleRate, 1 });
Microphone::startMic(param);
}
if (Microphone::availableAudioBufSize() > 0) {
u32 addSize = std::min(Microphone::availableAudioBufSize(), size);
u8 *tempbuf8 = new u8[addSize];
Microphone::getAudioData(tempbuf8, addSize);
Memory::Memcpy(curTargetAddr, tempbuf8, addSize);
delete[] tempbuf8;
readMicDataLength += addSize;
}
if (!block) {
return type == CAMERAMIC ? size : maxSamples;
}
u64 waitTimeus = (size - Microphone::availableAudioBufSize()) * 1000000 / 2 / sampleRate;
if (eventMicBlockingResume == -1)
eventMicBlockingResume = CoreTiming::RegisterEvent("MicBlockingResume", &__MicBlockingResume);
CoreTiming::ScheduleEvent(usToCycles(waitTimeus), eventMicBlockingResume, __KernelGetCurThread());
MicWaitInfo waitInfo = { __KernelGetCurThread(), bufAddr, size, sampleRate };
waitingThreads.push_back(waitInfo);
DEBUG_LOG(HLE, "MicInputBlocking: blocking thread(%d)", (int)__KernelGetCurThread());
__KernelWaitCurThread(WAITTYPE_MICINPUT, 1, size, 0, false, "blocking microphone");
return type == CAMERAMIC ? size : maxSamples;
}
const HLEFunction sceUsbMic[] =
{
{0x06128E42, &WrapI_V<sceUsbMicPollInputEnd>, "sceUsbMicPollInputEnd", 'i', "" },
{0x2E6DCDCD, &WrapI_UUU<sceUsbMicInputBlocking>, "sceUsbMicInputBlocking", 'i', "xxx" },
{0x45310F07, &WrapI_U<sceUsbMicInputInitEx>, "sceUsbMicInputInitEx", 'i', "x" },
{0x5F7F368D, &WrapI_UUU<sceUsbMicInput>, "sceUsbMicInput", 'i', "xxx" },
{0x63400E20, &WrapI_V<sceUsbMicGetInputLength>, "sceUsbMicGetInputLength", 'i', "" },
{0xB8E536EB, &WrapI_III<sceUsbMicInputInit>, "sceUsbMicInputInit", 'i', "iii" },
{0xF899001C, &WrapI_V<sceUsbMicWaitInputEnd>, "sceUsbMicWaitInputEnd", 'i', "" },
};
void Register_sceUsbMic()
{
RegisterModule("sceUsbMic", ARRAY_SIZE(sceUsbMic), sceUsbMic);
}