@@ -29,11 +29,43 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29
29
#include < iostream>
30
30
#include < assert.h>
31
31
32
+ #ifdef __linux__
33
+ #include " JackServerGlobals.h"
34
+ #endif
35
+
32
36
using namespace std ;
33
37
34
38
namespace Jack
35
39
{
36
40
41
+ #ifdef __linux__
42
+ static volatile bool device_reservation_loop_running = false ;
43
+
44
+ static void * on_device_reservation_loop (void *)
45
+ {
46
+ while (device_reservation_loop_running && JackServerGlobals::on_device_reservation_loop != NULL ) {
47
+ JackServerGlobals::on_device_reservation_loop ();
48
+ usleep (50 *1000 );
49
+ }
50
+
51
+ return NULL ;
52
+ }
53
+
54
+ static bool name_to_num (const char * paDeviceName, char * entry)
55
+ {
56
+ if (const char * sep = strstr (paDeviceName, " (hw:" ))
57
+ {
58
+ sep += 5 ;
59
+ while (*sep != ' \0 ' && *sep != ' ,' && *sep != ' )' )
60
+ *entry++ = *sep++;
61
+ *entry = ' \0 ' ;
62
+ return true ;
63
+ }
64
+
65
+ return false ;
66
+ }
67
+ #endif
68
+
37
69
int JackPortAudioDriver::Render (const void * inputBuffer, void * outputBuffer,
38
70
unsigned long framesPerBuffer,
39
71
const PaStreamCallbackTimeInfo* timeInfo,
@@ -231,6 +263,27 @@ int JackPortAudioDriver::Open(jack_nframes_t buffer_size,
231
263
fCaptureChannels = inchannels;
232
264
fPlaybackChannels = outchannels;
233
265
266
+ #ifdef __linux__
267
+ if (JackServerGlobals::on_device_acquire != NULL ) {
268
+ char audio_name[32 ];
269
+ snprintf (audio_name, sizeof (audio_name), " Audio" );
270
+
271
+ if (name_to_num (capture_driver_name, audio_name+5 )) {
272
+ if (!JackServerGlobals::on_device_acquire (audio_name)) {
273
+ jack_error (" Audio device %s cannot be acquired..." , capture_driver_name);
274
+ return -1 ;
275
+ }
276
+ }
277
+
278
+ if (strcmp (capture_driver_name, playback_driver_name) && name_to_num (playback_driver_name, audio_name+5 )) {
279
+ if (!JackServerGlobals::on_device_acquire (audio_name)) {
280
+ jack_error (" Audio device %s cannot be acquired..." , playback_driver_name);
281
+ return -1 ;
282
+ }
283
+ }
284
+ }
285
+ #endif
286
+
234
287
err = OpenStream (buffer_size);
235
288
if (err != paNoError) {
236
289
jack_error (" Pa_OpenStream error = %s" , Pa_GetErrorText (err));
@@ -243,6 +296,15 @@ int JackPortAudioDriver::Open(jack_nframes_t buffer_size,
243
296
fEngineControl ->fConstraint = fEngineControl ->fPeriodUsecs * 1000 ;
244
297
#endif
245
298
299
+ #ifdef __linux__
300
+ if (JackServerGlobals::on_device_reservation_loop != NULL ) {
301
+ device_reservation_loop_running = true ;
302
+ if (JackPosixThread::StartImp (&fReservationLoopThread , 0 , 0 , on_device_reservation_loop, NULL ) != 0 ) {
303
+ device_reservation_loop_running = false ;
304
+ }
305
+ }
306
+ #endif
307
+
246
308
return 0 ;
247
309
248
310
error:
@@ -260,6 +322,28 @@ int JackPortAudioDriver::Close()
260
322
if (err != paNoError) {
261
323
jack_error (" Pa_CloseStream error = %s" , Pa_GetErrorText (err));
262
324
}
325
+
326
+ #ifdef __linux__
327
+ if (device_reservation_loop_running) {
328
+ device_reservation_loop_running = false ;
329
+ JackPosixThread::StopImp (fReservationLoopThread );
330
+ }
331
+
332
+ if (JackServerGlobals::on_device_release != NULL )
333
+ {
334
+ char audio_name[32 ];
335
+ snprintf (audio_name, sizeof (audio_name), " Audio" );
336
+
337
+ if (name_to_num (fCaptureDriverName , audio_name+5 )) {
338
+ JackServerGlobals::on_device_release (audio_name);
339
+ }
340
+
341
+ if (strcmp (fCaptureDriverName , fPlaybackDriverName ) && name_to_num (fPlaybackDriverName , audio_name+5 )) {
342
+ JackServerGlobals::on_device_release (audio_name);
343
+ }
344
+ }
345
+ #endif
346
+
263
347
delete fPaDevices ;
264
348
fPaDevices = NULL ;
265
349
return (err != paNoError) ? -1 : 0 ;
@@ -269,9 +353,9 @@ int JackPortAudioDriver::Attach()
269
353
{
270
354
if (JackAudioDriver::Attach () == 0 ) {
271
355
356
+ #if defined(HAVE_ASIO)
272
357
const char * alias;
273
358
274
- #if defined(HAVE_ASIO)
275
359
if (fInputDevice != paNoDevice && fPaDevices ->GetHostFromDevice (fInputDevice ) == " ASIO" ) {
276
360
for (int i = 0 ; i < fCaptureChannels ; i++) {
277
361
if (PaAsio_GetInputChannelName (fInputDevice , i, &alias) == paNoError) {
0 commit comments