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