[0] | 1 | //sources.c: |
---|
| 2 | |
---|
[668] | 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
| 5 | * |
---|
| 6 | * This file is part of roard a part of RoarAudio, |
---|
| 7 | * a cross-platform sound system for both, home and professional use. |
---|
| 8 | * See README for details. |
---|
| 9 | * |
---|
| 10 | * This file is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License version 3 |
---|
| 12 | * as published by the Free Software Foundation. |
---|
| 13 | * |
---|
| 14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this software; see the file COPYING. If not, write to |
---|
| 21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 22 | * |
---|
| 23 | */ |
---|
| 24 | |
---|
[0] | 25 | #include "roard.h" |
---|
| 26 | |
---|
[2270] | 27 | struct roar_source g_source[] = { |
---|
[2271] | 28 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[2269] | 29 | {"raw", "Old raw source", "/some/file", SRC_FLAG_NONE, ROAR_SUBSYS_WAVEFORM, sources_add_raw, NULL}, |
---|
| 30 | {"wav", "Old RIFF/WAVE source", "/some/file.wav", SRC_FLAG_NONE, ROAR_SUBSYS_WAVEFORM, sources_add_wav, NULL}, |
---|
[2271] | 31 | #endif |
---|
[2269] | 32 | {"cf", "Old CF source", "/some/file.ext", SRC_FLAG_NONE, ROAR_SUBSYS_WAVEFORM, sources_add_cf, NULL}, |
---|
| 33 | {"roar", "Old simple RoarAudio source", "some.host", SRC_FLAG_NONE, ROAR_SUBSYS_WAVEFORM, sources_add_roar, NULL}, |
---|
| 34 | {NULL, NULL, NULL, SRC_FLAG_NONE, 0, NULL, NULL} // EOL |
---|
| 35 | }; |
---|
| 36 | |
---|
[0] | 37 | int sources_init (void) { |
---|
| 38 | g_source_client = -1; |
---|
| 39 | return 0; |
---|
| 40 | } |
---|
| 41 | |
---|
[2270] | 42 | void print_sourcelist (void) { |
---|
| 43 | int i; |
---|
| 44 | char subsys[7] = " "; |
---|
| 45 | |
---|
| 46 | printf(" Source Flag Subsys - Description (devices)\n"); |
---|
| 47 | printf("------------------------------------------------------\n"); |
---|
| 48 | |
---|
| 49 | for (i = 0; g_source[i].name != NULL; i++) { |
---|
| 50 | strncpy(subsys, " ", 6); |
---|
| 51 | |
---|
| 52 | if ( g_source[i].subsystems & ROAR_SUBSYS_WAVEFORM ) |
---|
| 53 | subsys[0] = 'W'; |
---|
| 54 | if ( g_source[i].subsystems & ROAR_SUBSYS_MIDI ) |
---|
| 55 | subsys[1] = 'M'; |
---|
| 56 | if ( g_source[i].subsystems & ROAR_SUBSYS_CB ) |
---|
| 57 | subsys[2] = 'C'; |
---|
| 58 | if ( g_source[i].subsystems & ROAR_SUBSYS_LIGHT ) |
---|
| 59 | subsys[3] = 'L'; |
---|
| 60 | if ( g_source[i].subsystems & ROAR_SUBSYS_RAW ) |
---|
| 61 | subsys[4] = 'R'; |
---|
| 62 | |
---|
| 63 | printf(" %-9s %c%c%c %6s - %s (devices: %s)\n", g_source[i].name, |
---|
| 64 | g_source[i].flags & DRV_FLAG_FHSEC ? 's' : ' ', |
---|
| 65 | g_source[i].old_open != NULL ? 'S' : ' ', |
---|
| 66 | g_source[i].new_open != NULL ? 'N' : ' ', |
---|
| 67 | subsys, |
---|
| 68 | g_source[i].desc, g_source[i].devices); |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | |
---|
[64] | 73 | int sources_set_client (int client) { |
---|
| 74 | if ( client >= 0 ) { |
---|
| 75 | g_source_client = client; |
---|
| 76 | return 0; |
---|
| 77 | } else { |
---|
| 78 | return -1; |
---|
| 79 | } |
---|
| 80 | } |
---|
[0] | 81 | |
---|
| 82 | int sources_free (void) { |
---|
| 83 | return 0; |
---|
| 84 | } |
---|
| 85 | |
---|
[65] | 86 | int sources_add (char * driver, char * device, char * container, char * options, int primary) { |
---|
[2272] | 87 | int i; |
---|
| 88 | |
---|
| 89 | for (i = 0; g_source[i].name != NULL; i++) { |
---|
| 90 | if ( !strcmp(g_source[i].name, driver) ) { |
---|
| 91 | if ( g_source[i].new_open != NULL ) { |
---|
[2273] | 92 | return sources_add_new(&(g_source[i]), driver, device, container, options, primary); |
---|
[2272] | 93 | } else if ( g_source[i].old_open != NULL ) { |
---|
| 94 | return g_source[i].old_open(driver, device, container, options, primary); |
---|
| 95 | } else { |
---|
| 96 | ROAR_ERR("sources_add(driver='%s', ...): Found source but did not find any open rutine", driver); |
---|
| 97 | return -1; |
---|
| 98 | } |
---|
| 99 | } |
---|
[67] | 100 | } |
---|
[65] | 101 | |
---|
[2272] | 102 | ROAR_ERR("sources_add(driver='%s', ...): Source not found", driver); |
---|
[0] | 103 | return -1; |
---|
| 104 | } |
---|
| 105 | |
---|
[2273] | 106 | int sources_add_new (struct roar_source * source, |
---|
| 107 | char * driver, char * device, |
---|
| 108 | char * container, |
---|
| 109 | char * options, int primary) { |
---|
[2274] | 110 | int stream; |
---|
| 111 | int fh = -1; |
---|
| 112 | struct roar_stream * s; |
---|
| 113 | struct roar_stream_server * ss; |
---|
| 114 | char * k, * v; |
---|
| 115 | int error = 0; |
---|
| 116 | int f_sync = 0, f_mmap = 0; |
---|
| 117 | int codec; |
---|
| 118 | |
---|
| 119 | if ( source == NULL ) |
---|
| 120 | return -1; |
---|
| 121 | |
---|
| 122 | if ( (stream = streams_new()) == -1 ) { |
---|
| 123 | return -1; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | streams_get(stream, &ss); |
---|
| 127 | s = ROAR_STREAM(ss); |
---|
| 128 | |
---|
| 129 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 130 | |
---|
| 131 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
| 132 | streams_delete(stream); |
---|
| 133 | return -1; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | s->pos_rel_id = -1; |
---|
| 137 | |
---|
| 138 | k = strtok(options, ","); |
---|
| 139 | while (k != NULL) { |
---|
| 140 | if ( (v = strstr(k, "=")) != NULL ) { |
---|
| 141 | *v++ = 0; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | if ( strcmp(k, "rate") == 0 ) { |
---|
| 145 | s->info.rate = atoi(v); |
---|
| 146 | } else if ( strcmp(k, "channels") == 0 ) { |
---|
| 147 | s->info.channels = atoi(v); |
---|
| 148 | } else if ( strcmp(k, "bits") == 0 ) { |
---|
| 149 | s->info.bits = atoi(v); |
---|
| 150 | } else if ( strcmp(k, "codec") == 0 ) { |
---|
| 151 | if ( (codec = roar_str2codec(v)) == -1 ) { |
---|
| 152 | ROAR_ERR("sources_add_new(*): unknown codec '%s'", v); |
---|
| 153 | error++; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | } else if ( strcmp(k, "name") == 0 ) { |
---|
| 157 | if ( streams_set_name(stream, v) == -1 ) { |
---|
| 158 | ROAR_ERR("add_output(*): Can not set Stream name"); |
---|
| 159 | error++; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | } else if ( strcmp(k, "mmap") == 0 ) { |
---|
| 163 | f_mmap = 1; |
---|
| 164 | } else if ( strcmp(k, "sync") == 0 ) { |
---|
| 165 | f_sync = 1; |
---|
| 166 | } else if ( strcmp(k, "primary") == 0 ) { |
---|
| 167 | primary = 1; |
---|
| 168 | } else if ( strcmp(k, "meta") == 0 ) { |
---|
| 169 | streams_set_flag(stream, ROAR_FLAG_META); |
---|
| 170 | } else if ( strcmp(k, "cleanmeta") == 0 ) { |
---|
| 171 | streams_set_flag(stream, ROAR_FLAG_CLEANMETA); |
---|
| 172 | } else if ( strcmp(k, "autoconf") == 0 ) { |
---|
| 173 | streams_set_flag(stream, ROAR_FLAG_AUTOCONF); |
---|
| 174 | |
---|
| 175 | } else { |
---|
| 176 | ROAR_ERR("sources_add_new(*): unknown option '%s'", k); |
---|
| 177 | error++; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | if ( error ) { |
---|
| 181 | streams_delete(stream); |
---|
| 182 | if ( primary ) alive = 0; |
---|
| 183 | return -1; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | k = strtok(NULL, ","); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | if ( primary ) |
---|
| 190 | streams_mark_primary(stream); |
---|
| 191 | |
---|
| 192 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
| 193 | client_stream_add(g_source_client, stream); |
---|
| 194 | |
---|
| 195 | if ( source->new_open(stream, device, fh) == -1 ) { |
---|
| 196 | streams_delete(stream); |
---|
| 197 | return -1; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAMID, &stream); // ignore errors here |
---|
| 201 | roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_SET_SSTREAM, s); // ignore errors here |
---|
| 202 | |
---|
| 203 | if ( f_sync ) { |
---|
| 204 | streams_set_flag(stream, ROAR_FLAG_SYNC); |
---|
| 205 | } else { |
---|
| 206 | streams_reset_flag(stream, ROAR_FLAG_SYNC); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | if ( f_mmap ) |
---|
| 210 | streams_set_flag(stream, ROAR_FLAG_MMAP); |
---|
| 211 | |
---|
| 212 | return 0; |
---|
[2273] | 213 | } |
---|
| 214 | |
---|
[1499] | 215 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[65] | 216 | int sources_add_raw (char * driver, char * device, char * container, char * options, int primary) { |
---|
| 217 | int stream; |
---|
| 218 | int fh; |
---|
| 219 | struct roar_stream * s; |
---|
| 220 | |
---|
[566] | 221 | ROAR_WARN("sources_add_raw(*): The raw source is obsolete, use source 'cf' (default)!"); |
---|
| 222 | |
---|
[65] | 223 | if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) { |
---|
| 224 | return -1; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | if ( (stream = streams_new()) == -1 ) { |
---|
| 228 | close(fh); |
---|
| 229 | return -1; |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | streams_get(stream, (struct roar_stream_server **)&s); |
---|
| 233 | |
---|
| 234 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 235 | |
---|
[1609] | 236 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
| 237 | streams_delete(stream); |
---|
| 238 | close(fh); |
---|
| 239 | return -1; |
---|
| 240 | } |
---|
| 241 | |
---|
[65] | 242 | s->pos_rel_id = -1; |
---|
| 243 | |
---|
| 244 | streams_set_fh(stream, fh); |
---|
| 245 | |
---|
[1031] | 246 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
[65] | 247 | client_stream_add(g_source_client, stream); |
---|
| 248 | |
---|
| 249 | return 0; |
---|
| 250 | } |
---|
[1499] | 251 | #endif |
---|
[65] | 252 | |
---|
[1499] | 253 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[67] | 254 | int sources_add_wav (char * driver, char * device, char * container, char * options, int primary) { |
---|
| 255 | int stream; |
---|
| 256 | int fh; |
---|
| 257 | char buf[44]; |
---|
| 258 | struct roar_stream * s; |
---|
| 259 | |
---|
[566] | 260 | ROAR_WARN("sources_add_raw(*): The wav(e) source is obsolete, use source 'cf' (default)!"); |
---|
| 261 | |
---|
[67] | 262 | if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) { |
---|
| 263 | return -1; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | if (read(fh, buf, 44) != 44) { |
---|
| 267 | close(fh); |
---|
| 268 | return -1; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | if ( (stream = streams_new()) == -1 ) { |
---|
| 272 | close(fh); |
---|
| 273 | return -1; |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | streams_get(stream, (struct roar_stream_server **)&s); |
---|
| 277 | |
---|
| 278 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 279 | |
---|
[1499] | 280 | memcpy(&(s->info.rate ), buf+24, 4); |
---|
| 281 | memcpy(&(s->info.channels), buf+22, 2); |
---|
| 282 | memcpy(&(s->info.bits ), buf+34, 2); |
---|
[67] | 283 | |
---|
[1609] | 284 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
| 285 | streams_delete(stream); |
---|
| 286 | close(fh); |
---|
| 287 | return -1; |
---|
| 288 | } |
---|
[67] | 289 | s->pos_rel_id = -1; |
---|
| 290 | |
---|
| 291 | streams_set_fh(stream, fh); |
---|
| 292 | |
---|
[1031] | 293 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
[67] | 294 | client_stream_add(g_source_client, stream); |
---|
| 295 | |
---|
| 296 | return 0; |
---|
| 297 | } |
---|
[1499] | 298 | #endif |
---|
| 299 | |
---|
[1614] | 300 | #define _ret(x) streams_delete(stream); return (x) |
---|
[542] | 301 | |
---|
| 302 | int sources_add_cf (char * driver, char * device, char * container, char * options, int primary) { |
---|
[555] | 303 | int stream; |
---|
| 304 | int codec; |
---|
| 305 | int len; |
---|
| 306 | char buf[64]; |
---|
[1499] | 307 | struct roar_stream * s; |
---|
| 308 | struct roar_vio_calls * vio; |
---|
[1614] | 309 | struct roar_vio_defaults def; |
---|
| 310 | |
---|
| 311 | if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, O_RDONLY, 0644) == -1 ) |
---|
| 312 | return -1; |
---|
[555] | 313 | |
---|
[542] | 314 | if ( (stream = streams_new()) == -1 ) { |
---|
| 315 | return -1; |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | streams_get(stream, (struct roar_stream_server **)&s); |
---|
| 319 | |
---|
| 320 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 321 | |
---|
[1609] | 322 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
| 323 | streams_delete(stream); |
---|
| 324 | return -1; |
---|
| 325 | } |
---|
| 326 | |
---|
[542] | 327 | s->pos_rel_id = -1; |
---|
[1499] | 328 | |
---|
| 329 | /* |
---|
| 330 | if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) { |
---|
| 331 | return -1; |
---|
| 332 | } |
---|
| 333 | */ |
---|
| 334 | |
---|
| 335 | vio = &(ROAR_STREAM_SERVER(s)->vio); |
---|
| 336 | |
---|
[1614] | 337 | //if ( roar_vio_open_file(vio, device, O_RDONLY, 0644) == -1 ) { |
---|
| 338 | if ( roar_vio_open_dstr(vio, device, &def, 1) == -1 ) { |
---|
[1499] | 339 | _ret(-1); |
---|
| 340 | } |
---|
| 341 | |
---|
[1614] | 342 | ROAR_DBG("sources_add_cf(*) = ?"); |
---|
| 343 | |
---|
[1499] | 344 | // TODO: finy out a better way of doing auto detetion without need for seek! |
---|
| 345 | if ( options == NULL ) { |
---|
| 346 | if ( (len = roar_vio_read(vio, buf, 64)) < 1 ) { |
---|
| 347 | _ret(-1); |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | if ( roar_vio_lseek(vio, -len, SEEK_CUR) == (off_t)-1 ) { |
---|
| 351 | _ret(-1); |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | if ( (codec = roar_file_codecdetect(buf, len)) == -1 ) { |
---|
| 355 | _ret(-1); |
---|
| 356 | } |
---|
| 357 | } else { |
---|
| 358 | if ( !strncmp(options, "codec=", 6) ) |
---|
| 359 | options += 6; |
---|
| 360 | |
---|
| 361 | if ( (codec = roar_str2codec(options)) == -1 ) { |
---|
| 362 | _ret(-1); |
---|
| 363 | } |
---|
| 364 | } |
---|
| 365 | |
---|
[542] | 366 | s->info.codec = codec; |
---|
| 367 | |
---|
[566] | 368 | ROAR_STREAM_SERVER(s)->codec_orgi = codec; |
---|
| 369 | |
---|
[1614] | 370 | ROAR_DBG("sources_add_cf(*) = ?"); |
---|
[1504] | 371 | streams_set_fh(stream, -2); |
---|
[1614] | 372 | ROAR_DBG("sources_add_cf(*) = ?"); |
---|
[644] | 373 | streams_set_socktype(stream, ROAR_SOCKET_TYPE_FILE); |
---|
| 374 | |
---|
| 375 | if ( primary ) |
---|
| 376 | streams_mark_primary(stream); |
---|
[542] | 377 | |
---|
[1031] | 378 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
[542] | 379 | client_stream_add(g_source_client, stream); |
---|
| 380 | |
---|
| 381 | return 0; |
---|
| 382 | } |
---|
| 383 | |
---|
[1499] | 384 | #undef _ret |
---|
| 385 | |
---|
| 386 | |
---|
[1022] | 387 | int sources_add_roar (char * driver, char * device, char * container, char * options, int primary) { |
---|
| 388 | int stream; |
---|
| 389 | int fh; |
---|
| 390 | int codec = ROAR_CODEC_DEFAULT; |
---|
| 391 | struct roar_stream * s; |
---|
| 392 | |
---|
| 393 | if ( options != NULL && *options ) { |
---|
[1186] | 394 | if ( !strncmp(options, "codec=", 6) ) |
---|
| 395 | options += 6; |
---|
| 396 | |
---|
[1022] | 397 | if ( (codec = roar_str2codec(options)) == -1 ) { |
---|
| 398 | return -1; |
---|
| 399 | } |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | if ( (fh = roar_simple_monitor(g_sa->rate, g_sa->channels, g_sa->bits, codec, device, "roard")) == -1 ) { |
---|
| 403 | return -1; |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | if ( (stream = streams_new()) == -1 ) { |
---|
| 407 | close(fh); |
---|
| 408 | return -1; |
---|
| 409 | } |
---|
| 410 | |
---|
| 411 | streams_get(stream, (struct roar_stream_server **)&s); |
---|
| 412 | |
---|
| 413 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 414 | |
---|
[1609] | 415 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
| 416 | streams_delete(stream); |
---|
| 417 | close(fh); |
---|
| 418 | return -1; |
---|
| 419 | } |
---|
| 420 | |
---|
[1022] | 421 | s->pos_rel_id = -1; |
---|
| 422 | s->info.codec = codec; |
---|
| 423 | |
---|
| 424 | ROAR_STREAM_SERVER(s)->codec_orgi = codec; |
---|
| 425 | |
---|
| 426 | streams_set_fh(stream, fh); |
---|
| 427 | |
---|
| 428 | if ( primary ) |
---|
| 429 | streams_mark_primary(stream); |
---|
| 430 | |
---|
[1031] | 431 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
[1022] | 432 | client_stream_add(g_source_client, stream); |
---|
| 433 | |
---|
| 434 | return 0; |
---|
| 435 | } |
---|
| 436 | |
---|
[0] | 437 | //ll |
---|