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
   65
   66
   67
   68
   69
   70
   71

media / audio / win / avrt_wrapper_win.cc [blame]

// Copyright 2011 The Chromium Authors
// 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 <iterator>

#include "base/check.h"
#include "base/win/win_util.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.
    auto path = base::win::ExpandEnvironmentVariables(
        L"%SystemRoot%\\system32\\avrt.dll");
    if (!path) {
      return false;
    }

    g_avrt = LoadLibraryExW(path->c_str(), 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