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.

129 lines
3.3KB

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