File: audio_reserve.c

package info (click to toggle)
jackd2 1.9.8~dfsg.4%2B20120529git007cdc37-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,212 kB
  • sloc: cpp: 37,659; ansic: 26,444; python: 12,592; sh: 140; makefile: 69
file content (128 lines) | stat: -rw-r--r-- 3,373 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
    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>
#include <stdint.h>

#include "reserve.h"
#include "audio_reserve.h"
#include "jack/control.h"

#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;

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

    if (!(gConnection = dbus_bus_get(DBUS_BUS_SESSION, &error))) {
        jack_error("Failed to connect to session bus for device reservation %s\n", error.message);
        return -1;
    }

    jack_info("audio_reservation_init");
    return 0;
}

SERVER_EXPORT int audio_reservation_finish()
{
    if (gConnection) {
        dbus_connection_unref(gConnection);
        gConnection = NULL;
        jack_info("audio_reservation_finish");
    }
    return 0;
}

SERVER_EXPORT bool audio_acquire(const char * device_name)
{
    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,
                 "Jack audio server",
                 INT32_MAX,
                 NULL,
                 &error)) < 0) {

        jack_error("Failed to acquire device name : %s error : %s", device_name, (error.message ? error.message : strerror(-ret)));
        return false;
    }

    strcpy(gReservedDevice[gReserveCount].device_name, device_name);
    gReserveCount++;
    jack_info("Acquire audio card %s", device_name);
    return true;
}

SERVER_EXPORT void audio_release(const char * device_name)
{
    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) {
        jack_info("Released audio card %s", device_name);
        rd_release(gReservedDevice[i].reserved_device);
    } else {
        jack_error("Audio card %s not found!!", device_name);
    }

    // Close DBus connection last time
    gReserveCount--;
    if (gReserveCount == 0)
        audio_reservation_finish();
}

SERVER_EXPORT void audio_reserve_loop()
{
    if (gConnection != NULL) {
       while (dbus_connection_read_write_dispatch (gConnection, -1))
         ; // empty loop body
    }
}