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.

135 lines
3.4KB

  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. return -1;
  39. }
  40. jack_info("audio_reservation_init");
  41. return 0;
  42. }
  43. SERVER_EXPORT int audio_reservation_finish()
  44. {
  45. if (gConnection) {
  46. dbus_connection_unref(gConnection);
  47. gConnection = NULL;
  48. jack_info("audio_reservation_finish");
  49. }
  50. return 0;
  51. }
  52. SERVER_EXPORT bool audio_acquire(const char * device_name)
  53. {
  54. DBusError error;
  55. int ret;
  56. // Open DBus connection first time
  57. if (gReserveCount == 0) {
  58. if (audio_reservation_init() != 0) {
  59. return false;
  60. }
  61. }
  62. assert(gReserveCount < DEVICE_MAX);
  63. dbus_error_init(&error);
  64. if ((ret= rd_acquire(
  65. &gReservedDevice[gReserveCount].reserved_device,
  66. gConnection,
  67. device_name,
  68. "Jack audio server",
  69. INT32_MAX,
  70. NULL,
  71. &error)) < 0) {
  72. jack_error("Failed to acquire device name : %s error : %s", device_name, (error.message ? error.message : strerror(-ret)));
  73. dbus_error_free(&error);
  74. return false;
  75. }
  76. strcpy(gReservedDevice[gReserveCount].device_name, device_name);
  77. gReserveCount++;
  78. jack_info("Acquire audio card %s", device_name);
  79. return true;
  80. }
  81. SERVER_EXPORT void audio_release(const char * device_name)
  82. {
  83. int i;
  84. // Look for corresponding reserved device
  85. for (i = 0; i < DEVICE_MAX; i++) {
  86. if (strcmp(gReservedDevice[i].device_name, device_name) == 0)
  87. break;
  88. }
  89. if (i < DEVICE_MAX) {
  90. jack_info("Released audio card %s", device_name);
  91. rd_release(gReservedDevice[i].reserved_device);
  92. } else {
  93. jack_error("Audio card %s not found!!", device_name);
  94. }
  95. // Close DBus connection last time
  96. gReserveCount--;
  97. if (gReserveCount == 0)
  98. audio_reservation_finish();
  99. }
  100. SERVER_EXPORT void audio_reserve_loop()
  101. {
  102. if (gConnection != NULL) {
  103. while (dbus_connection_read_write_dispatch (gConnection, -1))
  104. ; // empty loop body
  105. }
  106. }