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.

136 lines
3.5KB

  1. /*
  2. Copyright (C) 2009 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  13. */
  14. #include <string.h>
  15. #include <unistd.h>
  16. #include <errno.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <assert.h>
  20. #include <stdint.h>
  21. #include "reserve.h"
  22. #include "audio_reserve.h"
  23. #include "jack/control.h"
  24. #define DEVICE_MAX 2
  25. typedef struct reserved_audio_device {
  26. char device_name[64];
  27. rd_device * reserved_device;
  28. } reserved_audio_device;
  29. static DBusConnection* gConnection = NULL;
  30. static reserved_audio_device gReservedDevice[DEVICE_MAX];
  31. static int gReserveCount = 0;
  32. SERVER_EXPORT int audio_reservation_init()
  33. {
  34. DBusError error;
  35. dbus_error_init(&error);
  36. if (!(gConnection = dbus_bus_get(DBUS_BUS_SESSION, &error))) {
  37. jack_error("Failed to connect to session bus for device reservation: %s\n", error.message);
  38. jack_info("To bypass device reservation via session bus, set JACK_NO_AUDIO_RESERVATION=1 prior to starting jackd.\n");
  39. return -1;
  40. }
  41. jack_info("audio_reservation_init");
  42. return 0;
  43. }
  44. SERVER_EXPORT int audio_reservation_finish()
  45. {
  46. if (gConnection) {
  47. dbus_connection_unref(gConnection);
  48. gConnection = NULL;
  49. jack_info("audio_reservation_finish");
  50. }
  51. return 0;
  52. }
  53. SERVER_EXPORT bool audio_acquire(const char * device_name)
  54. {
  55. DBusError error;
  56. int ret;
  57. // Open DBus connection first time
  58. if (gReserveCount == 0) {
  59. if (audio_reservation_init() != 0) {
  60. return false;
  61. }
  62. }
  63. assert(gReserveCount < DEVICE_MAX);
  64. dbus_error_init(&error);
  65. if ((ret= rd_acquire(
  66. &gReservedDevice[gReserveCount].reserved_device,
  67. gConnection,
  68. device_name,
  69. "Jack audio server",
  70. INT32_MAX,
  71. NULL,
  72. &error)) < 0) {
  73. jack_error("Failed to acquire device name : %s error : %s", device_name, (error.message ? error.message : strerror(-ret)));
  74. dbus_error_free(&error);
  75. return false;
  76. }
  77. strcpy(gReservedDevice[gReserveCount].device_name, device_name);
  78. gReserveCount++;
  79. jack_info("Acquire audio card %s", device_name);
  80. return true;
  81. }
  82. SERVER_EXPORT void audio_release(const char * device_name)
  83. {
  84. int i;
  85. // Look for corresponding reserved device
  86. for (i = 0; i < DEVICE_MAX; i++) {
  87. if (strcmp(gReservedDevice[i].device_name, device_name) == 0)
  88. break;
  89. }
  90. if (i < DEVICE_MAX) {
  91. jack_info("Released audio card %s", device_name);
  92. rd_release(gReservedDevice[i].reserved_device);
  93. } else {
  94. jack_error("Audio card %s not found!!", device_name);
  95. }
  96. // Close DBus connection last time
  97. gReserveCount--;
  98. if (gReserveCount == 0)
  99. audio_reservation_finish();
  100. }
  101. SERVER_EXPORT void audio_reserve_loop()
  102. {
  103. if (gConnection != NULL) {
  104. while (dbus_connection_read_write_dispatch (gConnection, -1))
  105. ; // empty loop body
  106. }
  107. }