Skip to content

Commit

Permalink
wasapi: Call avrt functions directly since we only support Vista+.
Browse files Browse the repository at this point in the history
  • Loading branch information
kinetiknz authored and padenot committed Nov 15, 2016
1 parent 1c29faf commit c0ec441
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 46 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -95,6 +95,7 @@ if(USE_WASAPI)
target_sources(cubeb PRIVATE
src/cubeb_wasapi.cpp)
target_compile_definitions(cubeb PRIVATE USE_WASAPI)
target_link_libraries(cubeb PRIVATE avrt)
endif()

check_include_files("windows.h;mmsystem.h" USE_WINMM)
Expand Down
48 changes: 2 additions & 46 deletions src/cubeb_wasapi.cpp
Expand Up @@ -168,10 +168,6 @@ struct auto_com {
HRESULT result;
};

typedef HANDLE (WINAPI *set_mm_thread_characteristics_function)(
const char * TaskName, LPDWORD TaskIndex);
typedef BOOL (WINAPI *revert_mm_thread_characteristics_function)(HANDLE handle);

extern cubeb_ops const wasapi_ops;

int wasapi_stream_stop(cubeb_stream * stm);
Expand All @@ -185,11 +181,6 @@ static std::unique_ptr<wchar_t const []> utf8_to_wstr(char const * str);

struct cubeb {
cubeb_ops const * ops = &wasapi_ops;
/* Library dynamically opened to increase the render thread priority, and
the two function pointers we need. */
HMODULE mmcss_module = nullptr;
set_mm_thread_characteristics_function set_mm_thread_characteristics = nullptr;
revert_mm_thread_characteristics_function revert_mm_thread_characteristics = nullptr;
};

class wasapi_endpoint_notification_client;
Expand Down Expand Up @@ -875,8 +866,7 @@ wasapi_stream_render_loop(LPVOID stream)

/* We could consider using "Pro Audio" here for WebAudio and
maybe WebRTC. */
mmcss_handle =
stm->context->set_mm_thread_characteristics("Audio", &mmcss_task_index);
mmcss_handle = AvSetMmThreadCharacteristicsA("Audio", &mmcss_task_index);
if (!mmcss_handle) {
/* This is not fatal, but we might glitch under heavy load. */
LOG("Unable to use mmcss to bump the render thread priority: %x", GetLastError());
Expand Down Expand Up @@ -978,23 +968,13 @@ wasapi_stream_render_loop(LPVOID stream)
stm->state_callback(stm, stm->user_ptr, CUBEB_STATE_ERROR);
}

stm->context->revert_mm_thread_characteristics(mmcss_handle);
AvRevertMmThreadCharacteristics(mmcss_handle);

return 0;
}

void wasapi_destroy(cubeb * context);

HANDLE WINAPI set_mm_thread_characteristics_noop(const char *, LPDWORD mmcss_task_index)
{
return (HANDLE)1;
}

BOOL WINAPI revert_mm_thread_characteristics_noop(HANDLE mmcss_handle)
{
return true;
}

HRESULT register_notification_client(cubeb_stream * stm)
{
HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),
Expand Down Expand Up @@ -1172,27 +1152,6 @@ int wasapi_init(cubeb ** context, char const * context_name)

ctx->ops = &wasapi_ops;

ctx->mmcss_module = LoadLibraryA("Avrt.dll");

if (ctx->mmcss_module) {
ctx->set_mm_thread_characteristics =
(set_mm_thread_characteristics_function) GetProcAddress(
ctx->mmcss_module, "AvSetMmThreadCharacteristicsA");
ctx->revert_mm_thread_characteristics =
(revert_mm_thread_characteristics_function) GetProcAddress(
ctx->mmcss_module, "AvRevertMmThreadCharacteristics");
if (!(ctx->set_mm_thread_characteristics && ctx->revert_mm_thread_characteristics)) {
LOG("Could not load AvSetMmThreadCharacteristics or AvRevertMmThreadCharacteristics: %x", GetLastError());
FreeLibrary(ctx->mmcss_module);
}
} else {
// This is not a fatal error, but we might end up glitching when
// the system is under high load.
LOG("Could not load Avrt.dll");
ctx->set_mm_thread_characteristics = &set_mm_thread_characteristics_noop;
ctx->revert_mm_thread_characteristics = &revert_mm_thread_characteristics_noop;
}

*context = ctx;

return CUBEB_OK;
Expand Down Expand Up @@ -1244,9 +1203,6 @@ bool stop_and_join_render_thread(cubeb_stream * stm)

void wasapi_destroy(cubeb * context)
{
if (context->mmcss_module) {
FreeLibrary(context->mmcss_module);
}
delete context;
}

Expand Down

0 comments on commit c0ec441

Please sign in to comment.