In combined --dbus and --classic compilation ode, use PulseAudio acquire/release...
[jack2.git] / dbus / audio_reserve.c
blob120db64fc358c27da9a56e87127fcc886173ba03
1 /*
2 Copyright (C) 2009 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <string.h>
20 #include <unistd.h>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <assert.h>
25 #include <stdint.h>
27 #include "reserve.h"
28 #include "audio_reserve.h"
29 #include "JackError.h"
31 #define DEVICE_MAX 2
33 typedef struct reserved_audio_device {
35 char device_name[64];
36 rd_device * reserved_device;
38 } reserved_audio_device;
40 static DBusConnection* gConnection = NULL;
41 static reserved_audio_device gReservedDevice[DEVICE_MAX];
42 static int gReserveCount = 0;
44 SERVER_EXPORT int audio_reservation_init()
46 DBusError error;
47 dbus_error_init(&error);
49 if (!(gConnection = dbus_bus_get(DBUS_BUS_SESSION, &error))) {
50 jack_error("Failed to connect to session bus for device reservation %s\n", error.message);
51 return -1;
54 jack_info("audio_reservation_init");
55 return 0;
58 SERVER_EXPORT int audio_reservation_finish()
60 if (gConnection) {
61 dbus_connection_unref(gConnection);
62 gConnection = NULL;
63 jack_info("audio_reservation_finish");
65 return 0;
68 SERVER_EXPORT bool audio_acquire(const char * device_name)
70 DBusError error;
71 int ret;
73 // Open DBus connection first time
74 if (gReserveCount == 0)
75 audio_reservation_init();
77 assert(gReserveCount < DEVICE_MAX);
79 if ((ret= rd_acquire(
80 &gReservedDevice[gReserveCount].reserved_device,
81 gConnection,
82 device_name,
83 "Jack audio server",
84 INT32_MAX,
85 NULL,
86 &error)) < 0) {
88 jack_error("Failed to acquire device name : %s error : %s", device_name, (error.message ? error.message : strerror(-ret)));
89 return false;
92 strcpy(gReservedDevice[gReserveCount].device_name, device_name);
93 gReserveCount++;
94 jack_info("Acquire audio card %s", device_name);
95 return true;
98 SERVER_EXPORT void audio_release(const char * device_name)
100 int i;
102 // Look for corresponding reserved device
103 for (i = 0; i < DEVICE_MAX; i++) {
104 if (strcmp(gReservedDevice[i].device_name, device_name) == 0)
105 break;
108 if (i < DEVICE_MAX) {
109 jack_info("Released audio card %s", device_name);
110 rd_release(gReservedDevice[i].reserved_device);
111 } else {
112 jack_error("Audio card %s not found!!", device_name);
115 // Close DBus connection last time
116 gReserveCount--;
117 if (gReserveCount == 0)
118 audio_reservation_finish();
121 SERVER_EXPORT void audio_reserve_loop()
123 if (gConnection != NULL) {
124 while (dbus_connection_read_write_dispatch (gConnection, -1))
125 ; // empty loop body