1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/audio/win/avrt_wrapper_win.h"
7 #include "base/logging.h"
12 typedef BOOL (WINAPI
*AvRevertMmThreadCharacteristicsFn
)(HANDLE
);
13 typedef HANDLE (WINAPI
*AvSetMmThreadCharacteristicsFn
)(LPCWSTR
, LPDWORD
);
14 typedef BOOL (WINAPI
*AvSetMmThreadPriorityFn
)(HANDLE
, AVRT_PRIORITY
);
16 HMODULE g_avrt
= NULL
;
17 AvRevertMmThreadCharacteristicsFn g_revert_mm_thread_characteristics
= NULL
;
18 AvSetMmThreadCharacteristicsFn g_set_mm_thread_characteristics
= NULL
;
19 AvSetMmThreadPriorityFn g_set_mm_thread_priority
= NULL
;
22 if (!g_set_mm_thread_priority
) {
23 // The avrt.dll is available on Windows Vista and later.
24 wchar_t path
[MAX_PATH
] = {0};
25 ExpandEnvironmentStrings(L
"%WINDIR%\\system32\\avrt.dll", path
,
27 g_avrt
= LoadLibraryExW(path
, NULL
, LOAD_WITH_ALTERED_SEARCH_PATH
);
31 g_revert_mm_thread_characteristics
=
32 reinterpret_cast<AvRevertMmThreadCharacteristicsFn
>(
33 GetProcAddress(g_avrt
, "AvRevertMmThreadCharacteristics"));
34 g_set_mm_thread_characteristics
=
35 reinterpret_cast<AvSetMmThreadCharacteristicsFn
>(
36 GetProcAddress(g_avrt
, "AvSetMmThreadCharacteristicsW"));
37 g_set_mm_thread_priority
= reinterpret_cast<AvSetMmThreadPriorityFn
>(
38 GetProcAddress(g_avrt
, "AvSetMmThreadPriority"));
41 return (g_avrt
&& g_revert_mm_thread_characteristics
&&
42 g_set_mm_thread_characteristics
&& g_set_mm_thread_priority
);
45 bool AvRevertMmThreadCharacteristics(HANDLE avrt_handle
) {
46 DCHECK(g_revert_mm_thread_characteristics
);
47 return (g_revert_mm_thread_characteristics
&&
48 g_revert_mm_thread_characteristics(avrt_handle
));
51 HANDLE
AvSetMmThreadCharacteristics(const wchar_t* task_name
,
53 DCHECK(g_set_mm_thread_characteristics
);
54 return (g_set_mm_thread_characteristics
?
55 g_set_mm_thread_characteristics(task_name
, task_index
) : NULL
);
58 bool AvSetMmThreadPriority(HANDLE avrt_handle
, AVRT_PRIORITY priority
) {
59 DCHECK(g_set_mm_thread_priority
);
60 return (g_set_mm_thread_priority
&&
61 g_set_mm_thread_priority(avrt_handle
, priority
));