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