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