- add sources.
[platform/framework/web/crosswalk.git] / src / media / audio / win / avrt_wrapper_win.cc
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.
4
5 #include "media/audio/win/avrt_wrapper_win.h"
6
7 #include "base/logging.h"
8
9 namespace avrt {
10
11 // Function pointers
12 typedef BOOL (WINAPI *AvRevertMmThreadCharacteristicsFn)(HANDLE);
13 typedef HANDLE (WINAPI *AvSetMmThreadCharacteristicsFn)(LPCWSTR, LPDWORD);
14 typedef BOOL (WINAPI *AvSetMmThreadPriorityFn)(HANDLE, AVRT_PRIORITY);
15
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;
20
21 bool Initialize() {
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,
26         arraysize(path));
27     g_avrt = LoadLibraryExW(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
28     if (!g_avrt)
29       return false;
30
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"));
39   }
40
41   return (g_avrt && g_revert_mm_thread_characteristics &&
42           g_set_mm_thread_characteristics && g_set_mm_thread_priority);
43 }
44
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));
49 }
50
51 HANDLE AvSetMmThreadCharacteristics(const wchar_t* task_name,
52                                     DWORD* task_index) {
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);
56 }
57
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));
62 }
63
64 }  // namespace avrt