[4490] | 1 | //driver_jack.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[4708] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2011 |
---|
[4825] | 5 | * Copyright (C) Nedko Arnaudov <nedko@arnaudov.name> - 2010 |
---|
[4490] | 6 | * |
---|
| 7 | * This file is part of roard a part of RoarAudio, |
---|
| 8 | * a cross-platform sound system for both, home and professional use. |
---|
| 9 | * See README for details. |
---|
| 10 | * |
---|
| 11 | * This file is free software; you can redistribute it and/or modify |
---|
| 12 | * it under the terms of the GNU General Public License version 3 |
---|
| 13 | * as published by the Free Software Foundation. |
---|
| 14 | * |
---|
| 15 | * RoarAudio is distributed in the hope that it will be useful, |
---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 18 | * GNU General Public License for more details. |
---|
| 19 | * |
---|
| 20 | * You should have received a copy of the GNU General Public License |
---|
| 21 | * along with this software; see the file COPYING. If not, write to |
---|
| 22 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 23 | * Boston, MA 02110-1301, USA. |
---|
| 24 | * |
---|
| 25 | */ |
---|
| 26 | |
---|
| 27 | #include "roard.h" |
---|
| 28 | |
---|
[4491] | 29 | #ifdef ROAR_HAVE_LIBJACK |
---|
[4825] | 30 | |
---|
| 31 | static void unregister_ports(struct driver_jack * self) |
---|
| 32 | { |
---|
| 33 | while(self->channels--) |
---|
| 34 | { |
---|
| 35 | if ( self->ports_in != NULL ) |
---|
| 36 | if ( self->ports_in [self->channels] != NULL ) |
---|
| 37 | jack_port_unregister(self->client, self->ports_in [self->channels]); |
---|
| 38 | if ( self->ports_out[self->channels] != NULL ) |
---|
| 39 | jack_port_unregister(self->client, self->ports_out[self->channels]); |
---|
| 40 | } |
---|
| 41 | } |
---|
| 42 | |
---|
[4490] | 43 | int driver_jack_open_vio (struct roar_vio_calls * inst, |
---|
| 44 | char * device, |
---|
| 45 | struct roar_audio_info * info, |
---|
| 46 | int fh, |
---|
| 47 | struct roar_stream_server * sstream) { |
---|
[4492] | 48 | struct driver_jack * self; |
---|
[4825] | 49 | char port_name[128]; |
---|
| 50 | jack_nframes_t new_rate; |
---|
| 51 | int autoconfig = 0; |
---|
| 52 | int recsource = 0; |
---|
[4490] | 53 | |
---|
| 54 | // we are not FH Safe, return error if fh != -1: |
---|
| 55 | if ( fh != -1 ) |
---|
| 56 | return -1; |
---|
| 57 | |
---|
[4825] | 58 | if ( sstream != NULL ) { |
---|
| 59 | autoconfig = streams_get_flag(ROAR_STREAM(sstream)->id, ROAR_FLAG_AUTOCONF); |
---|
| 60 | recsource = streams_get_flag(ROAR_STREAM(sstream)->id, ROAR_FLAG_RECSOURCE); |
---|
| 61 | } |
---|
| 62 | |
---|
[4492] | 63 | // set up VIO: |
---|
| 64 | memset(inst, 0, sizeof(struct roar_vio_calls)); |
---|
| 65 | |
---|
| 66 | inst->read = driver_jack_read; |
---|
| 67 | inst->write = driver_jack_write; |
---|
| 68 | inst->lseek = NULL; // no seeking on this device |
---|
| 69 | inst->nonblock = driver_jack_nonblock; |
---|
| 70 | inst->sync = driver_jack_sync; |
---|
| 71 | inst->ctl = driver_jack_ctl; |
---|
| 72 | inst->close = driver_jack_close; |
---|
| 73 | |
---|
| 74 | // set up internal struct: |
---|
| 75 | if ( (self = roar_mm_malloc(sizeof(struct driver_jack))) == NULL ) |
---|
| 76 | return -1; |
---|
| 77 | |
---|
| 78 | memset(self, 0, sizeof(struct driver_jack)); |
---|
| 79 | |
---|
| 80 | inst->inst = self; |
---|
| 81 | |
---|
[4825] | 82 | if ( (self->client = jack_client_open("roard", JackNullOption, NULL)) == NULL ) { |
---|
[4493] | 83 | roar_mm_free(self); |
---|
| 84 | return -1; |
---|
| 85 | } |
---|
| 86 | |
---|
[4825] | 87 | new_rate = jack_get_sample_rate(self->client); |
---|
| 88 | |
---|
| 89 | // need to check if we need to change stream's parameters: |
---|
| 90 | if ( info->rate != new_rate || info->bits != 32 || info->codec != ROAR_CODEC_DEFAULT ) { |
---|
| 91 | if ( autoconfig ) { |
---|
| 92 | // we are allowed to change the parameters |
---|
| 93 | info->rate = new_rate; |
---|
| 94 | info->bits = 32; |
---|
| 95 | info->codec = ROAR_CODEC_DEFAULT; |
---|
| 96 | } else { |
---|
| 97 | // we are not allowed to change the parameters |
---|
| 98 | ROAR_WARN("driver_jack_open_vio(*): Can not open jack driver with given parameters, try -oO ...,autoconf"); |
---|
| 99 | goto free_close; |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | if ( recsource ) |
---|
| 104 | if ( (self->ports_in = roar_mm_malloc(sizeof(jack_port_t *) * info->channels)) == NULL ) |
---|
| 105 | goto free_close; |
---|
| 106 | |
---|
| 107 | if ( (self->ports_out = roar_mm_malloc(sizeof(jack_port_t *) * info->channels)) == NULL ) |
---|
| 108 | goto free_ins; |
---|
| 109 | |
---|
| 110 | // NULL ports so we do not segfaul if something goes wrong. |
---|
| 111 | if ( self->ports_in != NULL ) |
---|
| 112 | memset(self->ports_in, 0, sizeof(jack_port_t *) * info->channels); |
---|
| 113 | memset(self->ports_in, 0, sizeof(jack_port_t *) * info->channels); |
---|
| 114 | |
---|
| 115 | for (self->channels = 0; self->channels < info->channels; self->channels++) { |
---|
| 116 | if ( recsource ) { |
---|
[4887] | 117 | snprintf(port_name, sizeof(port_name), "in_%03u", self->channels); |
---|
[4825] | 118 | if ( (self->ports_in [self->channels] = jack_port_register(self->client, port_name, |
---|
| 119 | JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0)) == NULL ) |
---|
| 120 | goto unregister_ports; |
---|
| 121 | } |
---|
| 122 | |
---|
[4887] | 123 | snprintf(port_name, sizeof(port_name), "out_%03u", self->channels); |
---|
[4825] | 124 | if ( (self->ports_out[self->channels] = jack_port_register(self->client, port_name, |
---|
| 125 | JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0)) == NULL ) { |
---|
| 126 | jack_port_unregister(self->client, self->ports_in[self->channels]); |
---|
| 127 | goto unregister_ports; |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | if (jack_activate(self->client) != 0) |
---|
| 132 | goto unregister_ports; |
---|
| 133 | |
---|
[4493] | 134 | return 0; |
---|
[4825] | 135 | |
---|
| 136 | unregister_ports: |
---|
| 137 | unregister_ports(self); |
---|
| 138 | free_ins: |
---|
| 139 | if ( self->ports_in != NULL ) |
---|
| 140 | roar_mm_free(self->ports_in); |
---|
| 141 | free_close: |
---|
| 142 | jack_client_close(self->client); |
---|
| 143 | roar_mm_free(self); |
---|
| 144 | return -1; // error |
---|
[4492] | 145 | } |
---|
| 146 | |
---|
| 147 | ssize_t driver_jack_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 148 | struct driver_jack * self = vio->inst; |
---|
| 149 | // read up to count bytes into buf. |
---|
| 150 | // return the number of bits read. |
---|
| 151 | return -1; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | ssize_t driver_jack_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 155 | struct driver_jack * self = vio->inst; |
---|
| 156 | // write up to count bytes from buf. |
---|
| 157 | // return the number of written bytes. |
---|
| 158 | return -1; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | int driver_jack_nonblock(struct roar_vio_calls * vio, int state) { |
---|
| 162 | struct driver_jack * self = vio->inst; |
---|
| 163 | // control if read and write calls should block untill all data is read or written. |
---|
| 164 | // state could be: |
---|
| 165 | // ROAR_SOCKET_BLOCK - Block untill the data is read or written |
---|
| 166 | // ROAR_SOCKET_NONBLOCK - Return as soon as possible |
---|
[4490] | 167 | return -1; |
---|
| 168 | } |
---|
[4492] | 169 | |
---|
| 170 | int driver_jack_sync (struct roar_vio_calls * vio) { |
---|
| 171 | struct driver_jack * self = vio->inst; |
---|
| 172 | // init to sync data to device. |
---|
| 173 | // sync does not need to be complet when this function returns. |
---|
| 174 | return 0; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | int driver_jack_ctl (struct roar_vio_calls * vio, int cmd, void * data) { |
---|
| 178 | struct driver_jack * self = vio->inst; |
---|
| 179 | // function for a lot control features. |
---|
| 180 | |
---|
| 181 | switch (cmd) { |
---|
| 182 | case ROAR_VIO_CTL_GET_NAME: |
---|
| 183 | if ( data == NULL ) |
---|
| 184 | return -1; |
---|
| 185 | |
---|
| 186 | *(char**)data = "driver_jack"; |
---|
| 187 | return 0; |
---|
| 188 | break; |
---|
| 189 | case ROAR_VIO_CTL_GET_FH: |
---|
| 190 | case ROAR_VIO_CTL_GET_READ_FH: |
---|
| 191 | case ROAR_VIO_CTL_GET_WRITE_FH: |
---|
| 192 | case ROAR_VIO_CTL_GET_SELECT_FH: |
---|
| 193 | case ROAR_VIO_CTL_GET_SELECT_READ_FH: |
---|
| 194 | case ROAR_VIO_CTL_GET_SELECT_WRITE_FH: |
---|
| 195 | /* Return FH if possible: |
---|
| 196 | *(int*)data = FH...; |
---|
| 197 | */ |
---|
| 198 | return -1; |
---|
| 199 | break; |
---|
| 200 | case ROAR_VIO_CTL_SET_NOSYNC: |
---|
| 201 | vio->sync = NULL; |
---|
| 202 | return 0; |
---|
| 203 | break; |
---|
| 204 | case ROAR_VIO_CTL_GET_AUINFO: |
---|
| 205 | case ROAR_VIO_CTL_SET_AUINFO: |
---|
| 206 | // get or set audio info, data is a struct roar_audio_info*. |
---|
| 207 | return -1; |
---|
| 208 | break; |
---|
| 209 | case ROAR_VIO_CTL_GET_DBLKSIZE: |
---|
| 210 | case ROAR_VIO_CTL_SET_DBLKSIZE: |
---|
| 211 | // get or set block size used, data is uint_least32_t*, number of bytes. |
---|
| 212 | return -1; |
---|
| 213 | break; |
---|
| 214 | case ROAR_VIO_CTL_GET_DBLOCKS: |
---|
| 215 | case ROAR_VIO_CTL_SET_DBLOCKS: |
---|
| 216 | // get or set number of blocks used, data is uint_least32_t*. |
---|
| 217 | return -1; |
---|
| 218 | break; |
---|
| 219 | case ROAR_VIO_CTL_SET_SSTREAM: |
---|
| 220 | // set server stream object for this stream, data is struct roar_stream_server* |
---|
| 221 | return -1; |
---|
| 222 | break; |
---|
| 223 | case ROAR_VIO_CTL_SET_SSTREAMID: |
---|
| 224 | // set stream ID for this stream, data is int* |
---|
| 225 | return -1; |
---|
| 226 | break; |
---|
| 227 | case ROAR_VIO_CTL_SET_VOLUME: |
---|
| 228 | // set volume for this device, data is struct roar_mixer_settings* |
---|
| 229 | return -1; |
---|
| 230 | break; |
---|
| 231 | case ROAR_VIO_CTL_GET_DELAY: |
---|
| 232 | // get delay of this stream, data is uint_least32_t*, in bytes |
---|
| 233 | // there is more about delay. please ask. |
---|
| 234 | return -1; |
---|
| 235 | break; |
---|
| 236 | default: |
---|
| 237 | return -1; |
---|
| 238 | break; |
---|
| 239 | } |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | int driver_jack_close (struct roar_vio_calls * vio) { |
---|
| 243 | struct driver_jack * self = vio->inst; |
---|
| 244 | // close and free everything in here... |
---|
| 245 | |
---|
[4825] | 246 | jack_deactivate(self->client); |
---|
| 247 | unregister_ports(self); |
---|
[4493] | 248 | jack_client_close(self->client); |
---|
| 249 | |
---|
[4492] | 250 | roar_mm_free(self); |
---|
| 251 | |
---|
| 252 | return 0; |
---|
| 253 | } |
---|
| 254 | |
---|
[4490] | 255 | #endif |
---|
| 256 | |
---|
| 257 | //ll |
---|