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