[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 | |
---|
[2269] | 27 | struct roar_source g_sources[] = { |
---|
| 28 | {"raw", "Old raw source", "/some/file", SRC_FLAG_NONE, ROAR_SUBSYS_WAVEFORM, sources_add_raw, NULL}, |
---|
| 29 | {"wav", "Old RIFF/WAVE source", "/some/file.wav", SRC_FLAG_NONE, ROAR_SUBSYS_WAVEFORM, sources_add_wav, NULL}, |
---|
| 30 | {"cf", "Old CF source", "/some/file.ext", SRC_FLAG_NONE, ROAR_SUBSYS_WAVEFORM, sources_add_cf, NULL}, |
---|
| 31 | {"roar", "Old simple RoarAudio source", "some.host", SRC_FLAG_NONE, ROAR_SUBSYS_WAVEFORM, sources_add_roar, NULL}, |
---|
| 32 | {NULL, NULL, NULL, SRC_FLAG_NONE, 0, NULL, NULL} // EOL |
---|
| 33 | }; |
---|
| 34 | |
---|
[0] | 35 | int sources_init (void) { |
---|
| 36 | g_source_client = -1; |
---|
| 37 | return 0; |
---|
| 38 | } |
---|
| 39 | |
---|
[64] | 40 | int sources_set_client (int client) { |
---|
| 41 | if ( client >= 0 ) { |
---|
| 42 | g_source_client = client; |
---|
| 43 | return 0; |
---|
| 44 | } else { |
---|
| 45 | return -1; |
---|
| 46 | } |
---|
| 47 | } |
---|
[0] | 48 | |
---|
| 49 | int sources_free (void) { |
---|
| 50 | return 0; |
---|
| 51 | } |
---|
| 52 | |
---|
[65] | 53 | int sources_add (char * driver, char * device, char * container, char * options, int primary) { |
---|
[1499] | 54 | if (0) { |
---|
| 55 | #ifdef ROAR_HAVE_IO_POSIX |
---|
| 56 | } else if ( strcmp(driver, "raw") == 0 ) { |
---|
[65] | 57 | return sources_add_raw(driver, device, container, options, primary); |
---|
[67] | 58 | } else if ( strcmp(driver, "wav") == 0 ) { |
---|
| 59 | return sources_add_wav(driver, device, container, options, primary); |
---|
[1499] | 60 | #endif |
---|
[542] | 61 | } else if ( strcmp(driver, "cf") == 0 ) { |
---|
| 62 | return sources_add_cf(driver, device, container, options, primary); |
---|
[1022] | 63 | } else if ( strcmp(driver, "roar") == 0 ) { |
---|
| 64 | return sources_add_roar(driver, device, container, options, primary); |
---|
[67] | 65 | } |
---|
[65] | 66 | |
---|
[0] | 67 | return -1; |
---|
| 68 | } |
---|
| 69 | |
---|
[1499] | 70 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[65] | 71 | int sources_add_raw (char * driver, char * device, char * container, char * options, int primary) { |
---|
| 72 | int stream; |
---|
| 73 | int fh; |
---|
| 74 | struct roar_stream * s; |
---|
| 75 | |
---|
[566] | 76 | ROAR_WARN("sources_add_raw(*): The raw source is obsolete, use source 'cf' (default)!"); |
---|
| 77 | |
---|
[65] | 78 | if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) { |
---|
| 79 | return -1; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | if ( (stream = streams_new()) == -1 ) { |
---|
| 83 | close(fh); |
---|
| 84 | return -1; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | streams_get(stream, (struct roar_stream_server **)&s); |
---|
| 88 | |
---|
| 89 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 90 | |
---|
[1609] | 91 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
| 92 | streams_delete(stream); |
---|
| 93 | close(fh); |
---|
| 94 | return -1; |
---|
| 95 | } |
---|
| 96 | |
---|
[65] | 97 | s->pos_rel_id = -1; |
---|
| 98 | |
---|
| 99 | streams_set_fh(stream, fh); |
---|
| 100 | |
---|
[1031] | 101 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
[65] | 102 | client_stream_add(g_source_client, stream); |
---|
| 103 | |
---|
| 104 | return 0; |
---|
| 105 | } |
---|
[1499] | 106 | #endif |
---|
[65] | 107 | |
---|
[1499] | 108 | #ifdef ROAR_HAVE_IO_POSIX |
---|
[67] | 109 | int sources_add_wav (char * driver, char * device, char * container, char * options, int primary) { |
---|
| 110 | int stream; |
---|
| 111 | int fh; |
---|
| 112 | char buf[44]; |
---|
| 113 | struct roar_stream * s; |
---|
| 114 | |
---|
[566] | 115 | ROAR_WARN("sources_add_raw(*): The wav(e) source is obsolete, use source 'cf' (default)!"); |
---|
| 116 | |
---|
[67] | 117 | if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) { |
---|
| 118 | return -1; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | if (read(fh, buf, 44) != 44) { |
---|
| 122 | close(fh); |
---|
| 123 | return -1; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | if ( (stream = streams_new()) == -1 ) { |
---|
| 127 | close(fh); |
---|
| 128 | return -1; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | streams_get(stream, (struct roar_stream_server **)&s); |
---|
| 132 | |
---|
| 133 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 134 | |
---|
[1499] | 135 | memcpy(&(s->info.rate ), buf+24, 4); |
---|
| 136 | memcpy(&(s->info.channels), buf+22, 2); |
---|
| 137 | memcpy(&(s->info.bits ), buf+34, 2); |
---|
[67] | 138 | |
---|
[1609] | 139 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
| 140 | streams_delete(stream); |
---|
| 141 | close(fh); |
---|
| 142 | return -1; |
---|
| 143 | } |
---|
[67] | 144 | s->pos_rel_id = -1; |
---|
| 145 | |
---|
| 146 | streams_set_fh(stream, fh); |
---|
| 147 | |
---|
[1031] | 148 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
[67] | 149 | client_stream_add(g_source_client, stream); |
---|
| 150 | |
---|
| 151 | return 0; |
---|
| 152 | } |
---|
[1499] | 153 | #endif |
---|
| 154 | |
---|
[1614] | 155 | #define _ret(x) streams_delete(stream); return (x) |
---|
[542] | 156 | |
---|
| 157 | int sources_add_cf (char * driver, char * device, char * container, char * options, int primary) { |
---|
[555] | 158 | int stream; |
---|
| 159 | int codec; |
---|
| 160 | int len; |
---|
| 161 | char buf[64]; |
---|
[1499] | 162 | struct roar_stream * s; |
---|
| 163 | struct roar_vio_calls * vio; |
---|
[1614] | 164 | struct roar_vio_defaults def; |
---|
| 165 | |
---|
| 166 | if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, O_RDONLY, 0644) == -1 ) |
---|
| 167 | return -1; |
---|
[555] | 168 | |
---|
[542] | 169 | if ( (stream = streams_new()) == -1 ) { |
---|
| 170 | return -1; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | streams_get(stream, (struct roar_stream_server **)&s); |
---|
| 174 | |
---|
| 175 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 176 | |
---|
[1609] | 177 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
| 178 | streams_delete(stream); |
---|
| 179 | return -1; |
---|
| 180 | } |
---|
| 181 | |
---|
[542] | 182 | s->pos_rel_id = -1; |
---|
[1499] | 183 | |
---|
| 184 | /* |
---|
| 185 | if ( (fh = open(device, O_RDONLY, 0644)) == -1 ) { |
---|
| 186 | return -1; |
---|
| 187 | } |
---|
| 188 | */ |
---|
| 189 | |
---|
| 190 | vio = &(ROAR_STREAM_SERVER(s)->vio); |
---|
| 191 | |
---|
[1614] | 192 | //if ( roar_vio_open_file(vio, device, O_RDONLY, 0644) == -1 ) { |
---|
| 193 | if ( roar_vio_open_dstr(vio, device, &def, 1) == -1 ) { |
---|
[1499] | 194 | _ret(-1); |
---|
| 195 | } |
---|
| 196 | |
---|
[1614] | 197 | ROAR_DBG("sources_add_cf(*) = ?"); |
---|
| 198 | |
---|
[1499] | 199 | // TODO: finy out a better way of doing auto detetion without need for seek! |
---|
| 200 | if ( options == NULL ) { |
---|
| 201 | if ( (len = roar_vio_read(vio, buf, 64)) < 1 ) { |
---|
| 202 | _ret(-1); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | if ( roar_vio_lseek(vio, -len, SEEK_CUR) == (off_t)-1 ) { |
---|
| 206 | _ret(-1); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | if ( (codec = roar_file_codecdetect(buf, len)) == -1 ) { |
---|
| 210 | _ret(-1); |
---|
| 211 | } |
---|
| 212 | } else { |
---|
| 213 | if ( !strncmp(options, "codec=", 6) ) |
---|
| 214 | options += 6; |
---|
| 215 | |
---|
| 216 | if ( (codec = roar_str2codec(options)) == -1 ) { |
---|
| 217 | _ret(-1); |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | |
---|
[542] | 221 | s->info.codec = codec; |
---|
| 222 | |
---|
[566] | 223 | ROAR_STREAM_SERVER(s)->codec_orgi = codec; |
---|
| 224 | |
---|
[1614] | 225 | ROAR_DBG("sources_add_cf(*) = ?"); |
---|
[1504] | 226 | streams_set_fh(stream, -2); |
---|
[1614] | 227 | ROAR_DBG("sources_add_cf(*) = ?"); |
---|
[644] | 228 | streams_set_socktype(stream, ROAR_SOCKET_TYPE_FILE); |
---|
| 229 | |
---|
| 230 | if ( primary ) |
---|
| 231 | streams_mark_primary(stream); |
---|
[542] | 232 | |
---|
[1031] | 233 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
[542] | 234 | client_stream_add(g_source_client, stream); |
---|
| 235 | |
---|
| 236 | return 0; |
---|
| 237 | } |
---|
| 238 | |
---|
[1499] | 239 | #undef _ret |
---|
| 240 | |
---|
| 241 | |
---|
[1022] | 242 | int sources_add_roar (char * driver, char * device, char * container, char * options, int primary) { |
---|
| 243 | int stream; |
---|
| 244 | int fh; |
---|
| 245 | int codec = ROAR_CODEC_DEFAULT; |
---|
| 246 | struct roar_stream * s; |
---|
| 247 | |
---|
| 248 | if ( options != NULL && *options ) { |
---|
[1186] | 249 | if ( !strncmp(options, "codec=", 6) ) |
---|
| 250 | options += 6; |
---|
| 251 | |
---|
[1022] | 252 | if ( (codec = roar_str2codec(options)) == -1 ) { |
---|
| 253 | return -1; |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | if ( (fh = roar_simple_monitor(g_sa->rate, g_sa->channels, g_sa->bits, codec, device, "roard")) == -1 ) { |
---|
| 258 | return -1; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | if ( (stream = streams_new()) == -1 ) { |
---|
| 262 | close(fh); |
---|
| 263 | return -1; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | streams_get(stream, (struct roar_stream_server **)&s); |
---|
| 267 | |
---|
| 268 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
| 269 | |
---|
[1609] | 270 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
| 271 | streams_delete(stream); |
---|
| 272 | close(fh); |
---|
| 273 | return -1; |
---|
| 274 | } |
---|
| 275 | |
---|
[1022] | 276 | s->pos_rel_id = -1; |
---|
| 277 | s->info.codec = codec; |
---|
| 278 | |
---|
| 279 | ROAR_STREAM_SERVER(s)->codec_orgi = codec; |
---|
| 280 | |
---|
| 281 | streams_set_fh(stream, fh); |
---|
| 282 | |
---|
| 283 | if ( primary ) |
---|
| 284 | streams_mark_primary(stream); |
---|
| 285 | |
---|
[1031] | 286 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
[1022] | 287 | client_stream_add(g_source_client, stream); |
---|
| 288 | |
---|
| 289 | return 0; |
---|
| 290 | } |
---|
| 291 | |
---|
[0] | 292 | //ll |
---|