audio_reserve.c 3.29 KB
Newer Older
sletz's avatar
sletz committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
    Copyright (C) 2009 Grame
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
sletz's avatar
sletz committed
25
#include <stdint.h>
sletz's avatar
sletz committed
26
27
28
29
30

#include "reserve.h"
#include "audio_reserve.h"
#include "JackError.h"

sletz's avatar
sletz committed
31
32
33
34
35
36
37
38
39
40
41
42
#define DEVICE_MAX 2

typedef struct reserved_audio_device {

     char device_name[64];
     rd_device * reserved_device;

} reserved_audio_device;

static DBusConnection* gConnection = NULL;
static reserved_audio_device gReservedDevice[DEVICE_MAX];
static int gReserveCount = 0;
sletz's avatar
sletz committed
43
44
45
46
47
48

SERVER_EXPORT int audio_reservation_init()
{
    DBusError error;
    dbus_error_init(&error);

sletz's avatar
sletz committed
49
    if (!(gConnection = dbus_bus_get(DBUS_BUS_SESSION, &error))) {
sletz's avatar
sletz committed
50
51
52
53
        jack_error("Failed to connect to session bus for device reservation %s\n", error.message);
        return -1;
    }

sletz's avatar
sletz committed
54
    jack_info("audio_reservation_init");
sletz's avatar
sletz committed
55
56
57
58
59
    return 0;
}

SERVER_EXPORT int audio_reservation_finish()
{
sletz's avatar
sletz committed
60
61
62
    if (gConnection) {
        dbus_connection_unref(gConnection);
        gConnection = NULL;
sletz's avatar
sletz committed
63
        jack_info("audio_reservation_finish");
sletz's avatar
sletz committed
64
    }
sletz's avatar
sletz committed
65
66
67
    return 0;
}

sletz's avatar
sletz committed
68
SERVER_EXPORT bool audio_acquire(const char * device_name)
sletz's avatar
sletz committed
69
{
sletz's avatar
sletz committed
70
71
72
73
74
75
76
77
78
79
80
81
82
    DBusError error;   
    int ret;

    // Open DBus connection first time
    if (gReserveCount == 0)
       audio_reservation_init();

    assert(gReserveCount < DEVICE_MAX);

    if ((ret= rd_acquire(
                 &gReservedDevice[gReserveCount].reserved_device,
                 gConnection,
                 device_name,
sletz's avatar
sletz committed
83
84
85
86
87
                 "Jack audio server",
                 INT32_MAX,
                 NULL,
                 &error)) < 0) {

sletz's avatar
sletz committed
88
89
        jack_error("Failed to acquire device name : %s error : %s", device_name, (error.message ? error.message : strerror(-ret)));
        return false;
sletz's avatar
sletz committed
90
91
    }

sletz's avatar
sletz committed
92
93
94
95
    strcpy(gReservedDevice[gReserveCount].device_name, device_name);
    gReserveCount++;
    jack_info("Acquire audio card %s", device_name);
    return true;
sletz's avatar
sletz committed
96
97
}

sletz's avatar
sletz committed
98
SERVER_EXPORT void audio_release(const char * device_name)
sletz's avatar
sletz committed
99
{
sletz's avatar
sletz committed
100
101
102
103
104
105
106
107
108
    int i;

    // Look for corresponding reserved device
    for (i = 0; i < DEVICE_MAX; i++) {
 	if (strcmp(gReservedDevice[i].device_name, device_name) == 0)  
	    break;
    }
   
    if (i < DEVICE_MAX) {
sletz's avatar
sletz committed
109
        jack_info("Released audio card %s", device_name);
sletz's avatar
sletz committed
110
111
        rd_release(gReservedDevice[i].reserved_device);
    } else {
sletz's avatar
sletz committed
112
        jack_error("Audio card %s not found!!", device_name);
sletz's avatar
sletz committed
113
    }
sletz's avatar
sletz committed
114
115
116
117
118

    // Close DBus connection last time
    gReserveCount--;
    if (gReserveCount == 0)
        audio_reservation_finish();
sletz's avatar
sletz committed
119
120
}

sletz's avatar
sletz committed
121
SERVER_EXPORT void audio_reserve_loop()
sletz's avatar
sletz committed
122
{
sletz's avatar
sletz committed
123
124
125
126
    if (gConnection != NULL) {
       while (dbus_connection_read_write_dispatch (gConnection, -1))
         ; // empty loop body
    }
sletz's avatar
sletz committed
127
}
sletz's avatar
sletz committed
128