[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 ) { |
---|
| 92 | // TODO: add code to open driver here... |
---|
| 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 | |
---|
[1499] | 106 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[65] | 107 | int sources_add_raw (char * driver, char * device, char * container, char * options, int primary) { |
---|
| 108 | int stream; |
---|
| 109 | int fh; |
---|
| 110 | struct roar_stream * s; |
---|
| 111 | |
---|
[566] | 112 | ROAR_WARN("sources_add_raw(*): The raw source is obsolete, use source 'cf' (default)!"); |
---|
| 113 | |
---|
[65] | 114 | if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) { |
---|
| 115 | return -1; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | if ( (stream = streams_new()) == -1 ) { |
---|
| 119 | close(fh); |
---|
| 120 | return -1; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | streams_get(stream, (struct roar_stream_server **)&s); |
---|
| 124 | |
---|
| 125 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 126 | |
---|
[1609] | 127 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
| 128 | streams_delete(stream); |
---|
| 129 | close(fh); |
---|
| 130 | return -1; |
---|
| 131 | } |
---|
| 132 | |
---|
[65] | 133 | s->pos_rel_id = -1; |
---|
| 134 | |
---|
| 135 | streams_set_fh(stream, fh); |
---|
| 136 | |
---|
[1031] | 137 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
[65] | 138 | client_stream_add(g_source_client, stream); |
---|
| 139 | |
---|
| 140 | return 0; |
---|
| 141 | } |
---|
[1499] | 142 | #endif |
---|
[65] | 143 | |
---|
[1499] | 144 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[67] | 145 | int sources_add_wav (char * driver, char * device, char * container, char * options, int primary) { |
---|
| 146 | int stream; |
---|
| 147 | int fh; |
---|
| 148 | char buf[44]; |
---|
| 149 | struct roar_stream * s; |
---|
| 150 | |
---|
[566] | 151 | ROAR_WARN("sources_add_raw(*): The wav(e) source is obsolete, use source 'cf' (default)!"); |
---|
| 152 | |
---|
[67] | 153 | if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) { |
---|
| 154 | return -1; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | if (read(fh, buf, 44) != 44) { |
---|
| 158 | close(fh); |
---|
| 159 | return -1; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | if ( (stream = streams_new()) == -1 ) { |
---|
| 163 | close(fh); |
---|
| 164 | return -1; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | streams_get(stream, (struct roar_stream_server **)&s); |
---|
| 168 | |
---|
| 169 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 170 | |
---|
[1499] | 171 | memcpy(&(s->info.rate ), buf+24, 4); |
---|
| 172 | memcpy(&(s->info.channels), buf+22, 2); |
---|
| 173 | memcpy(&(s->info.bits ), buf+34, 2); |
---|
[67] | 174 | |
---|
[1609] | 175 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
| 176 | streams_delete(stream); |
---|
| 177 | close(fh); |
---|
| 178 | return -1; |
---|
| 179 | } |
---|
[67] | 180 | s->pos_rel_id = -1; |
---|
| 181 | |
---|
| 182 | streams_set_fh(stream, fh); |
---|
| 183 | |
---|
[1031] | 184 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
[67] | 185 | client_stream_add(g_source_client, stream); |
---|
| 186 | |
---|
| 187 | return 0; |
---|
| 188 | } |
---|
[1499] | 189 | #endif |
---|
| 190 | |
---|
[1614] | 191 | #define _ret(x) streams_delete(stream); return (x) |
---|
[542] | 192 | |
---|
| 193 | int sources_add_cf (char * driver, char * device, char * container, char * options, int primary) { |
---|
[555] | 194 | int stream; |
---|
| 195 | int codec; |
---|
| 196 | int len; |
---|
| 197 | char buf[64]; |
---|
[1499] | 198 | struct roar_stream * s; |
---|
| 199 | struct roar_vio_calls * vio; |
---|
[1614] | 200 | struct roar_vio_defaults def; |
---|
| 201 | |
---|
| 202 | if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, O_RDONLY, 0644) == -1 ) |
---|
| 203 | return -1; |
---|
[555] | 204 | |
---|
[542] | 205 | if ( (stream = streams_new()) == -1 ) { |
---|
| 206 | return -1; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | streams_get(stream, (struct roar_stream_server **)&s); |
---|
| 210 | |
---|
| 211 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 212 | |
---|
[1609] | 213 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
| 214 | streams_delete(stream); |
---|
| 215 | return -1; |
---|
| 216 | } |
---|
| 217 | |
---|
[542] | 218 | s->pos_rel_id = -1; |
---|
[1499] | 219 | |
---|
| 220 | /* |
---|
| 221 | if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) { |
---|
| 222 | return -1; |
---|
| 223 | } |
---|
| 224 | */ |
---|
| 225 | |
---|
| 226 | vio = &(ROAR_STREAM_SERVER(s)->vio); |
---|
| 227 | |
---|
[1614] | 228 | //if ( roar_vio_open_file(vio, device, O_RDONLY, 0644) == -1 ) { |
---|
| 229 | if ( roar_vio_open_dstr(vio, device, &def, 1) == -1 ) { |
---|
[1499] | 230 | _ret(-1); |
---|
| 231 | } |
---|
| 232 | |
---|
[1614] | 233 | ROAR_DBG("sources_add_cf(*) = ?"); |
---|
| 234 | |
---|
[1499] | 235 | // TODO: finy out a better way of doing auto detetion without need for seek! |
---|
| 236 | if ( options == NULL ) { |
---|
| 237 | if ( (len = roar_vio_read(vio, buf, 64)) < 1 ) { |
---|
| 238 | _ret(-1); |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | if ( roar_vio_lseek(vio, -len, SEEK_CUR) == (off_t)-1 ) { |
---|
| 242 | _ret(-1); |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | if ( (codec = roar_file_codecdetect(buf, len)) == -1 ) { |
---|
| 246 | _ret(-1); |
---|
| 247 | } |
---|
| 248 | } else { |
---|
| 249 | if ( !strncmp(options, "codec=", 6) ) |
---|
| 250 | options += 6; |
---|
| 251 | |
---|
| 252 | if ( (codec = roar_str2codec(options)) == -1 ) { |
---|
| 253 | _ret(-1); |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | |
---|
[542] | 257 | s->info.codec = codec; |
---|
| 258 | |
---|
[566] | 259 | ROAR_STREAM_SERVER(s)->codec_orgi = codec; |
---|
| 260 | |
---|
[1614] | 261 | ROAR_DBG("sources_add_cf(*) = ?"); |
---|
[1504] | 262 | streams_set_fh(stream, -2); |
---|
[1614] | 263 | ROAR_DBG("sources_add_cf(*) = ?"); |
---|
[644] | 264 | streams_set_socktype(stream, ROAR_SOCKET_TYPE_FILE); |
---|
| 265 | |
---|
| 266 | if ( primary ) |
---|
| 267 | streams_mark_primary(stream); |
---|
[542] | 268 | |
---|
[1031] | 269 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
[542] | 270 | client_stream_add(g_source_client, stream); |
---|
| 271 | |
---|
| 272 | return 0; |
---|
| 273 | } |
---|
| 274 | |
---|
[1499] | 275 | #undef _ret |
---|
| 276 | |
---|
| 277 | |
---|
[1022] | 278 | int sources_add_roar (char * driver, char * device, char * container, char * options, int primary) { |
---|
| 279 | int stream; |
---|
| 280 | int fh; |
---|
| 281 | int codec = ROAR_CODEC_DEFAULT; |
---|
| 282 | struct roar_stream * s; |
---|
| 283 | |
---|
| 284 | if ( options != NULL && *options ) { |
---|
[1186] | 285 | if ( !strncmp(options, "codec=", 6) ) |
---|
| 286 | options += 6; |
---|
| 287 | |
---|
[1022] | 288 | if ( (codec = roar_str2codec(options)) == -1 ) { |
---|
| 289 | return -1; |
---|
| 290 | } |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | if ( (fh = roar_simple_monitor(g_sa->rate, g_sa->channels, g_sa->bits, codec, device, "roard")) == -1 ) { |
---|
| 294 | return -1; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | if ( (stream = streams_new()) == -1 ) { |
---|
| 298 | close(fh); |
---|
| 299 | return -1; |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | streams_get(stream, (struct roar_stream_server **)&s); |
---|
| 303 | |
---|
| 304 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 305 | |
---|
[1609] | 306 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
| 307 | streams_delete(stream); |
---|
| 308 | close(fh); |
---|
| 309 | return -1; |
---|
| 310 | } |
---|
| 311 | |
---|
[1022] | 312 | s->pos_rel_id = -1; |
---|
| 313 | s->info.codec = codec; |
---|
| 314 | |
---|
| 315 | ROAR_STREAM_SERVER(s)->codec_orgi = codec; |
---|
| 316 | |
---|
| 317 | streams_set_fh(stream, fh); |
---|
| 318 | |
---|
| 319 | if ( primary ) |
---|
| 320 | streams_mark_primary(stream); |
---|
| 321 | |
---|
[1031] | 322 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
[1022] | 323 | client_stream_add(g_source_client, stream); |
---|
| 324 | |
---|
| 325 | return 0; |
---|
| 326 | } |
---|
| 327 | |
---|
[0] | 328 | //ll |
---|