Skip to content

Commit efa2866

Browse files
committed
windows: use the real-time work queue API
1 parent 08d4be0 commit efa2866

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ project(libgral)
33

44
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
55
add_library(gral gral_windows.cpp)
6-
target_link_libraries(gral d2d1 dwrite windowsapp)
6+
target_link_libraries(gral d2d1 dwrite rtworkq windowsapp)
77
target_compile_definitions(gral PUBLIC GRAL_WINDOWS)
88
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
99
find_library(COCOA Cocoa)

gral_windows.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
2121
#include <dwrite.h>
2222
#include <stdlib.h>
2323
#include <mmdeviceapi.h>
24+
#include <rtworkq.h>
2425
#include <audioclient.h>
2526
#include <roapi.h>
2627
#include <windows.foundation.h>
@@ -1507,9 +1508,17 @@ struct gral_audio {
15071508
HANDLE exit_event;
15081509
};
15091510

1510-
static DWORD WINAPI audio_thread(LPVOID user_data) {
1511-
gral_audio *audio = (gral_audio *)user_data;
1512-
CoInitialize(NULL);
1511+
class AudioCallback: public IRtwqAsyncCallback {
1512+
1513+
1514+
};
1515+
1516+
gral_audio *gral_audio_create(char const *name, void (*callback)(float *buffer, int frames, void *user_data), void *user_data) {
1517+
gral_audio *audio = new gral_audio();
1518+
audio->callback = callback;
1519+
audio->user_data = user_data;
1520+
//audio->exit_event = CreateEvent(NULL, FALSE, FALSE, NULL);
1521+
//audio->thread = CreateThread(NULL, 0, &audio_thread, audio, 0, NULL);
15131522
ComPointer<IMMDeviceEnumerator> device_enumerator;
15141523
CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void **)&device_enumerator);
15151524
ComPointer<IMMDevice> device;
@@ -1530,13 +1539,19 @@ static DWORD WINAPI audio_thread(LPVOID user_data) {
15301539
IAudioRenderClient *render_client;
15311540
audio_client->GetService(__uuidof(IAudioRenderClient), (void **)&render_client);
15321541
HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL);
1542+
DWORD task_id = 0;
1543+
DWORD id;
1544+
RtwqLockSharedWorkQueue(L"Pro Audio", 0, &task_id, &id);
1545+
ComPointer<IRtwqAsyncResult> async_result;
1546+
RtwqCreateAsyncResult(NULL, NULL, NULL, &async_result);
1547+
RtwqPutWaitingWorkItem(event, 1, async_result, NULL);
15331548
audio_client->SetEventHandle(event);
15341549
BYTE *buffer;
15351550
render_client->GetBuffer(buffer_size, &buffer);
15361551
audio->callback((float *)buffer, buffer_size, audio->user_data);
15371552
render_client->ReleaseBuffer(buffer_size, 0);
15381553
audio_client->Start();
1539-
HANDLE events[] = {event, audio->exit_event};
1554+
/*HANDLE events[] = {event, audio->exit_event};
15401555
UINT32 padding;
15411556
while (WaitForMultipleObjects(2, events, FALSE, INFINITE) != WAIT_OBJECT_0 + 1) {
15421557
audio_client->GetCurrentPadding(&padding);
@@ -1545,28 +1560,18 @@ static DWORD WINAPI audio_thread(LPVOID user_data) {
15451560
audio->callback((float *)buffer, buffer_size - padding, audio->user_data);
15461561
render_client->ReleaseBuffer(buffer_size - padding, 0);
15471562
}
1548-
}
1563+
}*/
15491564
audio_client->Stop();
15501565
CloseHandle(event);
15511566
render_client->Release();
1552-
CoUninitialize();
1553-
return 0;
1554-
}
1555-
1556-
gral_audio *gral_audio_create(char const *name, void (*callback)(float *buffer, int frames, void *user_data), void *user_data) {
1557-
gral_audio *audio = new gral_audio();
1558-
audio->callback = callback;
1559-
audio->user_data = user_data;
1560-
audio->exit_event = CreateEvent(NULL, FALSE, FALSE, NULL);
1561-
audio->thread = CreateThread(NULL, 0, &audio_thread, audio, 0, NULL);
15621567
return audio;
15631568
}
15641569

15651570
void gral_audio_delete(gral_audio *audio) {
1566-
SetEvent(audio->exit_event);
1571+
/*SetEvent(audio->exit_event);
15671572
WaitForSingleObject(audio->thread, INFINITE);
15681573
CloseHandle(audio->exit_event);
1569-
CloseHandle(audio->thread);
1574+
CloseHandle(audio->thread);*/
15701575
delete audio;
15711576
}
15721577

0 commit comments

Comments
 (0)