1 | //sources.c: |
---|
2 | |
---|
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 | |
---|
25 | #include "roard.h" |
---|
26 | |
---|
27 | struct roar_source g_source[] = { |
---|
28 | #ifdef ROAR_HAVE_IO_POSIX |
---|
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}, |
---|
31 | #endif |
---|
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 | |
---|
37 | int sources_init (void) { |
---|
38 | g_source_client = -1; |
---|
39 | return 0; |
---|
40 | } |
---|
41 | |
---|
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 | |
---|
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 | } |
---|
81 | |
---|
82 | int sources_free (void) { |
---|
83 | return 0; |
---|
84 | } |
---|
85 | |
---|
86 | int sources_add (char * driver, char * device, char * container, char * options, int primary) { |
---|
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 | return sources_add_new(&(g_source[i]), driver, device, container, options, primary); |
---|
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 | } |
---|
100 | } |
---|
101 | |
---|
102 | ROAR_ERR("sources_add(driver='%s', ...): Source not found", driver); |
---|
103 | return -1; |
---|
104 | } |
---|
105 | |
---|
106 | int sources_add_new (struct roar_source * source, |
---|
107 | char * driver, char * device, |
---|
108 | char * container, |
---|
109 | char * options, int primary) { |
---|
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; |
---|
213 | } |
---|
214 | |
---|
215 | #ifdef ROAR_HAVE_IO_POSIX |
---|
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 | |
---|
221 | ROAR_WARN("sources_add_raw(*): The raw source is obsolete, use source 'cf' (default)!"); |
---|
222 | |
---|
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 | |
---|
236 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
237 | streams_delete(stream); |
---|
238 | close(fh); |
---|
239 | return -1; |
---|
240 | } |
---|
241 | |
---|
242 | s->pos_rel_id = -1; |
---|
243 | |
---|
244 | streams_set_fh(stream, fh); |
---|
245 | |
---|
246 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
247 | client_stream_add(g_source_client, stream); |
---|
248 | |
---|
249 | return 0; |
---|
250 | } |
---|
251 | #endif |
---|
252 | |
---|
253 | #ifdef ROAR_HAVE_IO_POSIX |
---|
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 | |
---|
260 | ROAR_WARN("sources_add_raw(*): The wav(e) source is obsolete, use source 'cf' (default)!"); |
---|
261 | |
---|
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 | |
---|
280 | memcpy(&(s->info.rate ), buf+24, 4); |
---|
281 | memcpy(&(s->info.channels), buf+22, 2); |
---|
282 | memcpy(&(s->info.bits ), buf+34, 2); |
---|
283 | |
---|
284 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
285 | streams_delete(stream); |
---|
286 | close(fh); |
---|
287 | return -1; |
---|
288 | } |
---|
289 | s->pos_rel_id = -1; |
---|
290 | |
---|
291 | streams_set_fh(stream, fh); |
---|
292 | |
---|
293 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
294 | client_stream_add(g_source_client, stream); |
---|
295 | |
---|
296 | return 0; |
---|
297 | } |
---|
298 | #endif |
---|
299 | |
---|
300 | #define _ret(x) streams_delete(stream); return (x) |
---|
301 | |
---|
302 | int sources_add_cf (char * driver, char * device, char * container, char * options, int primary) { |
---|
303 | int stream; |
---|
304 | int codec; |
---|
305 | int len; |
---|
306 | char buf[64]; |
---|
307 | struct roar_stream * s; |
---|
308 | struct roar_vio_calls * vio; |
---|
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; |
---|
313 | |
---|
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 | |
---|
322 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
323 | streams_delete(stream); |
---|
324 | return -1; |
---|
325 | } |
---|
326 | |
---|
327 | s->pos_rel_id = -1; |
---|
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 | |
---|
337 | //if ( roar_vio_open_file(vio, device, O_RDONLY, 0644) == -1 ) { |
---|
338 | if ( roar_vio_open_dstr(vio, device, &def, 1) == -1 ) { |
---|
339 | _ret(-1); |
---|
340 | } |
---|
341 | |
---|
342 | ROAR_DBG("sources_add_cf(*) = ?"); |
---|
343 | |
---|
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 | |
---|
366 | s->info.codec = codec; |
---|
367 | |
---|
368 | ROAR_STREAM_SERVER(s)->codec_orgi = codec; |
---|
369 | |
---|
370 | ROAR_DBG("sources_add_cf(*) = ?"); |
---|
371 | streams_set_fh(stream, -2); |
---|
372 | ROAR_DBG("sources_add_cf(*) = ?"); |
---|
373 | streams_set_socktype(stream, ROAR_SOCKET_TYPE_FILE); |
---|
374 | |
---|
375 | if ( primary ) |
---|
376 | streams_mark_primary(stream); |
---|
377 | |
---|
378 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
379 | client_stream_add(g_source_client, stream); |
---|
380 | |
---|
381 | return 0; |
---|
382 | } |
---|
383 | |
---|
384 | #undef _ret |
---|
385 | |
---|
386 | |
---|
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 ) { |
---|
394 | if ( !strncmp(options, "codec=", 6) ) |
---|
395 | options += 6; |
---|
396 | |
---|
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 | |
---|
415 | if ( streams_set_dir(stream, ROAR_DIR_PLAY, 1) == -1 ) { |
---|
416 | streams_delete(stream); |
---|
417 | close(fh); |
---|
418 | return -1; |
---|
419 | } |
---|
420 | |
---|
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 | |
---|
431 | streams_set_flag(stream, ROAR_FLAG_SOURCE); |
---|
432 | client_stream_add(g_source_client, stream); |
---|
433 | |
---|
434 | return 0; |
---|
435 | } |
---|
436 | |
---|
437 | //ll |
---|