[0] | 1 | //esdctl.c: |
---|
| 2 | |
---|
[705] | 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
| 5 | * The code (may) include prototypes and comments (and maybe |
---|
| 6 | * other code fragements) from EsounD. |
---|
| 7 | * They are mostly copyrighted by Eric B. Mitchell (aka 'Ricdude) |
---|
| 8 | * <ericmit@ix.netcom.com>. For more information see AUTHORS.esd. |
---|
| 9 | * |
---|
| 10 | * This file is part of libroaresd a part of RoarAudio, |
---|
| 11 | * a cross-platform sound system for both, home and professional use. |
---|
| 12 | * See README for details. |
---|
| 13 | * |
---|
| 14 | * This file is free software; you can redistribute it and/or modify |
---|
| 15 | * it under the terms of the GNU General Public License version 3 |
---|
| 16 | * as published by the Free Software Foundation. |
---|
| 17 | * |
---|
| 18 | * RoarAudio is distributed in the hope that it will be useful, |
---|
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 21 | * GNU General Public License for more details. |
---|
| 22 | * |
---|
| 23 | * You should have received a copy of the GNU General Public License |
---|
| 24 | * along with this software; see the file COPYING. If not, write to |
---|
[3517] | 25 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 26 | * Boston, MA 02110-1301, USA. |
---|
[705] | 27 | * |
---|
| 28 | * NOTE for everyone want's to change something and send patches: |
---|
| 29 | * read README and HACKING! There a addition information on |
---|
| 30 | * the license of this document you need to read before you send |
---|
| 31 | * any patches. |
---|
| 32 | * |
---|
| 33 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 34 | * or libpulse*: |
---|
| 35 | * The libs libroaresd, libroararts and libroarpulse link this libroar |
---|
| 36 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 37 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 38 | */ |
---|
| 39 | |
---|
[0] | 40 | #include "libroaresd.h" |
---|
| 41 | |
---|
| 42 | /* lock/unlock will disable/enable foreign clients from connecting */ |
---|
| 43 | |
---|
| 44 | // we do not have such a thing at the moment... |
---|
| 45 | // so always return -1 |
---|
| 46 | int esd_lock( int esd ) { |
---|
| 47 | return -1; |
---|
| 48 | } |
---|
| 49 | int esd_unlock( int esd ) { |
---|
| 50 | return -1; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | /* standby/resume will free/reclaim audio device so others may use it */ |
---|
| 54 | int esd_standby( int esd ) { |
---|
| 55 | struct roar_connection con; |
---|
| 56 | |
---|
[1660] | 57 | roar_connect_fh(&con, esd); |
---|
[0] | 58 | |
---|
| 59 | return roar_set_standby(&con, ROAR_STANDBY_ACTIVE); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | int esd_resume( int esd ) { |
---|
| 63 | struct roar_connection con; |
---|
| 64 | |
---|
[1660] | 65 | roar_connect_fh(&con, esd); |
---|
[0] | 66 | |
---|
| 67 | return roar_set_standby(&con, ROAR_STANDBY_INACTIVE); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | /* print server into to stdout */ |
---|
| 71 | void esd_print_server_info( esd_server_info_t *server_info ) { |
---|
| 72 | char buf[80] = ""; |
---|
| 73 | |
---|
[631] | 74 | if ( server_info->format & ESD_BITS16 ) |
---|
| 75 | strcat(buf, "16 bit "); |
---|
| 76 | else |
---|
[0] | 77 | strcat(buf, "8 bit "); |
---|
| 78 | |
---|
[28] | 79 | if ( server_info->format & ESD_STEREO ) |
---|
[0] | 80 | strcat(buf, "stereo "); |
---|
| 81 | else |
---|
| 82 | strcat(buf, "mono "); |
---|
| 83 | |
---|
| 84 | printf("server version = %i\n", server_info->version); |
---|
| 85 | printf("server format = 0x%08x %s\n", server_info->format, buf); |
---|
| 86 | printf("server rate = %i\n", server_info->rate); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | void esd_print_player_info( esd_player_info_t *player_info ) { |
---|
| 91 | char buf[80] = ""; |
---|
| 92 | |
---|
[2540] | 93 | if ( (player_info->format & ESD_BITS16) == ESD_BITS16 ) |
---|
[631] | 94 | strcat(buf, "16 bit "); |
---|
| 95 | else |
---|
[0] | 96 | strcat(buf, "8 bit "); |
---|
| 97 | |
---|
[2540] | 98 | if ( (player_info->format & ESD_STEREO) == ESD_STEREO ) |
---|
[0] | 99 | strcat(buf, "stereo "); |
---|
| 100 | else |
---|
| 101 | strcat(buf, "mono "); |
---|
| 102 | |
---|
| 103 | printf("player %i name = %s\n", player_info->source_id, player_info->name); |
---|
| 104 | printf("player %i format = 0x%08x %s\n", player_info->source_id, player_info->format, buf); |
---|
| 105 | printf("player %i rate = %i\n", player_info->source_id, player_info->rate); |
---|
| 106 | printf("player %i left = %i\n", player_info->source_id, player_info->left_vol_scale ); |
---|
| 107 | printf("player %i right = %i\n", player_info->source_id, player_info->right_vol_scale ); |
---|
| 108 | } |
---|
[26] | 109 | |
---|
[0] | 110 | void esd_print_sample_info( esd_sample_info_t *sample_info ) { |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | /* print all info to stdout */ |
---|
| 114 | void esd_print_all_info( esd_info_t *all_info ) { |
---|
| 115 | esd_player_info_t *player; |
---|
| 116 | esd_sample_info_t *sample; |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | esd_print_server_info(all_info->server); |
---|
| 120 | |
---|
| 121 | player = all_info->player_list; |
---|
| 122 | |
---|
| 123 | while (player) { |
---|
| 124 | esd_print_player_info(player); |
---|
| 125 | player = player->next; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | sample = all_info->sample_list; |
---|
| 129 | |
---|
| 130 | while (sample) { |
---|
| 131 | esd_print_sample_info(sample); |
---|
| 132 | sample = sample->next; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | /* retrieve server properties (sample rate, format, version number) */ |
---|
| 138 | esd_server_info_t *esd_get_server_info( int esd ) { |
---|
| 139 | esd_server_info_t * r; |
---|
| 140 | struct roar_stream s; |
---|
| 141 | struct roar_connection con; |
---|
| 142 | struct roar_message m; |
---|
| 143 | |
---|
| 144 | r = malloc(sizeof(esd_server_info_t)); |
---|
| 145 | |
---|
| 146 | if ( r == NULL ) |
---|
| 147 | return NULL; |
---|
| 148 | |
---|
| 149 | r->version = 0; // seems to be static |
---|
| 150 | |
---|
[1660] | 151 | roar_connect_fh(&con, esd); |
---|
[0] | 152 | m.cmd = ROAR_CMD_SERVER_OINFO; |
---|
| 153 | m.datalen = 0; |
---|
| 154 | |
---|
| 155 | if ( roar_req(&con, &m, NULL) == -1 ) { |
---|
| 156 | free(r); |
---|
| 157 | return NULL; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | if ( m.cmd != ROAR_CMD_OK ) { |
---|
| 161 | free(r); |
---|
| 162 | return NULL; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | if ( roar_stream_m2s(&s, &m) == -1 ) { |
---|
| 166 | free(r); |
---|
| 167 | return NULL; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | r->rate = s.info.rate; |
---|
| 171 | r->format = 0; |
---|
| 172 | |
---|
| 173 | if ( s.info.channels == 1 ) |
---|
| 174 | r->format |= ESD_MONO; |
---|
| 175 | else |
---|
| 176 | r->format |= ESD_STEREO; |
---|
| 177 | |
---|
| 178 | if ( s.info.bits == 8 ) |
---|
| 179 | r->format |= ESD_BITS8; |
---|
| 180 | else |
---|
| 181 | r->format |= ESD_BITS16; |
---|
| 182 | |
---|
| 183 | return r; |
---|
| 184 | } |
---|
| 185 | /* release all memory allocated for the server properties structure */ |
---|
| 186 | void esd_free_server_info( esd_server_info_t *server_info ) { |
---|
| 187 | if (server_info) |
---|
| 188 | free(server_info); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | /* retrieve all information from server */ |
---|
| 192 | esd_info_t *esd_get_all_info( int esd ) { |
---|
| 193 | esd_info_t * r; |
---|
| 194 | int i; |
---|
| 195 | int num; |
---|
| 196 | int clients[ROAR_CLIENTS_MAX]; |
---|
| 197 | struct roar_client c; |
---|
| 198 | struct roar_stream s; |
---|
| 199 | struct roar_connection con[1]; |
---|
[26] | 200 | struct roar_mixer_settings mixer; |
---|
| 201 | int channels; |
---|
[3535] | 202 | float fs; |
---|
[0] | 203 | esd_player_info_t * new_player, * cur = NULL; // = NULL to avoid gcc warning |
---|
| 204 | |
---|
[1660] | 205 | roar_connect_fh(con, esd); |
---|
[0] | 206 | |
---|
| 207 | r = malloc(sizeof(esd_info_t)); |
---|
| 208 | |
---|
| 209 | if ( r == NULL ) |
---|
| 210 | return NULL; |
---|
| 211 | |
---|
| 212 | r->server = esd_get_server_info(esd); |
---|
| 213 | r->player_list = NULL; |
---|
| 214 | r->sample_list = NULL; |
---|
| 215 | |
---|
| 216 | if ( (num = roar_list_clients(con, clients, ROAR_CLIENTS_MAX)) == -1 ) { |
---|
| 217 | ROAR_ERR("esd_get_all_info(*): can not get client list"); |
---|
| 218 | num = 0; |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | for (i = 0; i < num; i++) { |
---|
| 222 | if ( roar_get_client(con, &c, clients[i]) == -1 ) { |
---|
| 223 | ROAR_ERR("esd_get_all_info(*): can not get client info"); |
---|
| 224 | continue; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | if ( c.execed != -1 ) { |
---|
| 228 | if ( roar_get_stream(con, &s, c.execed) == -1 ) { |
---|
| 229 | ROAR_ERR("esd_get_all_info(*): can not get stream info"); |
---|
| 230 | continue; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | if ( (new_player = malloc(sizeof(esd_player_info_t))) == NULL ) { |
---|
| 234 | ROAR_ERR("esd_get_all_info(*): can not alloc memory for new player! BAD"); |
---|
| 235 | continue; |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | new_player->next = NULL; |
---|
| 239 | |
---|
| 240 | new_player->source_id = c.execed; |
---|
| 241 | new_player->rate = s.info.rate; |
---|
| 242 | |
---|
| 243 | new_player->format = ROAR_S2ESD(&s); |
---|
| 244 | // sprintf(new_player->name, "roar stream %i", c.execed); |
---|
| 245 | strncpy(new_player->name, c.name, ESD_NAME_MAX < ROAR_BUFFER_NAME ? ESD_NAME_MAX : ESD_NAME_MAX); |
---|
| 246 | |
---|
| 247 | new_player->server = r->server; |
---|
[26] | 248 | |
---|
| 249 | if ( roar_get_vol(con, c.execed, &mixer, &channels) == -1 ) { |
---|
| 250 | ROAR_ERR("esd_get_all_info(*): can not get stream mixer info"); |
---|
| 251 | new_player->left_vol_scale = new_player->right_vol_scale = 256; |
---|
| 252 | } else { |
---|
[3535] | 253 | fs = mixer.scale / 257; |
---|
[26] | 254 | if ( channels == 1 ) { |
---|
[3535] | 255 | new_player->left_vol_scale = new_player->right_vol_scale = mixer.mixer[0] == mixer.scale ? 256 : mixer.mixer[0] / fs; |
---|
[26] | 256 | } else { |
---|
| 257 | if ( channels != 2 ) { |
---|
| 258 | ROAR_ERR("esd_get_all_info(*): server seems to run in > 2 channel mode. ignoring any but the first two channels!"); |
---|
| 259 | } |
---|
[3535] | 260 | new_player->left_vol_scale = mixer.mixer[0] == mixer.scale ? 256 : mixer.mixer[0] / fs; |
---|
| 261 | new_player->right_vol_scale = mixer.mixer[1] == mixer.scale ? 256 : mixer.mixer[1] / fs; |
---|
[26] | 262 | } |
---|
| 263 | } |
---|
| 264 | |
---|
[0] | 265 | |
---|
| 266 | if ( r->player_list == NULL ) { |
---|
| 267 | r->player_list = cur = new_player; |
---|
| 268 | } else { |
---|
| 269 | cur->next = new_player; // add to old player |
---|
| 270 | cur = new_player; // hop to next player |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | } |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | return r; |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | /* retrieve all information from server, and update until unsubsribed or closed */ |
---|
| 280 | esd_info_t *esd_subscribe_all_info( int esd ); |
---|
| 281 | |
---|
| 282 | /* call to update the info structure with new information, and call callbacks */ |
---|
| 283 | esd_info_t *esd_update_info( int esd, esd_info_t *info, |
---|
| 284 | esd_update_info_callbacks_t *callbacks ); |
---|
| 285 | esd_info_t *esd_unsubscribe_info( int esd ); |
---|
| 286 | |
---|
| 287 | /* release all memory allocated for the esd info structure */ |
---|
| 288 | void esd_free_all_info( esd_info_t *info ) { |
---|
| 289 | esd_player_info_t *player, *oplayer; |
---|
| 290 | esd_sample_info_t *sample, *osample; |
---|
| 291 | |
---|
| 292 | if ( info != NULL ) { |
---|
| 293 | |
---|
| 294 | /* |
---|
| 295 | if ( info->server ) |
---|
| 296 | free(info->server); |
---|
| 297 | */ |
---|
| 298 | esd_free_server_info(info->server); |
---|
| 299 | |
---|
| 300 | |
---|
| 301 | /* |
---|
| 302 | // TODO: do we need to free more? |
---|
| 303 | if ( info->player_list ) |
---|
| 304 | free(info->player_list); |
---|
| 305 | |
---|
| 306 | if ( info->sample_list ) |
---|
| 307 | free(info->sample_list); |
---|
| 308 | */ |
---|
| 309 | |
---|
| 310 | player = info->player_list; |
---|
| 311 | |
---|
| 312 | while (player) { |
---|
| 313 | oplayer = player; |
---|
| 314 | player = player->next; |
---|
| 315 | |
---|
| 316 | free(oplayer); |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | sample = info->sample_list; |
---|
| 320 | |
---|
| 321 | while (sample) { |
---|
| 322 | osample = sample; |
---|
| 323 | sample = sample->next; |
---|
| 324 | |
---|
| 325 | free(osample); |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | free(info); |
---|
| 329 | } |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | |
---|
| 333 | /* reset the volume panning for a stream */ |
---|
| 334 | int esd_set_stream_pan( int esd, int stream_id, |
---|
| 335 | int left_scale, int right_scale ) { |
---|
[17] | 336 | struct roar_connection con; |
---|
| 337 | struct roar_mixer_settings mixer; |
---|
| 338 | |
---|
[1660] | 339 | roar_connect_fh(&con, esd); |
---|
[17] | 340 | |
---|
[3535] | 341 | mixer.scale = 256; |
---|
| 342 | mixer.mixer[0] = left_scale; |
---|
| 343 | mixer.mixer[1] = right_scale; |
---|
[17] | 344 | |
---|
| 345 | ROAR_DBG("esd_set_stream_pan(esd=%i, stream_id=%i, left_scale=%i, right_scale=%i) = ?", |
---|
| 346 | esd, stream_id, left_scale, right_scale); |
---|
| 347 | |
---|
| 348 | return roar_set_vol(&con, stream_id, &mixer, 2); |
---|
[0] | 349 | } |
---|
| 350 | |
---|
| 351 | /* reset the default volume panning for a sample */ |
---|
| 352 | int esd_set_default_sample_pan( int esd, int sample_id, |
---|
| 353 | int left_scale, int right_scale ) { |
---|
| 354 | return -1; |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | /* see if the server is in stnaby, autostandby, etc */ |
---|
| 358 | esd_standby_mode_t esd_get_standby_mode( int esd ) { |
---|
| 359 | return roar_simple_get_standby(esd); |
---|
| 360 | } |
---|
| 361 | |
---|
| 362 | //ll |
---|