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 "JackError.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
);
54 jack_info("audio_reservation_init");
58 SERVER_EXPORT
int audio_reservation_finish()
61 dbus_connection_unref(gConnection
);
63 jack_info("audio_reservation_finish");
68 SERVER_EXPORT
bool audio_acquire(const char * device_name
)
73 // Open DBus connection first time
74 if (gReserveCount
== 0)
75 audio_reservation_init();
77 assert(gReserveCount
< DEVICE_MAX
);
80 &gReservedDevice
[gReserveCount
].reserved_device
,
88 jack_error("Failed to acquire device name : %s error : %s", device_name
, (error
.message
? error
.message
: strerror(-ret
)));
92 strcpy(gReservedDevice
[gReserveCount
].device_name
, device_name
);
94 jack_info("Acquire audio card %s", device_name
);
98 SERVER_EXPORT
void audio_release(const char * device_name
)
102 // Look for corresponding reserved device
103 for (i
= 0; i
< DEVICE_MAX
; i
++) {
104 if (strcmp(gReservedDevice
[i
].device_name
, device_name
) == 0)
108 if (i
< DEVICE_MAX
) {
109 jack_info("Released audio card %s", device_name
);
110 rd_release(gReservedDevice
[i
].reserved_device
);
112 jack_error("Audio card %s not found!!", device_name
);
115 // Close DBus connection last time
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))