jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.8KB

  1. /*
  2. Copyright (C) 2004-2008 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include "JackMMCSS.h"
  16. #include "JackError.h"
  17. #include <assert.h>
  18. #include <stdio.h>
  19. namespace Jack
  20. {
  21. avSetMmThreadCharacteristics JackMMCSS::ffMMCSSFun1 = NULL;
  22. avSetMmThreadPriority JackMMCSS::ffMMCSSFun2 = NULL;
  23. avRevertMmThreadCharacteristics JackMMCSS::ffMMCSSFun3 = NULL;
  24. JACK_HANDLE JackMMCSS::fAvrtDll;
  25. std::map<jack_native_thread_t, HANDLE> JackMMCSS::fHandleTable;
  26. JackMMCSS::JackMMCSS()
  27. {
  28. fAvrtDll = LoadJackModule("avrt.dll");
  29. if (fAvrtDll != NULL) {
  30. ffMMCSSFun1 = (avSetMmThreadCharacteristics)GetJackProc(fAvrtDll, "AvSetMmThreadCharacteristicsA");
  31. ffMMCSSFun2 = (avSetMmThreadPriority)GetJackProc(fAvrtDll, "AvSetMmThreadPriority");
  32. ffMMCSSFun3 = (avRevertMmThreadCharacteristics)GetJackProc(fAvrtDll, "AvRevertMmThreadCharacteristics");
  33. }
  34. }
  35. JackMMCSS::~JackMMCSS()
  36. {}
  37. int JackMMCSS::MMCSSAcquireRealTime(jack_native_thread_t thread, int priority)
  38. {
  39. if (fHandleTable.find(thread) != fHandleTable.end()) {
  40. return 0;
  41. }
  42. if (ffMMCSSFun1) {
  43. DWORD dummy = 0;
  44. HANDLE task = ffMMCSSFun1("Pro Audio", &dummy);
  45. if (task == NULL) {
  46. jack_error("AvSetMmThreadCharacteristics error : %d", GetLastError());
  47. } else if (ffMMCSSFun2(task, Jack::AVRT_PRIORITY(priority - BASE_REALTIME_PRIORITY))) {
  48. fHandleTable[thread] = task;
  49. jack_log("AvSetMmThreadPriority success");
  50. return 0;
  51. } else {
  52. jack_error("AvSetMmThreadPriority error : %d", GetLastError());
  53. }
  54. }
  55. return -1;
  56. }
  57. int JackMMCSS::MMCSSDropRealTime(jack_native_thread_t thread)
  58. {
  59. if (fHandleTable.find(thread) != fHandleTable.end()) {
  60. HANDLE task = fHandleTable[thread];
  61. if (ffMMCSSFun3(task) == 0) {
  62. jack_error("AvRevertMmThreadCharacteristics error : %d", GetLastError());
  63. } else {
  64. jack_log("AvRevertMmThreadCharacteristics success");
  65. }
  66. return 0;
  67. } else {
  68. return -1;
  69. }
  70. }
  71. }