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.
28 #include "audio_reserve.h"
29 #include "jack/control.h"
33 typedef struct reserved_audio_device
{
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()
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 jack_info("To bypass device reservation via session bus, set JACK_NO_AUDIO_RESERVATION=1 prior to starting jackd.\n");
55 jack_info("audio_reservation_init");
59 SERVER_EXPORT
int audio_reservation_finish()
62 dbus_connection_unref(gConnection
);
64 jack_info("audio_reservation_finish");
69 SERVER_EXPORT
bool audio_acquire(const char * device_name
)
74 // Open DBus connection first time
75 if (gReserveCount
== 0) {
76 if (audio_reservation_init() != 0) {
81 assert(gReserveCount
< DEVICE_MAX
);
83 dbus_error_init(&error
);
86 &gReservedDevice
[gReserveCount
].reserved_device
,
94 jack_error("Failed to acquire device name : %s error : %s", device_name
, (error
.message
? error
.message
: strerror(-ret
)));
95 dbus_error_free(&error
);
99 strcpy(gReservedDevice
[gReserveCount
].device_name
, device_name
);
101 jack_info("Acquire audio card %s", device_name
);
105 SERVER_EXPORT
void audio_release(const char * device_name
)
109 // Look for corresponding reserved device
110 for (i
= 0; i
< DEVICE_MAX
; i
++) {
111 if (strcmp(gReservedDevice
[i
].device_name
, device_name
) == 0)
115 if (i
< DEVICE_MAX
) {
116 jack_info("Released audio card %s", device_name
);
117 rd_release(gReservedDevice
[i
].reserved_device
);
119 jack_error("Audio card %s not found!!", device_name
);
122 // Close DBus connection last time
124 if (gReserveCount
== 0)
125 audio_reservation_finish();
128 SERVER_EXPORT
void audio_reserve_loop()
130 if (gConnection
!= NULL
) {
131 dbus_connection_read_write_dispatch (gConnection
, 200);