summaryrefslogtreecommitdiffstats
path: root/media/audio/win/avrt_wrapper_win.cc
blob: c9f15991743993089c13656156cb7520431d1e14 (plain)
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
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/audio/win/avrt_wrapper_win.h"

#include "base/logging.h"

namespace avrt {

// Function pointers
typedef BOOL (WINAPI *AvRevertMmThreadCharacteristicsFn)(HANDLE);
typedef HANDLE (WINAPI *AvSetMmThreadCharacteristicsFn)(LPCWSTR, LPDWORD);
typedef BOOL (WINAPI *AvSetMmThreadPriorityFn)(HANDLE, AVRT_PRIORITY);

HMODULE g_avrt = NULL;
AvRevertMmThreadCharacteristicsFn g_revert_mm_thread_characteristics = NULL;
AvSetMmThreadCharacteristicsFn g_set_mm_thread_characteristics = NULL;
AvSetMmThreadPriorityFn g_set_mm_thread_priority = NULL;

bool Initialize() {
  if (!g_set_mm_thread_priority) {
    // The avrt.dll is available on Windows Vista and later.
    wchar_t path[MAX_PATH] = {0};
    ExpandEnvironmentStrings(L"%WINDIR%\\system32\\avrt.dll", path,
        arraysize(path));
    g_avrt = LoadLibraryExW(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
    if (!g_avrt)
      return false;

    g_revert_mm_thread_characteristics =
        reinterpret_cast<AvRevertMmThreadCharacteristicsFn>(
            GetProcAddress(g_avrt, "AvRevertMmThreadCharacteristics"));
    g_set_mm_thread_characteristics =
        reinterpret_cast<AvSetMmThreadCharacteristicsFn>(
            GetProcAddress(g_avrt, "AvSetMmThreadCharacteristicsW"));
    g_set_mm_thread_priority = reinterpret_cast<AvSetMmThreadPriorityFn>(
        GetProcAddress(g_avrt, "AvSetMmThreadPriority"));
  }

  return (g_avrt && g_revert_mm_thread_characteristics &&
          g_set_mm_thread_characteristics && g_set_mm_thread_priority);
}

bool AvRevertMmThreadCharacteristics(HANDLE avrt_handle) {
  DCHECK(g_revert_mm_thread_characteristics);
  return (g_revert_mm_thread_characteristics &&
          g_revert_mm_thread_characteristics(avrt_handle));
}

HANDLE AvSetMmThreadCharacteristics(const wchar_t* task_name,
                                    DWORD* task_index) {
  DCHECK(g_set_mm_thread_characteristics);
  return (g_set_mm_thread_characteristics ?
          g_set_mm_thread_characteristics(task_name, task_index) : NULL);
}

bool AvSetMmThreadPriority(HANDLE avrt_handle, AVRT_PRIORITY priority) {
  DCHECK(g_set_mm_thread_priority);
  return (g_set_mm_thread_priority &&
          g_set_mm_thread_priority(avrt_handle, priority));
}

}  // namespace avrt