1 | //streams.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 | int streams_thru_num = 0; |
---|
28 | int streams_recsource_id = -1; |
---|
29 | |
---|
30 | int streams_init (void) { |
---|
31 | int i; |
---|
32 | |
---|
33 | for (i = 0; i < ROAR_STREAMS_MAX; i++) |
---|
34 | g_streams[i] = NULL; |
---|
35 | |
---|
36 | return 0; |
---|
37 | } |
---|
38 | |
---|
39 | int streams_free (void) { |
---|
40 | int i; |
---|
41 | |
---|
42 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
43 | if ( g_streams[i] != NULL ) { |
---|
44 | streams_delete(i); |
---|
45 | } |
---|
46 | } |
---|
47 | |
---|
48 | return 0; |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | int streams_new (void) { |
---|
53 | int i, j; |
---|
54 | struct roar_stream * n = NULL; |
---|
55 | struct roar_stream_server * s = NULL; |
---|
56 | |
---|
57 | #ifdef ROAR_SUPPORT_LISTEN |
---|
58 | if ( g_terminate && !g_no_listen ) // don't accept new streams in case of termination state |
---|
59 | return -1; |
---|
60 | #else |
---|
61 | if ( g_terminate ) // don't accept new streams in case of termination state |
---|
62 | return -1; |
---|
63 | #endif |
---|
64 | |
---|
65 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
66 | if ( g_streams[i] == NULL ) { |
---|
67 | s = ROAR_STREAM_SERVER(n = ROAR_STREAM(malloc(sizeof(struct roar_stream_server)))); |
---|
68 | if ( n == NULL ) { |
---|
69 | ROAR_ERR("streams_new(void): can not allocate memory for new stream: %s", strerror(errno)); |
---|
70 | ROAR_DBG("streams_new(void) = -1"); |
---|
71 | return -1; |
---|
72 | } |
---|
73 | |
---|
74 | n->id = i; |
---|
75 | n->fh = -1; |
---|
76 | // n->pos_rel_id = i; |
---|
77 | /* |
---|
78 | n->database = NULL; |
---|
79 | n->dataoff = NULL; |
---|
80 | n->datalen = 0; |
---|
81 | n->offset = 0; |
---|
82 | */ |
---|
83 | n->pos = 0; |
---|
84 | |
---|
85 | s->name = NULL; |
---|
86 | |
---|
87 | s->client = -1; |
---|
88 | s->socktype = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
89 | s->buffer = NULL; |
---|
90 | s->need_extra = 0; |
---|
91 | s->output = NULL; |
---|
92 | s->is_new = 1; |
---|
93 | s->codecfilter = -1; |
---|
94 | s->pre_underruns = 0; |
---|
95 | s->post_underruns = 0; |
---|
96 | s->delay = 0; |
---|
97 | s->codec_orgi = -1; |
---|
98 | s->primary = 0; |
---|
99 | s->ready = 0; |
---|
100 | s->outputbuffer = NULL; |
---|
101 | |
---|
102 | s->mixer.scale = 65535; |
---|
103 | s->mixer.rpg_mul = 1; |
---|
104 | s->mixer.rpg_div = 1; |
---|
105 | for (j = 0; j < ROAR_MAX_CHANNELS; j++) |
---|
106 | s->mixer.mixer[j] = 65535; |
---|
107 | |
---|
108 | #ifdef ROAR_SUPPORT_META |
---|
109 | for (j = 0; j < ROAR_META_MAX_PER_STREAM; j++) { |
---|
110 | s->meta[j].type = ROAR_META_TYPE_NONE; |
---|
111 | s->meta[j].key[0] = 0; |
---|
112 | s->meta[j].value = NULL; |
---|
113 | } |
---|
114 | #endif |
---|
115 | |
---|
116 | roar_vio_init_calls(&(s->vio)); |
---|
117 | s->driver_id = -1; |
---|
118 | s->flags = ROAR_FLAG_NONE; |
---|
119 | |
---|
120 | //roardsp_fchain_init(&(s->fc)); |
---|
121 | |
---|
122 | g_streams[i] = s; |
---|
123 | ROAR_DBG("streams_new(void): n->id=%i", n->id); |
---|
124 | ROAR_DBG("streams_new(void) = %i", i); |
---|
125 | return i; |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | return -1; |
---|
130 | } |
---|
131 | |
---|
132 | int streams_delete (int id) { |
---|
133 | struct roar_stream_server * s; |
---|
134 | int prim; |
---|
135 | int no_vio_close = 0; |
---|
136 | int i; |
---|
137 | |
---|
138 | if ( (s = g_streams[id]) == NULL ) |
---|
139 | return 0; |
---|
140 | |
---|
141 | ROAR_DBG("streams_delete(id=%i) = ?", id); |
---|
142 | ROAR_DBG("streams_delete(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(s)->id); |
---|
143 | |
---|
144 | if ( streams_get_flag(id, ROAR_FLAG_RECSOURCE) == 1 ) |
---|
145 | streams_reset_flag(id, ROAR_FLAG_RECSOURCE); |
---|
146 | |
---|
147 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
148 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) { |
---|
149 | switch (ROAR_STREAM(g_streams[i])->dir) { |
---|
150 | case ROAR_DIR_THRU: |
---|
151 | case ROAR_DIR_RAW_IN: |
---|
152 | if ( i != id ) |
---|
153 | streams_delete(i); |
---|
154 | break; |
---|
155 | default: |
---|
156 | if ( streams_get_flag(i, ROAR_FLAG_VIRTUAL) == 1 ) { |
---|
157 | if ( i != id ) |
---|
158 | streams_delete(i); |
---|
159 | } else { |
---|
160 | ROAR_STREAM(g_streams[i])->pos_rel_id = -1; |
---|
161 | } |
---|
162 | } |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | if ( ROAR_STREAM(s)->dir == ROAR_DIR_THRU ) |
---|
167 | streams_thru_num--; |
---|
168 | |
---|
169 | #ifdef ROAR_SUPPORT_META |
---|
170 | // delete meta data form other meta streams if needed |
---|
171 | if ( streams_get_flag(id, ROAR_FLAG_META) == 1 ) { |
---|
172 | ROAR_DBG("streams_delete(id=%i): deleting meta stream!", id); |
---|
173 | stream_meta_clear(id); |
---|
174 | stream_meta_finalize(id); |
---|
175 | } |
---|
176 | #endif |
---|
177 | |
---|
178 | if ( s->codecfilter != -1 ) { |
---|
179 | codecfilter_close(s->codecfilter_inst, s->codecfilter); |
---|
180 | s->codecfilter_inst = NULL; |
---|
181 | s->codecfilter = -1; |
---|
182 | } |
---|
183 | |
---|
184 | if ( s->driver_id != -1 ) { |
---|
185 | driver_closevio(&(s->vio), s->driver_id); |
---|
186 | roar_vio_init_calls(&(s->vio)); |
---|
187 | s->driver_id = -1; |
---|
188 | no_vio_close = 1; |
---|
189 | } |
---|
190 | |
---|
191 | //roardsp_fchain_uninit(&(s->fc)); |
---|
192 | |
---|
193 | if ( s->client != -1 ) { |
---|
194 | ROAR_DBG("streams_delete(id=%i): Stream is owned by client %i", id, g_streams[id]->client); |
---|
195 | client_stream_delete(s->client, id); |
---|
196 | } |
---|
197 | |
---|
198 | stream_outputbuffer_destroy(id); |
---|
199 | |
---|
200 | if ( s->buffer != NULL ) |
---|
201 | roar_buffer_free(s->buffer); |
---|
202 | |
---|
203 | if ( s->output != NULL ) |
---|
204 | free(s->output); |
---|
205 | |
---|
206 | /* |
---|
207 | if ( ROAR_STREAM(s)->fh != -1 ) |
---|
208 | close(ROAR_STREAM(s)->fh); |
---|
209 | */ |
---|
210 | |
---|
211 | if ( !no_vio_close ) |
---|
212 | roar_vio_close(&(s->vio)); |
---|
213 | |
---|
214 | prim = s->primary; |
---|
215 | |
---|
216 | if ( s->name != NULL ) |
---|
217 | free(s->name); |
---|
218 | |
---|
219 | free(s); |
---|
220 | |
---|
221 | g_streams[id] = NULL; |
---|
222 | |
---|
223 | if ( prim ) { |
---|
224 | alive = 0; |
---|
225 | clean_quit(); |
---|
226 | } |
---|
227 | |
---|
228 | ROAR_DBG("streams_delete(id=%i) = 0", id); |
---|
229 | return 0; |
---|
230 | } |
---|
231 | |
---|
232 | int streams_set_client (int id, int client) { |
---|
233 | if ( g_streams[id] == NULL ) |
---|
234 | return -1; |
---|
235 | |
---|
236 | ROAR_DBG("streams_set_client(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id); |
---|
237 | g_streams[id]->client = client; |
---|
238 | |
---|
239 | return 0; |
---|
240 | } |
---|
241 | |
---|
242 | int streams_get_client (int id) { |
---|
243 | if ( g_streams[id] == NULL ) |
---|
244 | return -1; |
---|
245 | |
---|
246 | return g_streams[id]->client; |
---|
247 | } |
---|
248 | |
---|
249 | int streams_set_dir (int id, int dir, int defaults) { |
---|
250 | struct roar_stream_server * ss; |
---|
251 | |
---|
252 | if ( (ss = g_streams[id]) == NULL ) |
---|
253 | return -1; |
---|
254 | |
---|
255 | ROAR_STREAM(ss)->dir = dir; |
---|
256 | |
---|
257 | if ( dir == ROAR_DIR_THRU ) |
---|
258 | streams_thru_num++; |
---|
259 | |
---|
260 | if ( defaults ) { |
---|
261 | if ( dir <= 0 || dir >= ROAR_DIR_DIRIDS ) |
---|
262 | return -1; |
---|
263 | |
---|
264 | ROAR_DBG("streams_set_dir(*): g_config->streams[dir=%i].flags = 0x%.4x", dir, g_config->streams[dir].flags); |
---|
265 | |
---|
266 | if ( streams_set_flag(id, g_config->streams[dir].flags) == -1 ) { |
---|
267 | ROAR_DBG("streams_set_dir(*) = -1 // can not set stream flags"); |
---|
268 | return -1; |
---|
269 | } |
---|
270 | |
---|
271 | ss->mixer.scale = g_config->streams[dir].mixer.scale; |
---|
272 | ss->mixer.rpg_mul = g_config->streams[dir].mixer.rpg_mul; |
---|
273 | ss->mixer.rpg_div = g_config->streams[dir].mixer.rpg_div; |
---|
274 | } |
---|
275 | |
---|
276 | ROAR_DBG("streams_set_dir(*) = 0"); |
---|
277 | return 0; |
---|
278 | } |
---|
279 | |
---|
280 | int streams_get_dir (int id) { |
---|
281 | struct roar_stream_server * ss; |
---|
282 | |
---|
283 | if ( (ss = g_streams[id]) == NULL ) |
---|
284 | return -1; |
---|
285 | |
---|
286 | return ROAR_STREAM(ss)->dir; |
---|
287 | } |
---|
288 | |
---|
289 | int streams_get_subsys (int id) { |
---|
290 | struct roar_stream_server * ss; |
---|
291 | |
---|
292 | if ( (ss = g_streams[id]) == NULL ) |
---|
293 | return -1; |
---|
294 | |
---|
295 | switch (ROAR_STREAM(ss)->dir) { |
---|
296 | case ROAR_DIR_PLAY: |
---|
297 | case ROAR_DIR_RECORD: |
---|
298 | case ROAR_DIR_MONITOR: |
---|
299 | case ROAR_DIR_FILTER: |
---|
300 | case ROAR_DIR_OUTPUT: |
---|
301 | case ROAR_DIR_BIDIR: |
---|
302 | return ROAR_SUBSYS_WAVEFORM; |
---|
303 | break; |
---|
304 | case ROAR_DIR_MIDI_IN: |
---|
305 | case ROAR_DIR_MIDI_OUT: |
---|
306 | return ROAR_SUBSYS_MIDI; |
---|
307 | break; |
---|
308 | case ROAR_DIR_LIGHT_IN: |
---|
309 | case ROAR_DIR_LIGHT_OUT: |
---|
310 | return ROAR_SUBSYS_LIGHT; |
---|
311 | break; |
---|
312 | case ROAR_DIR_RAW_IN: |
---|
313 | case ROAR_DIR_RAW_OUT: |
---|
314 | return ROAR_SUBSYS_RAW; |
---|
315 | break; |
---|
316 | case ROAR_DIR_THRU: |
---|
317 | return streams_get_subsys(ROAR_STREAM(ss)->pos_rel_id); |
---|
318 | break; |
---|
319 | } |
---|
320 | |
---|
321 | return -1; |
---|
322 | } |
---|
323 | |
---|
324 | #define _err() streams_delete(id); return -1; |
---|
325 | int streams_new_virtual (int parent) { |
---|
326 | struct roar_stream_server * parent_ss, * ss; |
---|
327 | struct roar_stream * parent_s , * s; |
---|
328 | int id = -1; |
---|
329 | int client, dir; |
---|
330 | |
---|
331 | if ( streams_get(parent, &parent_ss) == -1 ) |
---|
332 | return -1; |
---|
333 | |
---|
334 | if ( (client = streams_get_client(parent)) == -1 ) |
---|
335 | return -1; |
---|
336 | |
---|
337 | if ( (dir = streams_get_dir(parent)) == -1 ) |
---|
338 | return -1; |
---|
339 | |
---|
340 | if ( (id = streams_new()) == -1 ) { |
---|
341 | return -1; |
---|
342 | } |
---|
343 | |
---|
344 | if ( streams_set_client(id, client) == -1 ) { |
---|
345 | _err(); |
---|
346 | } |
---|
347 | |
---|
348 | if ( streams_get(id, &ss) == -1 ) { |
---|
349 | _err(); |
---|
350 | } |
---|
351 | |
---|
352 | if ( streams_set_dir(id, dir, 1) == -1 ) { |
---|
353 | _err(); |
---|
354 | } |
---|
355 | |
---|
356 | s = ROAR_STREAM( ss); |
---|
357 | parent_s = ROAR_STREAM(parent_ss); |
---|
358 | |
---|
359 | s->pos_rel_id = id; |
---|
360 | |
---|
361 | if ( streams_set_flag(id, ROAR_FLAG_VIRTUAL) == -1 ) { |
---|
362 | _err(); |
---|
363 | } |
---|
364 | |
---|
365 | return id; |
---|
366 | } |
---|
367 | #undef _err |
---|
368 | |
---|
369 | int streams_set_fh (int id, int fh) { |
---|
370 | struct roar_stream_server * ss; |
---|
371 | struct roar_stream * s; |
---|
372 | int dir; |
---|
373 | int nonblock = 1; |
---|
374 | |
---|
375 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
376 | return -1; |
---|
377 | |
---|
378 | ROAR_DBG("streams_set_fh(id=%i): g_streams[id]->id=%i", id, s->id); |
---|
379 | |
---|
380 | s->fh = fh; |
---|
381 | |
---|
382 | ROAR_DBG("streams_set_fh(id=%i, fh=%i): driverID=%i", id, fh, ss->driver_id); |
---|
383 | |
---|
384 | if ( ss->driver_id == -1 && fh != -2 ) |
---|
385 | roar_vio_set_fh(&(ss->vio), fh); |
---|
386 | |
---|
387 | if ( codecfilter_open(&(ss->codecfilter_inst), &(ss->codecfilter), NULL, |
---|
388 | s->info.codec, ss) == -1 ) { |
---|
389 | return streams_delete(id); // TODO: FIXME: is this correct? shoudn't we return -1 in any case here? |
---|
390 | } |
---|
391 | |
---|
392 | if ( fh == -2 ) { |
---|
393 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
394 | if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_READ_FH, &fh) == -1 ) { |
---|
395 | fh = -2; |
---|
396 | } else { |
---|
397 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
398 | if ( fh < 0 ) { |
---|
399 | fh = -2; |
---|
400 | } else { |
---|
401 | s->fh = fh; |
---|
402 | } |
---|
403 | } |
---|
404 | } |
---|
405 | |
---|
406 | if ( fh == -1 || fh == -2 ) { // yes, this is valid, indecats full vio! |
---|
407 | ss->ready = 1; |
---|
408 | return 0; |
---|
409 | } |
---|
410 | |
---|
411 | // roar_socket_recvbuf(fh, ROAR_OUTPUT_CALC_OUTBUFSIZE( &(ROAR_STREAM(g_streams[id])->info) )); // set recv buffer to minimum |
---|
412 | |
---|
413 | dir = ROAR_STREAM(ss)->dir; |
---|
414 | |
---|
415 | switch (dir) { |
---|
416 | case ROAR_DIR_MONITOR: |
---|
417 | case ROAR_DIR_RECORD: |
---|
418 | case ROAR_DIR_OUTPUT: |
---|
419 | case ROAR_DIR_MIDI_OUT: |
---|
420 | case ROAR_DIR_LIGHT_OUT: |
---|
421 | case ROAR_DIR_RAW_OUT: |
---|
422 | ROAR_SHUTDOWN(fh, SHUT_RD); |
---|
423 | break; |
---|
424 | } |
---|
425 | |
---|
426 | if ( dir >= ROAR_DIR_DIRIDS ) |
---|
427 | return -1; |
---|
428 | |
---|
429 | if ( g_config->streams[dir].flags & ROAR_FLAG_SYNC ) { |
---|
430 | switch (dir) { |
---|
431 | case ROAR_DIR_BRIDGE: |
---|
432 | case ROAR_DIR_MIDI_OUT: |
---|
433 | break; |
---|
434 | default: |
---|
435 | nonblock = 0; |
---|
436 | break; |
---|
437 | } |
---|
438 | } |
---|
439 | |
---|
440 | |
---|
441 | if ( !nonblock ) { |
---|
442 | ss->ready = 1; |
---|
443 | return 0; |
---|
444 | } else { |
---|
445 | if ( roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK) == -1 ) |
---|
446 | return -1; |
---|
447 | |
---|
448 | ss->ready = 1; |
---|
449 | return 0; |
---|
450 | } |
---|
451 | } |
---|
452 | |
---|
453 | int streams_get_fh (int id) { |
---|
454 | if ( id < 0 ) |
---|
455 | return -1; |
---|
456 | |
---|
457 | if ( g_streams[id] == NULL ) |
---|
458 | return -1; |
---|
459 | |
---|
460 | return ROAR_STREAM(g_streams[id])->fh; |
---|
461 | } |
---|
462 | |
---|
463 | int streams_get (int id, struct roar_stream_server ** stream) { |
---|
464 | if ( g_streams[id] == NULL ) |
---|
465 | return -1; |
---|
466 | |
---|
467 | *stream = g_streams[id]; |
---|
468 | |
---|
469 | return 0; |
---|
470 | } |
---|
471 | |
---|
472 | int streams_set_socktype (int id, int socktype) { |
---|
473 | if ( g_streams[id] == NULL ) |
---|
474 | return -1; |
---|
475 | |
---|
476 | g_streams[id]->socktype = socktype; |
---|
477 | |
---|
478 | return 0; |
---|
479 | } |
---|
480 | |
---|
481 | int streams_get_socktype (int id) { |
---|
482 | if ( g_streams[id] == NULL ) |
---|
483 | return -1; |
---|
484 | |
---|
485 | return g_streams[id]->socktype; |
---|
486 | } |
---|
487 | |
---|
488 | int streams_set_primary (int id, int prim) { |
---|
489 | if ( g_streams[id] == NULL ) |
---|
490 | return -1; |
---|
491 | |
---|
492 | g_streams[id]->primary = prim; |
---|
493 | |
---|
494 | return 0; |
---|
495 | } |
---|
496 | |
---|
497 | int streams_mark_primary (int id) { |
---|
498 | return streams_set_primary(id, 1); |
---|
499 | } |
---|
500 | |
---|
501 | int streams_set_sync (int id, int sync) { |
---|
502 | int fh = streams_get_fh(id); |
---|
503 | |
---|
504 | if ( fh != -1 ) { |
---|
505 | if ( roar_socket_nonblock(fh, sync ? ROAR_SOCKET_BLOCK : ROAR_SOCKET_NONBLOCK) == -1 ) |
---|
506 | return -1; |
---|
507 | |
---|
508 | #ifdef ROAR_FDATASYNC |
---|
509 | ROAR_FDATASYNC(fh); |
---|
510 | #endif |
---|
511 | |
---|
512 | return 0; |
---|
513 | } else { |
---|
514 | return roar_vio_nonblock(&(g_streams[id]->vio), sync); |
---|
515 | } |
---|
516 | } |
---|
517 | |
---|
518 | int streams_set_mmap (int id, int reset) { |
---|
519 | int use = !reset; |
---|
520 | |
---|
521 | if ( g_streams[id] == NULL ) |
---|
522 | return -1; |
---|
523 | |
---|
524 | return roar_vio_ctl(&(g_streams[id]->vio), ROAR_VIO_CTL_SET_UMMAP, &use); |
---|
525 | } |
---|
526 | |
---|
527 | int streams_set_flag (int id, int flag) { |
---|
528 | if ( g_streams[id] == NULL ) |
---|
529 | return -1; |
---|
530 | |
---|
531 | if ( flag & ROAR_FLAG_MMAP ) |
---|
532 | if ( streams_set_mmap(id, 0) == -1 ) |
---|
533 | flag -= ROAR_FLAG_MMAP; |
---|
534 | |
---|
535 | if ( flag & ROAR_FLAG_PRIMARY ) { |
---|
536 | streams_set_primary(id, 1); |
---|
537 | flag -= ROAR_FLAG_PRIMARY; |
---|
538 | } |
---|
539 | |
---|
540 | if ( flag & ROAR_FLAG_SYNC ) { |
---|
541 | switch (ROAR_STREAM(g_streams[id])->dir) { |
---|
542 | // for this stream types the flag is used in the subsystem: |
---|
543 | case ROAR_DIR_BRIDGE: |
---|
544 | case ROAR_DIR_MIDI_OUT: |
---|
545 | break; |
---|
546 | |
---|
547 | // normal behavor (vio blocking): |
---|
548 | default: |
---|
549 | // the fh is updated as soon as the fh get ready in case the default ask to set sync |
---|
550 | if ( !g_streams[id]->ready && !(g_config->streams[ROAR_STREAM(g_streams[id])->dir].flags & ROAR_FLAG_SYNC) ) { |
---|
551 | if ( streams_set_sync(id, 1) == -1 ) |
---|
552 | flag -= ROAR_FLAG_SYNC; |
---|
553 | } |
---|
554 | } |
---|
555 | } |
---|
556 | |
---|
557 | if ( flag & ROAR_FLAG_HWMIXER ) { // currently not supported -> ignored |
---|
558 | g_streams[id]->flags |= flag; |
---|
559 | if ( streams_set_mixer(id) == -1 ) { |
---|
560 | g_streams[id]->flags -= flag; |
---|
561 | return -1; |
---|
562 | } |
---|
563 | } |
---|
564 | |
---|
565 | if ( flag & ROAR_FLAG_RECSOURCE ) { |
---|
566 | if ( streams_recsource_id != -1 ) { |
---|
567 | if ( streams_reset_flag(streams_recsource_id, ROAR_FLAG_RECSOURCE) == -1 ) |
---|
568 | return -1; |
---|
569 | } |
---|
570 | |
---|
571 | streams_recsource_id = id; |
---|
572 | } |
---|
573 | |
---|
574 | g_streams[id]->flags |= flag; |
---|
575 | |
---|
576 | #ifdef ROAR_SUPPORT_META |
---|
577 | if ( flag & ROAR_FLAG_META ) |
---|
578 | stream_meta_finalize(id); |
---|
579 | #endif |
---|
580 | |
---|
581 | return 0; |
---|
582 | } |
---|
583 | |
---|
584 | int streams_reset_flag (int id, int flag) { |
---|
585 | if ( g_streams[id] == NULL ) |
---|
586 | return -1; |
---|
587 | |
---|
588 | if ( flag & ROAR_FLAG_RECSOURCE ) |
---|
589 | if ( streams_recsource_id == id ) |
---|
590 | streams_recsource_id = -1; |
---|
591 | |
---|
592 | if ( flag & ROAR_FLAG_MMAP ) |
---|
593 | if ( streams_set_mmap(id, 1) == -1 ) |
---|
594 | flag -= ROAR_FLAG_MMAP; |
---|
595 | |
---|
596 | if ( flag & ROAR_FLAG_PRIMARY ) { |
---|
597 | streams_set_primary(id, 0); |
---|
598 | flag -= ROAR_FLAG_PRIMARY; |
---|
599 | } |
---|
600 | |
---|
601 | if ( flag & ROAR_FLAG_SYNC ) { |
---|
602 | // we refuse to reset the flag on FILTER streams |
---|
603 | if ( streams_get_dir(id) == ROAR_DIR_FILTER ) { |
---|
604 | // flags -= ROAR_FLAG_SYNC; |
---|
605 | return -1; |
---|
606 | } else { |
---|
607 | streams_set_sync(id, 0); |
---|
608 | } |
---|
609 | } |
---|
610 | |
---|
611 | g_streams[id]->flags |= flag; |
---|
612 | g_streams[id]->flags -= flag; |
---|
613 | |
---|
614 | return 0; |
---|
615 | } |
---|
616 | |
---|
617 | int streams_get_flag (int id, int flag) { |
---|
618 | if ( g_streams[id] == NULL ) |
---|
619 | return -1; |
---|
620 | |
---|
621 | return g_streams[id]->flags & flag ? 1 : 0; |
---|
622 | } |
---|
623 | |
---|
624 | int streams_set_name (int id, char * name) { |
---|
625 | char * str; |
---|
626 | |
---|
627 | if ( g_streams[id] == NULL ) |
---|
628 | return -1; |
---|
629 | |
---|
630 | if ( (str = strdup(name)) == NULL ) |
---|
631 | return -1; |
---|
632 | |
---|
633 | if ( g_streams[id]->name != NULL ) |
---|
634 | free(g_streams[id]->name); |
---|
635 | |
---|
636 | g_streams[id]->name = str; |
---|
637 | |
---|
638 | return 0; |
---|
639 | } |
---|
640 | |
---|
641 | char * streams_get_name (int id) { |
---|
642 | if ( g_streams[id] == NULL ) |
---|
643 | return NULL; |
---|
644 | |
---|
645 | return g_streams[id]->name; |
---|
646 | } |
---|
647 | |
---|
648 | |
---|
649 | int streams_calc_delay (int id) { |
---|
650 | struct roar_stream_server * ss; |
---|
651 | struct roar_stream * s; |
---|
652 | register uint_least32_t d = 0; |
---|
653 | uint_least32_t t[1]; |
---|
654 | uint64_t tmp; |
---|
655 | |
---|
656 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
657 | return -1; |
---|
658 | |
---|
659 | if ( ss->codecfilter != -1 ) { |
---|
660 | if ( codecfilter_delay(ss->codecfilter_inst, ss->codecfilter, t) != -1 ) |
---|
661 | d += *t; |
---|
662 | } |
---|
663 | |
---|
664 | if ( ss->vio.ctl != NULL ) { |
---|
665 | if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_DELAY, t) != -1 ) { // *t is in byte |
---|
666 | ROAR_DBG("streams_calc_delay(id=%i): VIO delay in byte: %i", id, *t); |
---|
667 | tmp = *t; |
---|
668 | tmp *= 1000000; // musec per sec |
---|
669 | tmp /= s->info.rate * s->info.channels * (s->info.bits/8); |
---|
670 | ROAR_DBG("streams_calc_delay(id=%i): VIO delay in musec: %llu", id, tmp); |
---|
671 | |
---|
672 | d += tmp; |
---|
673 | } |
---|
674 | } |
---|
675 | |
---|
676 | ROAR_DBG("streams_calc_delay(id=%i): delay in musec: %i", id, d); |
---|
677 | |
---|
678 | ss->delay = d; |
---|
679 | |
---|
680 | return 0; |
---|
681 | } |
---|
682 | |
---|
683 | int streams_set_mixer (int id) { |
---|
684 | struct roar_stream_server * ss; |
---|
685 | struct roar_stream_server * pmss; |
---|
686 | int i; |
---|
687 | int subsys; |
---|
688 | |
---|
689 | if ( (ss = g_streams[id]) == NULL ) |
---|
690 | return -1; |
---|
691 | |
---|
692 | if ( streams_get_flag(id, ROAR_FLAG_PASSMIXER) == 1 ) { |
---|
693 | if ( (subsys = streams_get_subsys(id)) == -1 ) |
---|
694 | return -1; |
---|
695 | |
---|
696 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
697 | if ( (pmss = g_streams[i]) != NULL ) { |
---|
698 | if ( streams_get_flag(i, ROAR_FLAG_PASSMIXER) == 1 ) { |
---|
699 | if ( streams_get_subsys(i) == subsys ) { |
---|
700 | memcpy(&(pmss->mixer), &(ss->mixer), sizeof(struct roar_mixer_settings)); |
---|
701 | |
---|
702 | // update hwmixers and the like but do not set mixer value recrusivly. |
---|
703 | streams_reset_flag(i, ROAR_FLAG_PASSMIXER); |
---|
704 | streams_set_mixer(i); |
---|
705 | streams_set_flag(i, ROAR_FLAG_PASSMIXER); |
---|
706 | } |
---|
707 | } |
---|
708 | } |
---|
709 | } |
---|
710 | } |
---|
711 | |
---|
712 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) |
---|
713 | return 0; |
---|
714 | |
---|
715 | if ( ss->driver_id == -1 ) |
---|
716 | return 0; |
---|
717 | |
---|
718 | return driver_set_volume(id, &(ss->mixer)); |
---|
719 | } |
---|
720 | |
---|
721 | int streams_ctl (int id, int_least32_t cmd, void * data) { |
---|
722 | struct roar_stream_server * ss; |
---|
723 | int_least32_t comp; |
---|
724 | |
---|
725 | if ( (ss = g_streams[id]) == NULL ) |
---|
726 | return -1; |
---|
727 | |
---|
728 | comp = cmd & ROAR_STREAM_CTL_COMPMASK; |
---|
729 | |
---|
730 | cmd &= ~comp; |
---|
731 | |
---|
732 | ROAR_DBG("streams_ctl(id=%i, cmd=?, data=%p): comp=0x%.8x, cmd=0x%.4x", id, data, comp, cmd); |
---|
733 | |
---|
734 | switch (comp) { |
---|
735 | case ROAR_STREAM_CTL_COMP_BASE: |
---|
736 | break; |
---|
737 | case ROAR_STREAM_CTL_COMP_CF: |
---|
738 | return codecfilter_ctl(ss->codecfilter_inst, ss->codecfilter, cmd, data); |
---|
739 | break; |
---|
740 | case ROAR_STREAM_CTL_COMP_DRV: |
---|
741 | break; |
---|
742 | default: |
---|
743 | return -1; |
---|
744 | } |
---|
745 | |
---|
746 | return -1; |
---|
747 | } |
---|
748 | |
---|
749 | int streams_get_outputbuffer (int id, void ** buffer, size_t size) { |
---|
750 | if ( g_streams[id] == NULL ) |
---|
751 | return -1; |
---|
752 | |
---|
753 | // output buffer size does never change. |
---|
754 | if ( g_streams[id]->output != NULL ) { |
---|
755 | *buffer = g_streams[id]->output; |
---|
756 | return 0; |
---|
757 | } |
---|
758 | |
---|
759 | if ( (g_streams[id]->output = malloc(size)) == NULL ) { |
---|
760 | ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno)); |
---|
761 | return -1; |
---|
762 | } |
---|
763 | |
---|
764 | *buffer = g_streams[id]->output; |
---|
765 | |
---|
766 | return 0; |
---|
767 | } |
---|
768 | |
---|
769 | int streams_fill_mixbuffer2 (int id, struct roar_audio_info * info) { |
---|
770 | size_t outlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(info); |
---|
771 | void * outdata; |
---|
772 | size_t inlen; |
---|
773 | size_t inlen_got; |
---|
774 | void * indata = NULL; |
---|
775 | size_t buflen; |
---|
776 | void * bufdata = NULL; |
---|
777 | struct roar_buffer * bufbuf = NULL; |
---|
778 | int is_the_same = 0; |
---|
779 | struct roar_audio_info * stream_info; |
---|
780 | struct roar_stream * s; |
---|
781 | struct roar_stream_server * ss; |
---|
782 | |
---|
783 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
784 | return -1; |
---|
785 | |
---|
786 | // set up stream_info |
---|
787 | stream_info = &(s->info); |
---|
788 | |
---|
789 | // calc todo_in |
---|
790 | inlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info); |
---|
791 | |
---|
792 | buflen = ROAR_OUTPUT_CALC_OUTBUFSIZE_MAX(info, stream_info); |
---|
793 | |
---|
794 | if ( inlen == 0 ) { |
---|
795 | ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=%p{...}): inlen == 0, this should not happen!", id, info); |
---|
796 | return -1; |
---|
797 | } |
---|
798 | |
---|
799 | if ( streams_get_outputbuffer(id, &outdata, outlen) == -1 ) { |
---|
800 | return -1; |
---|
801 | } |
---|
802 | |
---|
803 | if ( outdata == NULL ) { |
---|
804 | return -1; |
---|
805 | } |
---|
806 | |
---|
807 | ROAR_DBG("streams_fill_mixbuffer2(*): outdata=%p, len=%i->%i (in->out)", outdata, inlen, outlen); |
---|
808 | |
---|
809 | is_the_same = stream_info->rate == info->rate && stream_info->bits == info->bits && |
---|
810 | stream_info->channels == info->channels && stream_info->codec == info->codec; |
---|
811 | |
---|
812 | ROAR_DBG("streams_fill_mixbuffer2(*): is_the_same=%i", is_the_same); |
---|
813 | |
---|
814 | if ( !is_the_same && buflen > outlen ) { |
---|
815 | /* |
---|
816 | // this is not supported at the moment |
---|
817 | memset(outdata, 0, outlen); |
---|
818 | return -1; |
---|
819 | */ |
---|
820 | |
---|
821 | if ( roar_buffer_new(&bufbuf, buflen) == -1 ) |
---|
822 | return -1; |
---|
823 | |
---|
824 | if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 ) |
---|
825 | return -1; |
---|
826 | indata = bufdata; |
---|
827 | } else { |
---|
828 | indata = outdata; |
---|
829 | bufdata = outdata; |
---|
830 | } |
---|
831 | |
---|
832 | inlen_got = inlen; |
---|
833 | |
---|
834 | ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=...): inlen_got=%u", id, inlen_got); |
---|
835 | |
---|
836 | if ( stream_shift_out_buffer(id, indata, &inlen_got) == -1 ) { |
---|
837 | if ( ss->is_new ) { |
---|
838 | ss->pre_underruns++; |
---|
839 | } else { |
---|
840 | ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=...): underrun in stream", id); |
---|
841 | ss->post_underruns++; |
---|
842 | } |
---|
843 | memset(outdata, 0, outlen); |
---|
844 | return 0; |
---|
845 | } |
---|
846 | |
---|
847 | ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=...): inlen_got=%u", id, inlen_got); |
---|
848 | |
---|
849 | if ( ss->is_new ) { |
---|
850 | ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=...): stream state: new->old", id); |
---|
851 | } |
---|
852 | |
---|
853 | ss->is_new = 0; |
---|
854 | |
---|
855 | if ( is_the_same ) { |
---|
856 | if ( indata != outdata ) |
---|
857 | memcpy(outdata, indata, inlen); |
---|
858 | |
---|
859 | if ( inlen < outlen ) |
---|
860 | memset(outdata+inlen, 0, outlen-inlen); |
---|
861 | } else { |
---|
862 | // if ( roar_conv(outdata, indata, (8*inlen_got*info->rate)/(stream_info->rate * stream_info->bits), stream_info, info) == -1 ) { |
---|
863 | ROAR_DBG("streams_fill_mixbuffer2(*): CALL roar_conv2(*)..."); |
---|
864 | if ( roar_conv2(bufdata, indata, inlen, stream_info, info, buflen) == -1 ) { |
---|
865 | if ( bufbuf != NULL ) |
---|
866 | roar_buffer_free(bufbuf); |
---|
867 | return -1; |
---|
868 | } |
---|
869 | |
---|
870 | // memset(outdata, 0, outlen); |
---|
871 | } |
---|
872 | |
---|
873 | if ( bufbuf != NULL ) { |
---|
874 | memcpy(outdata, bufdata, outlen); |
---|
875 | roar_buffer_free(bufbuf); |
---|
876 | } |
---|
877 | |
---|
878 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) && !streams_get_flag(id, ROAR_FLAG_PASSMIXER) ) { |
---|
879 | if ( change_vol(outdata, info->bits, outdata, 8*outlen / info->bits, info->channels, &(ss->mixer)) == -1 ) |
---|
880 | return -1; |
---|
881 | } |
---|
882 | |
---|
883 | if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) ) { |
---|
884 | // we can ignore errors here: |
---|
885 | if ( stream_outputbuffer_request(id, &bufbuf, buflen) == 0 ) { |
---|
886 | if ( roar_buffer_get_data(bufbuf, &bufdata) != -1 ) |
---|
887 | memcpy(bufdata, outdata, outlen); |
---|
888 | } |
---|
889 | } |
---|
890 | |
---|
891 | |
---|
892 | return 0; |
---|
893 | } |
---|
894 | |
---|
895 | |
---|
896 | int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) { |
---|
897 | static void * bufs[ROAR_STREAMS_MAX+1]; |
---|
898 | int i; |
---|
899 | int have = 0; |
---|
900 | int dir; |
---|
901 | |
---|
902 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
903 | if ( g_streams[i] != NULL ) { |
---|
904 | dir = streams_get_dir(i); |
---|
905 | |
---|
906 | switch (dir) { |
---|
907 | case ROAR_DIR_PLAY: |
---|
908 | case ROAR_DIR_BIDIR: |
---|
909 | break; |
---|
910 | case ROAR_DIR_BRIDGE: |
---|
911 | if ( g_streams[i]->buffer == NULL ) |
---|
912 | continue; |
---|
913 | break; |
---|
914 | default: |
---|
915 | continue; |
---|
916 | } |
---|
917 | |
---|
918 | if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) { |
---|
919 | ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i); |
---|
920 | ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round."); |
---|
921 | continue; |
---|
922 | } |
---|
923 | if ( streams_fill_mixbuffer2(i, info) == -1 ) { |
---|
924 | ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i); |
---|
925 | continue; |
---|
926 | } |
---|
927 | |
---|
928 | // printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
929 | |
---|
930 | ROAR_DBG("streams_get_mixbuffers(*): bufs[have] = %p", bufs[have]); |
---|
931 | ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]); |
---|
932 | |
---|
933 | if ( !streams_get_flag(i, ROAR_FLAG_MUTE) ) |
---|
934 | have++; // we have a new stream! |
---|
935 | } |
---|
936 | } |
---|
937 | |
---|
938 | bufs[have] = NULL; |
---|
939 | //printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
940 | |
---|
941 | ROAR_DBG("streams_get_mixbuffers(*): have = %i", have); |
---|
942 | |
---|
943 | *bufferlist = bufs; |
---|
944 | return have; |
---|
945 | } |
---|
946 | |
---|
947 | |
---|
948 | int stream_add_buffer (int id, struct roar_buffer * buf) { |
---|
949 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
950 | |
---|
951 | if ( g_streams[id] == NULL ) |
---|
952 | return -1; |
---|
953 | |
---|
954 | if ( g_streams[id]->buffer == NULL ) { |
---|
955 | g_streams[id]->buffer = buf; |
---|
956 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf); |
---|
957 | return 0; |
---|
958 | } |
---|
959 | |
---|
960 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
961 | return roar_buffer_add(g_streams[id]->buffer, buf); |
---|
962 | } |
---|
963 | |
---|
964 | int stream_shift_out_buffer (int id, void * data, size_t * len) { |
---|
965 | if ( g_streams[id] == NULL ) |
---|
966 | return -1; |
---|
967 | |
---|
968 | if ( g_streams[id]->buffer == NULL ) |
---|
969 | return -1; |
---|
970 | |
---|
971 | return roar_buffer_shift_out(&(g_streams[id]->buffer), data, len); |
---|
972 | } |
---|
973 | |
---|
974 | int stream_shift_buffer (int id, struct roar_buffer ** buf) { |
---|
975 | struct roar_buffer * next; |
---|
976 | |
---|
977 | if ( g_streams[id] == NULL ) |
---|
978 | return -1; |
---|
979 | |
---|
980 | if ( g_streams[id]->buffer == NULL ) { |
---|
981 | *buf = NULL; |
---|
982 | return 0; |
---|
983 | } |
---|
984 | |
---|
985 | roar_buffer_get_next(g_streams[id]->buffer, &next); |
---|
986 | |
---|
987 | *buf = g_streams[id]->buffer; |
---|
988 | g_streams[id]->buffer = next; |
---|
989 | |
---|
990 | return 0; |
---|
991 | } |
---|
992 | int stream_unshift_buffer (int id, struct roar_buffer * buf) { |
---|
993 | if ( g_streams[id] == NULL ) |
---|
994 | return -1; |
---|
995 | |
---|
996 | if ( g_streams[id]->buffer == NULL ) { |
---|
997 | g_streams[id]->buffer = buf; |
---|
998 | return 0; |
---|
999 | } |
---|
1000 | |
---|
1001 | buf->next = NULL; |
---|
1002 | |
---|
1003 | roar_buffer_add(buf, g_streams[id]->buffer); |
---|
1004 | |
---|
1005 | g_streams[id]->buffer = buf; |
---|
1006 | |
---|
1007 | return 0; |
---|
1008 | } |
---|
1009 | |
---|
1010 | int stream_outputbuffer_request(int id, struct roar_buffer ** buf, size_t len) { |
---|
1011 | register struct roar_stream_server * ss; |
---|
1012 | size_t buflen; |
---|
1013 | void * bufdata; |
---|
1014 | int ret; |
---|
1015 | |
---|
1016 | if ( (ss = g_streams[id]) == NULL ) |
---|
1017 | return -1; |
---|
1018 | |
---|
1019 | if ( buf != NULL ) /* just be be sure */ |
---|
1020 | *buf = NULL; |
---|
1021 | |
---|
1022 | if ( ss->outputbuffer != NULL ) { |
---|
1023 | if ( roar_buffer_get_len(ss->outputbuffer, &buflen) == 0 ) { |
---|
1024 | if ( buflen == len ) { |
---|
1025 | if ( buf != NULL ) |
---|
1026 | *buf = ss->outputbuffer; |
---|
1027 | return 0; |
---|
1028 | } else if ( buflen > len ) { |
---|
1029 | if ( roar_buffer_set_len(ss->outputbuffer, len) == 0 ) { |
---|
1030 | if ( buf != NULL ) |
---|
1031 | *buf = ss->outputbuffer; |
---|
1032 | return 0; |
---|
1033 | } |
---|
1034 | } |
---|
1035 | } |
---|
1036 | |
---|
1037 | // if the buffer is not suitable: |
---|
1038 | |
---|
1039 | ret = roar_buffer_free(ss->outputbuffer); |
---|
1040 | ss->outputbuffer = NULL; |
---|
1041 | if ( ret == -1 ) |
---|
1042 | return ret; |
---|
1043 | } |
---|
1044 | |
---|
1045 | if ( roar_buffer_new(&(ss->outputbuffer), len) == -1 ) |
---|
1046 | return -1; |
---|
1047 | |
---|
1048 | if ( roar_buffer_get_data(ss->outputbuffer, &bufdata) == -1 ) { |
---|
1049 | roar_buffer_free(ss->outputbuffer); |
---|
1050 | ss->outputbuffer = NULL; |
---|
1051 | return -1; |
---|
1052 | } |
---|
1053 | |
---|
1054 | memset(bufdata, 0, len); |
---|
1055 | |
---|
1056 | if ( buf != NULL ) |
---|
1057 | *buf = ss->outputbuffer; |
---|
1058 | |
---|
1059 | return 0; |
---|
1060 | } |
---|
1061 | |
---|
1062 | int stream_outputbuffer_destroy(int id) { |
---|
1063 | int ret; |
---|
1064 | register struct roar_stream_server * ss; |
---|
1065 | |
---|
1066 | if ( (ss = g_streams[id]) == NULL ) |
---|
1067 | return -1; |
---|
1068 | |
---|
1069 | if ( ss->outputbuffer != NULL ) { |
---|
1070 | ret = roar_buffer_free(ss->outputbuffer); |
---|
1071 | ss->outputbuffer = NULL; |
---|
1072 | return ret; |
---|
1073 | } |
---|
1074 | |
---|
1075 | return 0; |
---|
1076 | } |
---|
1077 | |
---|
1078 | int streams_check (int id) { |
---|
1079 | int fh; |
---|
1080 | ssize_t req, realreq, done; |
---|
1081 | struct roar_stream * s; |
---|
1082 | struct roar_stream_server * ss; |
---|
1083 | struct roar_buffer * b; |
---|
1084 | char * buf; |
---|
1085 | // char tmp; |
---|
1086 | |
---|
1087 | if ( g_streams[id] == NULL ) |
---|
1088 | return -1; |
---|
1089 | |
---|
1090 | ROAR_DBG("streams_check(id=%i) = ?", id); |
---|
1091 | |
---|
1092 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
1093 | |
---|
1094 | if ( (fh = s->fh) == -1 ) |
---|
1095 | return 0; |
---|
1096 | |
---|
1097 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
1098 | return 0; |
---|
1099 | |
---|
1100 | switch (s->dir) { |
---|
1101 | case ROAR_DIR_LIGHT_IN: |
---|
1102 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
1103 | return light_check_stream(id); |
---|
1104 | #else |
---|
1105 | streams_delete(id); |
---|
1106 | return -1; |
---|
1107 | #endif |
---|
1108 | break; |
---|
1109 | case ROAR_DIR_MIDI_IN: |
---|
1110 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
1111 | return midi_check_stream(id); |
---|
1112 | #else |
---|
1113 | streams_delete(id); |
---|
1114 | return -1; |
---|
1115 | #endif |
---|
1116 | break; |
---|
1117 | case ROAR_DIR_RAW_IN: |
---|
1118 | #ifndef ROAR_WITHOUT_DCOMP_RAW |
---|
1119 | return raw_check_stream(id); |
---|
1120 | #else |
---|
1121 | streams_delete(id); |
---|
1122 | return -1; |
---|
1123 | #endif |
---|
1124 | break; |
---|
1125 | case ROAR_DIR_PLAY: |
---|
1126 | case ROAR_DIR_BIDIR: |
---|
1127 | break; |
---|
1128 | default: |
---|
1129 | /* |
---|
1130 | ROAR_WARN("streams_check(id=%i): Read event on non input stream of type/dir %s", id, roar_dir2str(s->dir)); |
---|
1131 | errno = 0; |
---|
1132 | req = stream_vio_s_read(ss, &tmp, 1); |
---|
1133 | ROAR_DBG("streams_check(id=%i): stream_vio_s_read(ss, &tmp, 1) = %li // errno=%s(%i)", id, req, strerror(errno), errno); |
---|
1134 | if ( req == 1 ) { |
---|
1135 | ROAR_ERR("streams_check(id=%i): Client violates protocol, got one byte of data on output stream, kicking stream"); |
---|
1136 | streams_delete(id); |
---|
1137 | return -1; |
---|
1138 | } |
---|
1139 | */ |
---|
1140 | return 0; |
---|
1141 | break; |
---|
1142 | } |
---|
1143 | |
---|
1144 | ROAR_DBG("streams_check(id=%i): fh = %i", id, fh); |
---|
1145 | |
---|
1146 | /* |
---|
1147 | ROAR_DBG("streams_check(id=%i): ROAR_OUTPUT_BUFFER_SAMPLES=%i, s->info.channels=%i, s->info.bits=%i, s->info.rat=%i, g_sa->rate=%i", id, ROAR_OUTPUT_BUFFER_SAMPLES, s->info.channels, s->info.bits, s->info.rate, g_sa->rate); |
---|
1148 | */ |
---|
1149 | |
---|
1150 | req = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // optimal size |
---|
1151 | // req = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * (s->info.bits / 8) * ((float)s->info.rate/g_sa->rate); |
---|
1152 | req += ss->need_extra; // bytes left we sould get.... |
---|
1153 | |
---|
1154 | ROAR_DBG("streams_check(id=%i): asking for %i bytes", id, req); |
---|
1155 | |
---|
1156 | if ( roar_buffer_new(&b, req) == -1 ) { |
---|
1157 | ROAR_ERR("streams_check(*): Can not alloc buffer space!"); |
---|
1158 | ROAR_DBG("streams_check(*) = -1"); |
---|
1159 | return -1; |
---|
1160 | } |
---|
1161 | |
---|
1162 | roar_buffer_get_data(b, (void **)&buf); |
---|
1163 | |
---|
1164 | ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id); |
---|
1165 | ROAR_DBG("streams_check(id=%i): asking for %i bytes", id, req); |
---|
1166 | |
---|
1167 | if ( ss->codecfilter == -1 ) { |
---|
1168 | realreq = req; |
---|
1169 | /* |
---|
1170 | req = read(fh, buf, req); |
---|
1171 | if ( req < realreq ) { // we can do this as the stream is in nonblocking mode! |
---|
1172 | if ( (realreq = read(fh, buf+req, realreq-req)) > 0 ) |
---|
1173 | req += realreq; |
---|
1174 | } |
---|
1175 | */ |
---|
1176 | done = 0; |
---|
1177 | while (req > 0 && done != realreq) { |
---|
1178 | if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 ) |
---|
1179 | done += req; |
---|
1180 | } |
---|
1181 | req = done; |
---|
1182 | |
---|
1183 | roar_buffer_get_data(b, (void **)&buf); |
---|
1184 | } else { |
---|
1185 | req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req); |
---|
1186 | } |
---|
1187 | |
---|
1188 | if ( req > 0 ) { |
---|
1189 | ROAR_DBG("streams_check(id=%i): got %i bytes", id, req); |
---|
1190 | |
---|
1191 | roar_buffer_set_len(b, req); |
---|
1192 | |
---|
1193 | if ( stream_add_buffer(id, b) != -1 ) |
---|
1194 | return 0; |
---|
1195 | |
---|
1196 | ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id); |
---|
1197 | roar_buffer_free(b); |
---|
1198 | } else { |
---|
1199 | ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno)); |
---|
1200 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
1201 | if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :) |
---|
1202 | #else |
---|
1203 | if ( errno != EAGAIN ) { |
---|
1204 | #endif |
---|
1205 | ROAR_DBG("streams_check(id=%i): EOF!", id); |
---|
1206 | streams_delete(id); |
---|
1207 | ROAR_DBG("streams_check(id=%i) = 0", id); |
---|
1208 | } |
---|
1209 | roar_buffer_free(b); |
---|
1210 | return 0; |
---|
1211 | } |
---|
1212 | |
---|
1213 | |
---|
1214 | ROAR_DBG("streams_check(id=%i) = -1", id); |
---|
1215 | return -1; |
---|
1216 | } |
---|
1217 | |
---|
1218 | |
---|
1219 | #define _return(x) return (x) |
---|
1220 | int streams_send_mon (int id) { |
---|
1221 | // int fh; |
---|
1222 | struct roar_stream * s; |
---|
1223 | struct roar_stream_server * ss; |
---|
1224 | struct roar_buffer * bufbuf = NULL; |
---|
1225 | struct roar_remove_state removalstate; |
---|
1226 | void * ip; |
---|
1227 | void * obuf; |
---|
1228 | int olen; |
---|
1229 | int is_the_same = 1; |
---|
1230 | int is_vol_eq = 1; |
---|
1231 | int antiecho = 0; |
---|
1232 | ssize_t ret; |
---|
1233 | |
---|
1234 | if ( g_streams[id] == NULL ) |
---|
1235 | return -1; |
---|
1236 | |
---|
1237 | ROAR_DBG("streams_send_mon(id=%i) = ?", id); |
---|
1238 | |
---|
1239 | s = ROAR_STREAM((ss = g_streams[id])); |
---|
1240 | |
---|
1241 | /* |
---|
1242 | if ( (fh = s->fh) == -1 ) |
---|
1243 | return 0; |
---|
1244 | */ |
---|
1245 | |
---|
1246 | if ( !ss->ready ) |
---|
1247 | return 0; |
---|
1248 | |
---|
1249 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
1250 | return 0; |
---|
1251 | |
---|
1252 | switch (s->dir) { |
---|
1253 | case ROAR_DIR_LIGHT_OUT: |
---|
1254 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
1255 | return light_send_stream(id); |
---|
1256 | #else |
---|
1257 | streams_delete(id); |
---|
1258 | return -1; |
---|
1259 | #endif |
---|
1260 | break; |
---|
1261 | case ROAR_DIR_MIDI_OUT: |
---|
1262 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
1263 | return midi_send_stream(id); |
---|
1264 | #else |
---|
1265 | streams_delete(id); |
---|
1266 | return -1; |
---|
1267 | #endif |
---|
1268 | break; |
---|
1269 | case ROAR_DIR_OUTPUT: |
---|
1270 | if ( g_standby ) |
---|
1271 | return 0; |
---|
1272 | case ROAR_DIR_MONITOR: |
---|
1273 | case ROAR_DIR_BIDIR: |
---|
1274 | break; |
---|
1275 | |
---|
1276 | default: |
---|
1277 | return 0; |
---|
1278 | break; |
---|
1279 | } |
---|
1280 | |
---|
1281 | |
---|
1282 | ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, s->fh); |
---|
1283 | |
---|
1284 | if ( s->info.channels != g_sa->channels || s->info.bits != g_sa->bits || |
---|
1285 | s->info.rate != g_sa->rate || s->info.codec != g_sa->codec ) |
---|
1286 | is_the_same = 0; |
---|
1287 | |
---|
1288 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) { |
---|
1289 | is_vol_eq = need_vol_change(g_sa->channels, &(ss->mixer)) ? 0 : 1; |
---|
1290 | } |
---|
1291 | |
---|
1292 | if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) ) |
---|
1293 | antiecho = 1; |
---|
1294 | |
---|
1295 | if ( !is_the_same || !is_vol_eq || antiecho ) { |
---|
1296 | olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len |
---|
1297 | // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here |
---|
1298 | if ( stream_outputbuffer_request(id, &bufbuf, ROAR_OUTPUT_CALC_OUTBUFSIZE_MAX(&(s->info), g_sa)) == -1 ) |
---|
1299 | return -1; |
---|
1300 | |
---|
1301 | if ( roar_buffer_get_data(bufbuf, &obuf) == -1 ) { |
---|
1302 | _return(-1); |
---|
1303 | } |
---|
1304 | |
---|
1305 | ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen); |
---|
1306 | } else { |
---|
1307 | obuf = g_output_buffer; |
---|
1308 | olen = g_output_buffer_len; |
---|
1309 | } |
---|
1310 | |
---|
1311 | ip = g_output_buffer; |
---|
1312 | |
---|
1313 | if ( antiecho ) { |
---|
1314 | ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho); |
---|
1315 | if ( roar_remove_init(&removalstate) == -1 ) { |
---|
1316 | _return(-1); |
---|
1317 | } |
---|
1318 | |
---|
1319 | ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho); |
---|
1320 | if ( roar_remove_so(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->bits, &removalstate) == -1 ) { |
---|
1321 | ROAR_DBG("streams_send_mon(id=%i): anti echo failed", id); |
---|
1322 | _return(-1); |
---|
1323 | } |
---|
1324 | ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho); |
---|
1325 | } |
---|
1326 | |
---|
1327 | if ( !is_vol_eq ) { |
---|
1328 | if ( change_vol(obuf, g_sa->bits, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->channels, &(ss->mixer)) == -1 ) { |
---|
1329 | _return(-1); |
---|
1330 | } |
---|
1331 | |
---|
1332 | ip = obuf; |
---|
1333 | } |
---|
1334 | |
---|
1335 | if ( !is_the_same ) { |
---|
1336 | if ( roar_conv(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) { |
---|
1337 | _return(-1); |
---|
1338 | } |
---|
1339 | } |
---|
1340 | |
---|
1341 | errno = 0; |
---|
1342 | |
---|
1343 | if ( ss->codecfilter == -1 ) { |
---|
1344 | ROAR_DBG("streams_send_mon(id=%i): not a CF stream", id); |
---|
1345 | if ( s->fh == -1 && roar_vio_get_fh(&(ss->vio)) == -1 ) { |
---|
1346 | _return(0); |
---|
1347 | } |
---|
1348 | |
---|
1349 | if ( (ret = stream_vio_s_write(ss, obuf, olen)) == olen ) { |
---|
1350 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
1351 | _return(0); |
---|
1352 | } |
---|
1353 | |
---|
1354 | if ( ret > 0 && errno == 0 ) { |
---|
1355 | ROAR_WARN("streams_send_mon(id=%i): Overrun in stream: wrote %i of %i bytes, %i bytes missing", id, (int)ret, olen, olen-(int)ret); |
---|
1356 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), ret)*s->info.channels); |
---|
1357 | _return(0); |
---|
1358 | } |
---|
1359 | } else { |
---|
1360 | errno = 0; |
---|
1361 | if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen) |
---|
1362 | == olen ) { |
---|
1363 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
1364 | _return(0); |
---|
1365 | } else { // we cann't retry on codec filetered streams |
---|
1366 | if ( errno != EAGAIN ) { |
---|
1367 | streams_delete(id); |
---|
1368 | _return(-1); |
---|
1369 | } |
---|
1370 | } |
---|
1371 | } |
---|
1372 | |
---|
1373 | if ( errno == EAGAIN ) { |
---|
1374 | // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to |
---|
1375 | // make any gapes in any output because of this |
---|
1376 | |
---|
1377 | #ifdef ROAR_HAVE_USLEEP |
---|
1378 | usleep(100); // 0.1ms |
---|
1379 | #endif |
---|
1380 | |
---|
1381 | if ( stream_vio_s_write(ss, obuf, olen) == olen ) { |
---|
1382 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
1383 | _return(0); |
---|
1384 | } else if ( errno == EAGAIN ) { |
---|
1385 | ROAR_WARN("streams_send_mon(id=%i): Can not send data to client: %s", id, strerror(errno)); |
---|
1386 | _return(0); |
---|
1387 | } |
---|
1388 | } |
---|
1389 | |
---|
1390 | // ug... error... delete stream! |
---|
1391 | |
---|
1392 | streams_delete(id); |
---|
1393 | |
---|
1394 | _return(-1); |
---|
1395 | } |
---|
1396 | #undef _return |
---|
1397 | |
---|
1398 | int streams_send_filter(int id) { |
---|
1399 | int fh; |
---|
1400 | int have = 0; |
---|
1401 | int len; |
---|
1402 | struct roar_stream * s; |
---|
1403 | struct roar_stream_server * ss; |
---|
1404 | |
---|
1405 | if ( g_streams[id] == NULL ) |
---|
1406 | return -1; |
---|
1407 | |
---|
1408 | ROAR_DBG("streams_send_filter(id=%i) = ?", id); |
---|
1409 | |
---|
1410 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
1411 | |
---|
1412 | if ( (fh = s->fh) == -1 ) |
---|
1413 | return 0; |
---|
1414 | |
---|
1415 | if ( s->dir != ROAR_DIR_FILTER ) |
---|
1416 | return 0; |
---|
1417 | |
---|
1418 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
1419 | return 0; |
---|
1420 | |
---|
1421 | |
---|
1422 | ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh); |
---|
1423 | |
---|
1424 | if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) { |
---|
1425 | while ( have < g_output_buffer_len ) { |
---|
1426 | if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) { |
---|
1427 | streams_delete(id); |
---|
1428 | return -1; |
---|
1429 | } |
---|
1430 | have += len; |
---|
1431 | } |
---|
1432 | return 0; |
---|
1433 | } |
---|
1434 | |
---|
1435 | // ug... error... delete stream! |
---|
1436 | |
---|
1437 | streams_delete(id); |
---|
1438 | |
---|
1439 | return -1; |
---|
1440 | } |
---|
1441 | |
---|
1442 | |
---|
1443 | // VIO: |
---|
1444 | |
---|
1445 | ssize_t stream_vio_read (int stream, void *buf, size_t count) { |
---|
1446 | struct roar_stream_server * s = g_streams[stream]; |
---|
1447 | |
---|
1448 | if ( !s ) |
---|
1449 | return -1; |
---|
1450 | |
---|
1451 | return stream_vio_s_read(s, buf, count); |
---|
1452 | } |
---|
1453 | |
---|
1454 | ssize_t stream_vio_write(int stream, void *buf, size_t count) { |
---|
1455 | struct roar_stream_server * s = g_streams[stream]; |
---|
1456 | |
---|
1457 | if ( !s ) |
---|
1458 | return -1; |
---|
1459 | |
---|
1460 | return stream_vio_s_write(s, buf, count); |
---|
1461 | } |
---|
1462 | |
---|
1463 | |
---|
1464 | ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) { |
---|
1465 | void * orig_buf = buf; |
---|
1466 | size_t len = 0; |
---|
1467 | ssize_t r = -1; |
---|
1468 | int i; |
---|
1469 | |
---|
1470 | errno = 0; |
---|
1471 | |
---|
1472 | if ( !stream ) |
---|
1473 | return -1; |
---|
1474 | |
---|
1475 | //roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
1476 | |
---|
1477 | if ( ! stream->vio.read ) |
---|
1478 | return -1; |
---|
1479 | |
---|
1480 | while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) { |
---|
1481 | len += r; |
---|
1482 | buf += r; |
---|
1483 | count -= r; |
---|
1484 | if ( count == 0 ) |
---|
1485 | break; |
---|
1486 | } |
---|
1487 | |
---|
1488 | if ( len == 0 && r == -1 ) |
---|
1489 | return -1; |
---|
1490 | |
---|
1491 | if ( streams_thru_num ) |
---|
1492 | for (i = 0; i < ROAR_STREAMS_MAX; i++) |
---|
1493 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == ROAR_STREAM(stream)->id ) |
---|
1494 | if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) |
---|
1495 | if ( g_streams[i]->ready ) |
---|
1496 | if ( stream_vio_write(i, orig_buf, len) != len ) |
---|
1497 | streams_delete(i); |
---|
1498 | |
---|
1499 | return len; |
---|
1500 | } |
---|
1501 | |
---|
1502 | ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) { |
---|
1503 | int i; |
---|
1504 | |
---|
1505 | errno = 0; |
---|
1506 | |
---|
1507 | if ( !stream ) |
---|
1508 | return -1; |
---|
1509 | |
---|
1510 | /* |
---|
1511 | if ( roar_vio_get_fh(&(stream->vio)) == -1 && ROAR_STREAM(stream)->fh != -1 ) |
---|
1512 | roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
1513 | */ |
---|
1514 | |
---|
1515 | // ROAR_WARN("stream_vio_s_write(*): writing..."); |
---|
1516 | |
---|
1517 | if ( streams_thru_num ) |
---|
1518 | for (i = 0; i < ROAR_STREAMS_MAX; i++) |
---|
1519 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == ROAR_STREAM(stream)->id ) |
---|
1520 | if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) |
---|
1521 | if ( g_streams[i]->ready ) |
---|
1522 | if ( stream_vio_write(i, buf, count) != count ) |
---|
1523 | streams_delete(i); |
---|
1524 | |
---|
1525 | return roar_vio_write(&(stream->vio), buf, count); |
---|
1526 | } |
---|
1527 | |
---|
1528 | //ll |
---|