1 | //streams.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
5 | * |
---|
6 | * This file is part of roard a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | #include "roard.h" |
---|
26 | |
---|
27 | int streams_init (void) { |
---|
28 | int i; |
---|
29 | |
---|
30 | for (i = 0; i < ROAR_STREAMS_MAX; i++) |
---|
31 | g_streams[i] = NULL; |
---|
32 | |
---|
33 | return 0; |
---|
34 | } |
---|
35 | |
---|
36 | int streams_free (void) { |
---|
37 | int i; |
---|
38 | |
---|
39 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
40 | if ( g_streams[i] != NULL ) { |
---|
41 | streams_delete(i); |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | return 0; |
---|
46 | } |
---|
47 | |
---|
48 | |
---|
49 | int streams_new (void) { |
---|
50 | int i, j; |
---|
51 | struct roar_stream * n = NULL; |
---|
52 | struct roar_stream_server * s = NULL; |
---|
53 | |
---|
54 | #ifdef ROAR_SUPPORT_LISTEN |
---|
55 | if ( g_terminate && !g_no_listen ) // don't accept new streams in case of termination state |
---|
56 | return -1; |
---|
57 | #else |
---|
58 | if ( g_terminate ) // don't accept new streams in case of termination state |
---|
59 | return -1; |
---|
60 | #endif |
---|
61 | |
---|
62 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
63 | if ( g_streams[i] == NULL ) { |
---|
64 | s = ROAR_STREAM_SERVER(n = ROAR_STREAM(malloc(sizeof(struct roar_stream_server)))); |
---|
65 | if ( n == NULL ) { |
---|
66 | ROAR_ERR("streams_new(void): can not allocate memory for new stream: %s", strerror(errno)); |
---|
67 | ROAR_DBG("streams_new(void) = -1"); |
---|
68 | return -1; |
---|
69 | } |
---|
70 | |
---|
71 | n->id = i; |
---|
72 | n->fh = -1; |
---|
73 | // n->pos_rel_id = i; |
---|
74 | /* |
---|
75 | n->database = NULL; |
---|
76 | n->dataoff = NULL; |
---|
77 | n->datalen = 0; |
---|
78 | n->offset = 0; |
---|
79 | */ |
---|
80 | n->pos = 0; |
---|
81 | |
---|
82 | s->name = NULL; |
---|
83 | |
---|
84 | s->client = -1; |
---|
85 | s->socktype = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
86 | s->buffer = NULL; |
---|
87 | s->need_extra = 0; |
---|
88 | s->output = NULL; |
---|
89 | s->is_new = 1; |
---|
90 | s->codecfilter = -1; |
---|
91 | s->pre_underruns = 0; |
---|
92 | s->post_underruns = 0; |
---|
93 | s->delay = 0; |
---|
94 | s->codec_orgi = -1; |
---|
95 | s->primary = 0; |
---|
96 | |
---|
97 | s->mixer.scale = 65535; |
---|
98 | s->mixer.rpg_mul = 1; |
---|
99 | s->mixer.rpg_div = 1; |
---|
100 | for (j = 0; j < ROAR_MAX_CHANNELS; j++) |
---|
101 | s->mixer.mixer[j] = 65535; |
---|
102 | |
---|
103 | #ifdef ROAR_SUPPORT_META |
---|
104 | for (j = 0; j < ROAR_META_MAX_PER_STREAM; j++) { |
---|
105 | s->meta[j].type = ROAR_META_TYPE_NONE; |
---|
106 | s->meta[j].key[0] = 0; |
---|
107 | s->meta[j].value = NULL; |
---|
108 | } |
---|
109 | #endif |
---|
110 | |
---|
111 | roar_vio_init_calls(&(s->vio)); |
---|
112 | s->driver_id = -1; |
---|
113 | s->flags = ROAR_FLAG_NONE; |
---|
114 | |
---|
115 | //roardsp_fchain_init(&(s->fc)); |
---|
116 | |
---|
117 | g_streams[i] = s; |
---|
118 | ROAR_DBG("streams_new(void): n->id=%i", n->id); |
---|
119 | ROAR_DBG("streams_new(void) = %i", i); |
---|
120 | return i; |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | return -1; |
---|
125 | } |
---|
126 | |
---|
127 | int streams_delete (int id) { |
---|
128 | struct roar_stream_server * s; |
---|
129 | int prim; |
---|
130 | int no_vio_close = 0; |
---|
131 | int i; |
---|
132 | |
---|
133 | if ( (s = g_streams[id]) == NULL ) |
---|
134 | return 0; |
---|
135 | |
---|
136 | ROAR_DBG("streams_delete(id=%i) = ?", id); |
---|
137 | ROAR_DBG("streams_delete(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(s)->id); |
---|
138 | |
---|
139 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
140 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) { |
---|
141 | if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) { |
---|
142 | streams_delete(i); |
---|
143 | } else { |
---|
144 | ROAR_STREAM(g_streams[i])->pos_rel_id = -1; |
---|
145 | } |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | #ifdef ROAR_SUPPORT_META |
---|
150 | // delete meta data form other meta streams if needed |
---|
151 | if ( streams_get_flag(id, ROAR_FLAG_META) == 1 ) { |
---|
152 | ROAR_DBG("streams_delete(id=%i): deleting meta stream!", id); |
---|
153 | stream_meta_clear(id); |
---|
154 | stream_meta_finalize(id); |
---|
155 | } |
---|
156 | #endif |
---|
157 | |
---|
158 | if ( s->codecfilter != -1 ) { |
---|
159 | codecfilter_close(s->codecfilter_inst, s->codecfilter); |
---|
160 | s->codecfilter_inst = NULL; |
---|
161 | s->codecfilter = -1; |
---|
162 | } |
---|
163 | |
---|
164 | if ( s->driver_id != -1 ) { |
---|
165 | driver_closevio(&(s->vio), s->driver_id); |
---|
166 | roar_vio_init_calls(&(s->vio)); |
---|
167 | s->driver_id = -1; |
---|
168 | no_vio_close = 1; |
---|
169 | } |
---|
170 | |
---|
171 | //roardsp_fchain_uninit(&(s->fc)); |
---|
172 | |
---|
173 | if ( s->client != -1 ) { |
---|
174 | ROAR_DBG("streams_delete(id=%i): Stream is owned by client %i", id, g_streams[id]->client); |
---|
175 | client_stream_delete(s->client, id); |
---|
176 | } |
---|
177 | |
---|
178 | if ( s->buffer != NULL ) |
---|
179 | roar_buffer_free(s->buffer); |
---|
180 | |
---|
181 | if ( s->output != NULL ) |
---|
182 | free(s->output); |
---|
183 | |
---|
184 | /* |
---|
185 | if ( ROAR_STREAM(s)->fh != -1 ) |
---|
186 | close(ROAR_STREAM(s)->fh); |
---|
187 | */ |
---|
188 | |
---|
189 | if ( !no_vio_close ) |
---|
190 | roar_vio_close(&(s->vio)); |
---|
191 | |
---|
192 | prim = s->primary; |
---|
193 | |
---|
194 | if ( s->name != NULL ) |
---|
195 | free(s->name); |
---|
196 | |
---|
197 | free(s); |
---|
198 | |
---|
199 | g_streams[id] = NULL; |
---|
200 | |
---|
201 | if ( prim ) { |
---|
202 | alive = 0; |
---|
203 | clean_quit(); |
---|
204 | } |
---|
205 | |
---|
206 | ROAR_DBG("streams_delete(id=%i) = 0", id); |
---|
207 | return 0; |
---|
208 | } |
---|
209 | |
---|
210 | int streams_set_client (int id, int client) { |
---|
211 | if ( g_streams[id] == NULL ) |
---|
212 | return -1; |
---|
213 | |
---|
214 | ROAR_DBG("streams_set_client(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id); |
---|
215 | g_streams[id]->client = client; |
---|
216 | |
---|
217 | return 0; |
---|
218 | } |
---|
219 | |
---|
220 | int streams_get_client (int id) { |
---|
221 | if ( g_streams[id] == NULL ) |
---|
222 | return -1; |
---|
223 | |
---|
224 | return g_streams[id]->client; |
---|
225 | } |
---|
226 | |
---|
227 | int streams_set_dir (int id, int dir, int defaults) { |
---|
228 | struct roar_stream_server * ss; |
---|
229 | |
---|
230 | if ( (ss = g_streams[id]) == NULL ) |
---|
231 | return -1; |
---|
232 | |
---|
233 | ROAR_STREAM(ss)->dir = dir; |
---|
234 | |
---|
235 | if ( defaults ) { |
---|
236 | if ( dir <= 0 || dir >= ROAR_DIR_DIRIDS ) |
---|
237 | return -1; |
---|
238 | |
---|
239 | ROAR_DBG("streams_set_dir(*): g_config->streams[dir=%i].flags = 0x%0.4x", dir, g_config->streams[dir].flags); |
---|
240 | |
---|
241 | if ( streams_set_flag(id, g_config->streams[dir].flags) == -1 ) { |
---|
242 | ROAR_DBG("streams_set_dir(*) = -1 // can not set stream flags"); |
---|
243 | return -1; |
---|
244 | } |
---|
245 | |
---|
246 | ss->mixer.scale = g_config->streams[dir].mixer.scale; |
---|
247 | ss->mixer.rpg_mul = g_config->streams[dir].mixer.rpg_mul; |
---|
248 | ss->mixer.rpg_div = g_config->streams[dir].mixer.rpg_div; |
---|
249 | } |
---|
250 | |
---|
251 | ROAR_DBG("streams_set_dir(*) = 0"); |
---|
252 | return 0; |
---|
253 | } |
---|
254 | |
---|
255 | int streams_set_fh (int id, int fh) { |
---|
256 | struct roar_stream_server * ss; |
---|
257 | int dir; |
---|
258 | |
---|
259 | if ( (ss = g_streams[id]) == NULL ) |
---|
260 | return -1; |
---|
261 | |
---|
262 | ROAR_DBG("streams_set_fh(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(ss)->id); |
---|
263 | |
---|
264 | ROAR_STREAM(g_streams[id])->fh = fh; |
---|
265 | |
---|
266 | ROAR_DBG("streams_set_fh(id=%i, fh=%i): driverID=%i", id, fh, ss->driver_id); |
---|
267 | |
---|
268 | if ( ss->driver_id == -1 && fh != -2 ) |
---|
269 | roar_vio_set_fh(&(ss->vio), fh); |
---|
270 | |
---|
271 | if ( codecfilter_open(&(ss->codecfilter_inst), &(ss->codecfilter), NULL, |
---|
272 | ROAR_STREAM(ss)->info.codec, ss) == -1 ) { |
---|
273 | return streams_delete(id); |
---|
274 | } |
---|
275 | |
---|
276 | if ( fh == -2 ) { |
---|
277 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
278 | if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_READ_FH, &fh) == -1 ) { |
---|
279 | fh = -2; |
---|
280 | } else { |
---|
281 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
282 | if ( fh < 0 ) { |
---|
283 | fh = -2; |
---|
284 | } else { |
---|
285 | ROAR_STREAM(g_streams[id])->fh = fh; |
---|
286 | } |
---|
287 | } |
---|
288 | } |
---|
289 | |
---|
290 | if ( fh == -1 || fh == -2 ) { // yes, this is valid, indecats full vio! |
---|
291 | return 0; |
---|
292 | } |
---|
293 | |
---|
294 | // roar_socket_recvbuf(fh, ROAR_OUTPUT_CALC_OUTBUFSIZE( &(ROAR_STREAM(g_streams[id])->info) )); // set recv buffer to minimum |
---|
295 | |
---|
296 | dir = ROAR_STREAM(ss)->dir; |
---|
297 | |
---|
298 | if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD || dir == ROAR_DIR_OUTPUT ) { |
---|
299 | ROAR_SHUTDOWN(fh, SHUT_RD); |
---|
300 | } |
---|
301 | |
---|
302 | if ( dir == ROAR_DIR_FILTER ) { |
---|
303 | return 0; |
---|
304 | } else { |
---|
305 | return roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK); |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | int streams_get_fh (int id) { |
---|
310 | if ( id < 0 ) |
---|
311 | return -1; |
---|
312 | |
---|
313 | if ( g_streams[id] == NULL ) |
---|
314 | return -1; |
---|
315 | |
---|
316 | return ROAR_STREAM(g_streams[id])->fh; |
---|
317 | } |
---|
318 | |
---|
319 | int streams_get (int id, struct roar_stream_server ** stream) { |
---|
320 | if ( g_streams[id] == NULL ) |
---|
321 | return -1; |
---|
322 | |
---|
323 | *stream = g_streams[id]; |
---|
324 | |
---|
325 | return 0; |
---|
326 | } |
---|
327 | |
---|
328 | int streams_set_socktype (int id, int socktype) { |
---|
329 | if ( g_streams[id] == NULL ) |
---|
330 | return -1; |
---|
331 | |
---|
332 | g_streams[id]->socktype = socktype; |
---|
333 | |
---|
334 | return 0; |
---|
335 | } |
---|
336 | |
---|
337 | int streams_get_socktype (int id) { |
---|
338 | if ( g_streams[id] == NULL ) |
---|
339 | return -1; |
---|
340 | |
---|
341 | return g_streams[id]->socktype; |
---|
342 | } |
---|
343 | |
---|
344 | int streams_set_primary (int id, int prim) { |
---|
345 | if ( g_streams[id] == NULL ) |
---|
346 | return -1; |
---|
347 | |
---|
348 | g_streams[id]->primary = prim; |
---|
349 | |
---|
350 | return 0; |
---|
351 | } |
---|
352 | |
---|
353 | int streams_mark_primary (int id) { |
---|
354 | return streams_set_primary(id, 1); |
---|
355 | } |
---|
356 | |
---|
357 | int streams_set_sync (int id, int sync) { |
---|
358 | int fh = streams_get_fh(id); |
---|
359 | |
---|
360 | if ( fh != -1 ) { |
---|
361 | if ( roar_socket_nonblock(fh, sync ? ROAR_SOCKET_BLOCK : ROAR_SOCKET_NONBLOCK) == -1 ) |
---|
362 | return -1; |
---|
363 | |
---|
364 | #ifdef ROAR_FDATASYNC |
---|
365 | ROAR_FDATASYNC(fh); |
---|
366 | #endif |
---|
367 | |
---|
368 | return 0; |
---|
369 | } else { |
---|
370 | return roar_vio_nonblock(&(g_streams[id]->vio), sync); |
---|
371 | } |
---|
372 | } |
---|
373 | |
---|
374 | int streams_set_flag (int id, int flag) { |
---|
375 | if ( g_streams[id] == NULL ) |
---|
376 | return -1; |
---|
377 | |
---|
378 | if ( flag & ROAR_FLAG_PRIMARY ) { |
---|
379 | streams_set_primary(id, 1); |
---|
380 | flag -= ROAR_FLAG_PRIMARY; |
---|
381 | } |
---|
382 | |
---|
383 | if ( flag & ROAR_FLAG_SYNC ) { |
---|
384 | switch (ROAR_STREAM(g_streams[id])->dir) { |
---|
385 | // for this stream types the flag is used in the subsystem: |
---|
386 | case ROAR_DIR_BRIDGE: |
---|
387 | case ROAR_DIR_MIDI_OUT: |
---|
388 | break; |
---|
389 | |
---|
390 | // normal behavor (vio blocking): |
---|
391 | default: |
---|
392 | if ( streams_set_sync(id, 1) == -1 ) |
---|
393 | flag -= ROAR_FLAG_SYNC; |
---|
394 | } |
---|
395 | } |
---|
396 | |
---|
397 | if ( flag & ROAR_FLAG_HWMIXER ) { // currently not supported -> ignored |
---|
398 | g_streams[id]->flags |= flag; |
---|
399 | if ( streams_set_mixer(id) == -1 ) { |
---|
400 | g_streams[id]->flags -= flag; |
---|
401 | return -1; |
---|
402 | } |
---|
403 | } |
---|
404 | |
---|
405 | g_streams[id]->flags |= flag; |
---|
406 | |
---|
407 | #ifdef ROAR_SUPPORT_META |
---|
408 | if ( flag & ROAR_FLAG_META ) |
---|
409 | stream_meta_finalize(id); |
---|
410 | #endif |
---|
411 | |
---|
412 | return 0; |
---|
413 | } |
---|
414 | |
---|
415 | int streams_reset_flag (int id, int flag) { |
---|
416 | if ( g_streams[id] == NULL ) |
---|
417 | return -1; |
---|
418 | |
---|
419 | if ( flag & ROAR_FLAG_PRIMARY ) { |
---|
420 | streams_set_primary(id, 0); |
---|
421 | flag -= ROAR_FLAG_PRIMARY; |
---|
422 | } |
---|
423 | |
---|
424 | if ( flag & ROAR_FLAG_SYNC ) { |
---|
425 | streams_set_sync(id, 0); |
---|
426 | } |
---|
427 | |
---|
428 | g_streams[id]->flags |= flag; |
---|
429 | g_streams[id]->flags -= flag; |
---|
430 | |
---|
431 | return 0; |
---|
432 | } |
---|
433 | |
---|
434 | int streams_get_flag (int id, int flag) { |
---|
435 | if ( g_streams[id] == NULL ) |
---|
436 | return -1; |
---|
437 | |
---|
438 | return g_streams[id]->flags & flag ? 1 : 0; |
---|
439 | } |
---|
440 | |
---|
441 | int streams_set_name (int id, char * name) { |
---|
442 | char * str; |
---|
443 | |
---|
444 | if ( g_streams[id] == NULL ) |
---|
445 | return -1; |
---|
446 | |
---|
447 | if ( (str = strdup(name)) == NULL ) |
---|
448 | return -1; |
---|
449 | |
---|
450 | if ( g_streams[id]->name != NULL ) |
---|
451 | free(g_streams[id]->name); |
---|
452 | |
---|
453 | g_streams[id]->name = str; |
---|
454 | |
---|
455 | return 0; |
---|
456 | } |
---|
457 | |
---|
458 | char * streams_get_name (int id) { |
---|
459 | if ( g_streams[id] == NULL ) |
---|
460 | return NULL; |
---|
461 | |
---|
462 | return g_streams[id]->name; |
---|
463 | } |
---|
464 | |
---|
465 | |
---|
466 | int streams_calc_delay (int id) { |
---|
467 | struct roar_stream_server * ss; |
---|
468 | struct roar_stream * s; |
---|
469 | register uint_least32_t d = 0; |
---|
470 | uint_least32_t t[1]; |
---|
471 | uint64_t tmp; |
---|
472 | |
---|
473 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
474 | return -1; |
---|
475 | |
---|
476 | if ( ss->codecfilter != -1 ) { |
---|
477 | if ( codecfilter_delay(ss->codecfilter_inst, ss->codecfilter, t) != -1 ) |
---|
478 | d += *t; |
---|
479 | } |
---|
480 | |
---|
481 | if ( ss->vio.ctl != NULL ) { |
---|
482 | if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_DELAY, t) != -1 ) { // *t is in byte |
---|
483 | ROAR_DBG("streams_calc_delay(id=%i): VIO delay in byte: %i", id, *t); |
---|
484 | tmp = *t; |
---|
485 | tmp *= 1000000; // musec per sec |
---|
486 | tmp /= s->info.rate * s->info.channels * (s->info.bits/8); |
---|
487 | ROAR_DBG("streams_calc_delay(id=%i): VIO delay in musec: %i", id, tmp); |
---|
488 | |
---|
489 | d += tmp; |
---|
490 | } |
---|
491 | } |
---|
492 | |
---|
493 | ROAR_DBG("streams_calc_delay(id=%i): delay in musec: %i", id, d); |
---|
494 | |
---|
495 | ss->delay = d; |
---|
496 | |
---|
497 | return 0; |
---|
498 | } |
---|
499 | |
---|
500 | int streams_set_mixer (int id) { |
---|
501 | struct roar_stream_server * ss; |
---|
502 | |
---|
503 | if ( (ss = g_streams[id]) == NULL ) |
---|
504 | return -1; |
---|
505 | |
---|
506 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) |
---|
507 | return 0; |
---|
508 | |
---|
509 | if ( ss->driver_id == -1 ) |
---|
510 | return 0; |
---|
511 | |
---|
512 | return driver_set_volume(id, &(ss->mixer)); |
---|
513 | } |
---|
514 | |
---|
515 | int streams_ctl (int id, int_least32_t cmd, void * data) { |
---|
516 | struct roar_stream_server * ss; |
---|
517 | int_least32_t comp; |
---|
518 | |
---|
519 | if ( (ss = g_streams[id]) == NULL ) |
---|
520 | return -1; |
---|
521 | |
---|
522 | comp = cmd & ROAR_STREAM_CTL_COMPMASK; |
---|
523 | |
---|
524 | cmd &= ~comp; |
---|
525 | |
---|
526 | ROAR_DBG("streams_ctl(id=%i, cmd=?, data=%p): comp=0x%.8x, cmd=0x%.4x", id, data, comp, cmd); |
---|
527 | |
---|
528 | switch (comp) { |
---|
529 | case ROAR_STREAM_CTL_COMP_BASE: |
---|
530 | break; |
---|
531 | case ROAR_STREAM_CTL_COMP_CF: |
---|
532 | return codecfilter_ctl(ss->codecfilter_inst, ss->codecfilter, cmd, data); |
---|
533 | break; |
---|
534 | case ROAR_STREAM_CTL_COMP_DRV: |
---|
535 | break; |
---|
536 | default: |
---|
537 | return -1; |
---|
538 | } |
---|
539 | |
---|
540 | return -1; |
---|
541 | } |
---|
542 | |
---|
543 | int streams_get_outputbuffer (int id, void ** buffer, size_t size) { |
---|
544 | if ( g_streams[id] == NULL ) |
---|
545 | return -1; |
---|
546 | |
---|
547 | // output buffer size does never change. |
---|
548 | if ( g_streams[id]->output != NULL ) { |
---|
549 | *buffer = g_streams[id]->output; |
---|
550 | return 0; |
---|
551 | } |
---|
552 | |
---|
553 | if ( (g_streams[id]->output = malloc(size)) == NULL ) { |
---|
554 | ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno)); |
---|
555 | return -1; |
---|
556 | } |
---|
557 | |
---|
558 | *buffer = g_streams[id]->output; |
---|
559 | |
---|
560 | return 0; |
---|
561 | } |
---|
562 | |
---|
563 | int streams_fill_mixbuffer (int id, struct roar_audio_info * info) { |
---|
564 | // TODO: decide: is this the most complex, hacked, un-understadable, |
---|
565 | // un-writeable and even worse: un-readable |
---|
566 | // function in the whole project? |
---|
567 | size_t todo = ROAR_OUTPUT_CALC_OUTBUFSIZE(info); |
---|
568 | size_t needed = todo; |
---|
569 | size_t todo_in; |
---|
570 | size_t len, outlen; |
---|
571 | size_t mul = 1, div = 1; |
---|
572 | void * rest = NULL; |
---|
573 | void * in = NULL; |
---|
574 | struct roar_buffer * buf; |
---|
575 | struct roar_audio_info * stream_info; |
---|
576 | struct roar_stream_server * stream = g_streams[id]; |
---|
577 | int is_the_same = 0; |
---|
578 | |
---|
579 | if ( g_streams[id] == NULL ) |
---|
580 | return -1; |
---|
581 | |
---|
582 | if ( streams_get_outputbuffer(id, &rest, todo) == -1 ) { |
---|
583 | return -1; |
---|
584 | } |
---|
585 | |
---|
586 | if ( rest == NULL ) { |
---|
587 | return -1; |
---|
588 | } |
---|
589 | |
---|
590 | // set up stream_info |
---|
591 | |
---|
592 | stream_info = &(ROAR_STREAM(stream)->info); |
---|
593 | |
---|
594 | // calc todo_in |
---|
595 | todo_in = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info); |
---|
596 | |
---|
597 | // calc mul and div: |
---|
598 | mul = todo / todo_in; |
---|
599 | div = todo_in / todo; |
---|
600 | |
---|
601 | if ( mul == 0 ) { |
---|
602 | mul = 1; |
---|
603 | } else { |
---|
604 | div = 1; |
---|
605 | } |
---|
606 | |
---|
607 | ROAR_DBG("streams_fill_mixbuffer(*): mul=%i, div=%i", mul, div); |
---|
608 | |
---|
609 | ROAR_DBG("streams_fill_mixbuffer(*): rest=%p, todo=%i->%i (in->out)", rest, todo_in, todo); |
---|
610 | // are both (input and output) of same format? |
---|
611 | |
---|
612 | |
---|
613 | ROAR_DBG("streams_fill_mixbuffer(*): stream_info:"); |
---|
614 | roar_debug_audio_info_print(stream_info); |
---|
615 | ROAR_DBG("streams_fill_mixbuffer(*): info:"); |
---|
616 | roar_debug_audio_info_print(info); |
---|
617 | |
---|
618 | is_the_same = stream_info->rate == info->rate && stream_info->bits == info->bits && |
---|
619 | stream_info->channels == info->channels && stream_info->codec == info->codec; |
---|
620 | |
---|
621 | ROAR_DBG("streams_fill_mixbuffer(*): is_the_same=%i", is_the_same); |
---|
622 | |
---|
623 | /* How it works: |
---|
624 | * |
---|
625 | * set a counter to the number of samples we need. |
---|
626 | * loop until we have all samples done or no samples are |
---|
627 | * left in our input buffer. |
---|
628 | * If there a no samples left in the input buffer: fill the rest |
---|
629 | * of the output buffer with zeros. |
---|
630 | * |
---|
631 | * The loop: |
---|
632 | * get a buffer from the input. |
---|
633 | * if it's bigger than needed, set an offset. |
---|
634 | * The rest of the data: |
---|
635 | * 0) convert endianness (codec) from remote to local... |
---|
636 | * 1) change bits in of the samples |
---|
637 | * 2) change sample rate |
---|
638 | * 3) change the nummber of channels |
---|
639 | * 4) insert into output buffer |
---|
640 | */ |
---|
641 | |
---|
642 | /* |
---|
643 | // get our first buffer: |
---|
644 | |
---|
645 | if ( stream_shift_buffer(id, &buf) == -1 ) { |
---|
646 | return -1; |
---|
647 | } |
---|
648 | |
---|
649 | // first test for some basic simple cases... |
---|
650 | |
---|
651 | if ( buf == NULL ) { // we habe nothing in input queue |
---|
652 | // we may memset() our output buffer OR |
---|
653 | // just return with -1 so we are going to |
---|
654 | // be ignored. |
---|
655 | return -1; |
---|
656 | } |
---|
657 | */ |
---|
658 | |
---|
659 | while (todo) { // main loop |
---|
660 | ROAR_DBG("streams_fill_mixbuffer(*): looping..."); |
---|
661 | // exit loop if nothing is left, even if we need more data.. |
---|
662 | if ( stream_shift_buffer(id, &buf) == -1 ) |
---|
663 | break; |
---|
664 | if ( buf == NULL ) |
---|
665 | break; |
---|
666 | |
---|
667 | // read the data for this loop... |
---|
668 | roar_buffer_get_data(buf, &in); |
---|
669 | roar_buffer_get_len(buf, &len); |
---|
670 | |
---|
671 | ROAR_DBG("streams_fill_mixbuffer(*): len = %i", len); |
---|
672 | |
---|
673 | if ( len > todo_in ) { |
---|
674 | roar_buffer_set_offset(buf, todo_in); |
---|
675 | len = todo_in; |
---|
676 | } else { |
---|
677 | roar_buffer_set_len(buf, 0); // queue for deletation |
---|
678 | } |
---|
679 | |
---|
680 | // we now have 'len' bytes in 'in' |
---|
681 | |
---|
682 | // calc how much outlen this has... |
---|
683 | outlen = (len * mul) / div; |
---|
684 | |
---|
685 | ROAR_DBG("streams_fill_mixbuffer(*): outlen = %i, buf = %p, len = %i", outlen, in, len); |
---|
686 | |
---|
687 | if ( is_the_same ) { |
---|
688 | /* |
---|
689 | * 0) convert endianness (codec) from remote to local... |
---|
690 | * 1) change bits in of the samples |
---|
691 | * 2) change sample rate |
---|
692 | * 3) change the nummber of channels |
---|
693 | \\==> skiping,... |
---|
694 | */ |
---|
695 | // * 4) insert into output buffer |
---|
696 | ROAR_DBG("streams_fill_mixbuffer(*): memcpy: in->rest: %p -> %p", in, rest); |
---|
697 | if ( memcpy(rest, in, len) != rest ) { |
---|
698 | ROAR_ERR("streams_fill_mixbuffer(*): memcpy returned invalid pointer."); |
---|
699 | } |
---|
700 | |
---|
701 | } else { |
---|
702 | |
---|
703 | /* |
---|
704 | // * 0) convert endianness (codec) from remote to local... |
---|
705 | if ( stream_info->codec != info->codec ) { |
---|
706 | // we neet to convert... |
---|
707 | return -1; |
---|
708 | } |
---|
709 | |
---|
710 | // * 1) change bits in of the samples |
---|
711 | if ( stream_info->bits != info->bits ) { |
---|
712 | return -1; |
---|
713 | } |
---|
714 | |
---|
715 | // * 2) change sample rate |
---|
716 | if ( stream_info->rate != info->rate ) { |
---|
717 | return -1; |
---|
718 | } |
---|
719 | |
---|
720 | // * 3) change the nummber of channels |
---|
721 | if ( stream_info->channels != info->channels ) { |
---|
722 | return -1; |
---|
723 | } |
---|
724 | |
---|
725 | // * 4) insert into output buffer |
---|
726 | */ |
---|
727 | // hey! we have roar_conv() :) |
---|
728 | |
---|
729 | if ( roar_conv(rest, in, 8*len / stream_info->bits, stream_info, info) == -1 ) |
---|
730 | return -1; |
---|
731 | } |
---|
732 | |
---|
733 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) { |
---|
734 | if ( change_vol(rest, info->bits, rest, 8*outlen / info->bits, info->channels, &(stream->mixer)) == -1 ) |
---|
735 | return -1; |
---|
736 | } |
---|
737 | |
---|
738 | // we habe outlen bytes more... |
---|
739 | todo -= outlen; |
---|
740 | rest += outlen; |
---|
741 | todo_in -= len; |
---|
742 | |
---|
743 | roar_buffer_get_len(buf, &len); |
---|
744 | ROAR_DBG("streams_fill_mixbuffer(*): New length of buffer %p is %i", buf, len); |
---|
745 | if ( len == 0 ) { |
---|
746 | roar_buffer_delete(buf, NULL); |
---|
747 | } else { |
---|
748 | stream_unshift_buffer(id, buf); |
---|
749 | } |
---|
750 | } |
---|
751 | |
---|
752 | //len = 0; |
---|
753 | //roar_buffer_get_len(buf, &len); |
---|
754 | |
---|
755 | /* |
---|
756 | if ( len > 0 ) // we still have some data in this buffer, re-inserting it to the input buffers... |
---|
757 | stream_unshift_buffer(id, buf); |
---|
758 | else |
---|
759 | buffer_delete(buf, NULL); |
---|
760 | */ |
---|
761 | |
---|
762 | ROAR_STREAM(g_streams[id])->pos = |
---|
763 | ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(g_streams[id])->pos, |
---|
764 | ROAR_OUTPUT_CALC_OUTBUFSAMP(info, needed-todo)); |
---|
765 | //ROAR_WARN("stream=%i, pos=%u", id, ((struct roar_stream*)g_streams[id])->pos); |
---|
766 | |
---|
767 | if ( todo > 0 ) { // zeroize the rest of the buffer |
---|
768 | memset(rest, 0, todo); |
---|
769 | |
---|
770 | if ( todo != ROAR_OUTPUT_CALC_OUTBUFSIZE(info) ) { |
---|
771 | if ( g_streams[id]->is_new ) { |
---|
772 | stream->pre_underruns++; |
---|
773 | } else { |
---|
774 | ROAR_WARN("streams_fill_mixbuffer(*): Underrun in stream: %u bytes missing, filling with zeros", (unsigned int)todo); |
---|
775 | stream->post_underruns++; |
---|
776 | } |
---|
777 | |
---|
778 | stream->is_new = 0; |
---|
779 | } |
---|
780 | } else { |
---|
781 | stream->is_new = 0; |
---|
782 | } |
---|
783 | |
---|
784 | return 0; |
---|
785 | } |
---|
786 | |
---|
787 | |
---|
788 | int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) { |
---|
789 | static void * bufs[ROAR_STREAMS_MAX+1]; |
---|
790 | int i; |
---|
791 | int have = 0; |
---|
792 | |
---|
793 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
794 | if ( g_streams[i] != NULL ) { |
---|
795 | if ( ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_PLAY && ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_BIDIR ) |
---|
796 | continue; |
---|
797 | |
---|
798 | if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) { |
---|
799 | ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i); |
---|
800 | ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round."); |
---|
801 | continue; |
---|
802 | } |
---|
803 | if ( streams_fill_mixbuffer(i, info) == -1 ) { |
---|
804 | ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i); |
---|
805 | continue; |
---|
806 | } |
---|
807 | |
---|
808 | // printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
809 | |
---|
810 | ROAR_DBG("streams_get_mixbuffers(*): bufs[have] = %p", bufs[have]); |
---|
811 | ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]); |
---|
812 | |
---|
813 | if ( !streams_get_flag(i, ROAR_FLAG_MUTE) ) |
---|
814 | have++; // we have a new stream! |
---|
815 | } |
---|
816 | } |
---|
817 | |
---|
818 | bufs[have] = NULL; |
---|
819 | //printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
820 | |
---|
821 | ROAR_DBG("streams_get_mixbuffers(*): have = %i", have); |
---|
822 | |
---|
823 | *bufferlist = bufs; |
---|
824 | return have; |
---|
825 | } |
---|
826 | |
---|
827 | |
---|
828 | int stream_add_buffer (int id, struct roar_buffer * buf) { |
---|
829 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
830 | |
---|
831 | if ( g_streams[id] == NULL ) |
---|
832 | return -1; |
---|
833 | |
---|
834 | if ( g_streams[id]->buffer == NULL ) { |
---|
835 | g_streams[id]->buffer = buf; |
---|
836 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf); |
---|
837 | return 0; |
---|
838 | } |
---|
839 | |
---|
840 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
841 | return roar_buffer_add(g_streams[id]->buffer, buf); |
---|
842 | } |
---|
843 | |
---|
844 | int stream_shift_buffer (int id, struct roar_buffer ** buf) { |
---|
845 | struct roar_buffer * next; |
---|
846 | |
---|
847 | if ( g_streams[id] == NULL ) |
---|
848 | return -1; |
---|
849 | |
---|
850 | if ( g_streams[id]->buffer == NULL ) { |
---|
851 | *buf = NULL; |
---|
852 | return 0; |
---|
853 | } |
---|
854 | |
---|
855 | roar_buffer_get_next(g_streams[id]->buffer, &next); |
---|
856 | |
---|
857 | *buf = g_streams[id]->buffer; |
---|
858 | g_streams[id]->buffer = next; |
---|
859 | |
---|
860 | return 0; |
---|
861 | } |
---|
862 | int stream_unshift_buffer (int id, struct roar_buffer * buf) { |
---|
863 | if ( g_streams[id] == NULL ) |
---|
864 | return -1; |
---|
865 | |
---|
866 | if ( g_streams[id]->buffer == NULL ) { |
---|
867 | g_streams[id]->buffer = buf; |
---|
868 | return 0; |
---|
869 | } |
---|
870 | |
---|
871 | buf->next = NULL; |
---|
872 | |
---|
873 | roar_buffer_add(buf, g_streams[id]->buffer); |
---|
874 | |
---|
875 | g_streams[id]->buffer = buf; |
---|
876 | |
---|
877 | return 0; |
---|
878 | } |
---|
879 | |
---|
880 | int streams_check (int id) { |
---|
881 | int fh; |
---|
882 | int i; |
---|
883 | ssize_t req, realreq, done; |
---|
884 | struct roar_stream * s; |
---|
885 | struct roar_stream_server * ss; |
---|
886 | struct roar_buffer * b; |
---|
887 | char * buf; |
---|
888 | |
---|
889 | if ( g_streams[id] == NULL ) |
---|
890 | return -1; |
---|
891 | |
---|
892 | ROAR_DBG("streams_check(id=%i) = ?", id); |
---|
893 | |
---|
894 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
895 | |
---|
896 | if ( (fh = s->fh) == -1 ) |
---|
897 | return 0; |
---|
898 | |
---|
899 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
900 | return 0; |
---|
901 | |
---|
902 | switch (s->dir) { |
---|
903 | case ROAR_DIR_LIGHT_IN: |
---|
904 | return light_check_stream(id); |
---|
905 | break; |
---|
906 | case ROAR_DIR_MIDI_IN: |
---|
907 | return midi_check_stream(id); |
---|
908 | break; |
---|
909 | case ROAR_DIR_PLAY: |
---|
910 | case ROAR_DIR_BIDIR: |
---|
911 | break; |
---|
912 | default: |
---|
913 | return 0; |
---|
914 | break; |
---|
915 | } |
---|
916 | |
---|
917 | ROAR_DBG("streams_check(id=%i): fh = %i", id, fh); |
---|
918 | |
---|
919 | req = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size |
---|
920 | req += ss->need_extra; // bytes left we sould get.... |
---|
921 | |
---|
922 | if ( roar_buffer_new(&b, req) == -1 ) { |
---|
923 | ROAR_ERR("streams_check(*): Can not alloc buffer space!"); |
---|
924 | ROAR_DBG("streams_check(*) = -1"); |
---|
925 | return -1; |
---|
926 | } |
---|
927 | |
---|
928 | roar_buffer_get_data(b, (void **)&buf); |
---|
929 | |
---|
930 | ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id); |
---|
931 | |
---|
932 | if ( ss->codecfilter == -1 ) { |
---|
933 | realreq = req; |
---|
934 | /* |
---|
935 | req = read(fh, buf, req); |
---|
936 | if ( req < realreq ) { // we can do this as the stream is in nonblocking mode! |
---|
937 | if ( (realreq = read(fh, buf+req, realreq-req)) > 0 ) |
---|
938 | req += realreq; |
---|
939 | } |
---|
940 | */ |
---|
941 | done = 0; |
---|
942 | while (req > 0 && done != realreq) { |
---|
943 | if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 ) |
---|
944 | done += req; |
---|
945 | } |
---|
946 | req = done; |
---|
947 | |
---|
948 | roar_buffer_get_data(b, (void **)&buf); |
---|
949 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
950 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) { |
---|
951 | if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) { |
---|
952 | if ( stream_vio_write(i, buf, req) != req ) { |
---|
953 | streams_delete(i); |
---|
954 | } |
---|
955 | } |
---|
956 | } |
---|
957 | } |
---|
958 | } else { |
---|
959 | req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req); |
---|
960 | } |
---|
961 | |
---|
962 | if ( req > 0 ) { |
---|
963 | ROAR_DBG("streams_check(id=%i): got %i bytes", id, req); |
---|
964 | |
---|
965 | roar_buffer_set_len(b, req); |
---|
966 | |
---|
967 | if ( stream_add_buffer(id, b) != -1 ) |
---|
968 | return 0; |
---|
969 | |
---|
970 | ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id); |
---|
971 | roar_buffer_free(b); |
---|
972 | } else { |
---|
973 | ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno)); |
---|
974 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
975 | if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :) |
---|
976 | #else |
---|
977 | if ( errno != EAGAIN ) { |
---|
978 | #endif |
---|
979 | ROAR_DBG("streams_check(id=%i): EOF!", id); |
---|
980 | streams_delete(id); |
---|
981 | ROAR_DBG("streams_check(id=%i) = 0", id); |
---|
982 | } |
---|
983 | roar_buffer_free(b); |
---|
984 | return 0; |
---|
985 | } |
---|
986 | |
---|
987 | |
---|
988 | ROAR_DBG("streams_check(id=%i) = -1", id); |
---|
989 | return -1; |
---|
990 | } |
---|
991 | |
---|
992 | |
---|
993 | int streams_send_mon (int id) { |
---|
994 | // int fh; |
---|
995 | struct roar_stream * s; |
---|
996 | struct roar_stream_server * ss; |
---|
997 | void * obuf; |
---|
998 | int olen; |
---|
999 | int need_to_free = 0; |
---|
1000 | ssize_t ret; |
---|
1001 | |
---|
1002 | if ( g_streams[id] == NULL ) |
---|
1003 | return -1; |
---|
1004 | |
---|
1005 | ROAR_DBG("streams_send_mon(id=%i) = ?", id); |
---|
1006 | |
---|
1007 | s = ROAR_STREAM((ss = g_streams[id])); |
---|
1008 | |
---|
1009 | /* |
---|
1010 | if ( (fh = s->fh) == -1 ) |
---|
1011 | return 0; |
---|
1012 | */ |
---|
1013 | |
---|
1014 | |
---|
1015 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
1016 | return 0; |
---|
1017 | |
---|
1018 | switch (s->dir) { |
---|
1019 | case ROAR_DIR_LIGHT_OUT: |
---|
1020 | return light_send_stream(id); |
---|
1021 | break; |
---|
1022 | case ROAR_DIR_MIDI_OUT: |
---|
1023 | return midi_send_stream(id); |
---|
1024 | break; |
---|
1025 | case ROAR_DIR_OUTPUT: |
---|
1026 | if ( g_standby ) |
---|
1027 | return 0; |
---|
1028 | case ROAR_DIR_MONITOR: |
---|
1029 | case ROAR_DIR_BIDIR: |
---|
1030 | break; |
---|
1031 | |
---|
1032 | default: |
---|
1033 | return 0; |
---|
1034 | break; |
---|
1035 | } |
---|
1036 | |
---|
1037 | |
---|
1038 | ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, s->fh); |
---|
1039 | |
---|
1040 | if ( s->info.channels != g_sa->channels || s->info.bits != g_sa->bits || |
---|
1041 | s->info.rate != g_sa->rate || s->info.codec != g_sa->codec ) { |
---|
1042 | olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len |
---|
1043 | // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here |
---|
1044 | if ( (obuf = malloc(olen)) == NULL ) |
---|
1045 | return -1; |
---|
1046 | |
---|
1047 | need_to_free = 1; |
---|
1048 | |
---|
1049 | ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen); |
---|
1050 | |
---|
1051 | if ( roar_conv(obuf, g_output_buffer, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) { |
---|
1052 | free(obuf); |
---|
1053 | return -1; |
---|
1054 | } |
---|
1055 | } else { |
---|
1056 | obuf = g_output_buffer; |
---|
1057 | olen = g_output_buffer_len; |
---|
1058 | } |
---|
1059 | |
---|
1060 | errno = 0; |
---|
1061 | |
---|
1062 | if ( ss->codecfilter == -1 ) { |
---|
1063 | ROAR_DBG("streams_send_mon(id=%i): not a CF stream", id); |
---|
1064 | if ( s->fh == -1 && roar_vio_get_fh(&(ss->vio)) == -1 ) |
---|
1065 | return 0; |
---|
1066 | |
---|
1067 | if ( (ret = stream_vio_s_write(ss, obuf, olen)) == olen ) { |
---|
1068 | if ( need_to_free ) free(obuf); |
---|
1069 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
1070 | return 0; |
---|
1071 | } |
---|
1072 | |
---|
1073 | if ( ret > 0 && errno == 0 ) { |
---|
1074 | 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); |
---|
1075 | if ( need_to_free ) free(obuf); |
---|
1076 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), ret)*s->info.channels); |
---|
1077 | return 0; |
---|
1078 | } |
---|
1079 | } else { |
---|
1080 | errno = 0; |
---|
1081 | if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen) |
---|
1082 | == olen ) { |
---|
1083 | if ( need_to_free ) free(obuf); |
---|
1084 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
1085 | return 0; |
---|
1086 | } else { // we cann't retry on codec filetered streams |
---|
1087 | if ( errno != EAGAIN ) { |
---|
1088 | if ( need_to_free ) free(obuf); |
---|
1089 | streams_delete(id); |
---|
1090 | return -1; |
---|
1091 | } |
---|
1092 | } |
---|
1093 | } |
---|
1094 | |
---|
1095 | if ( errno == EAGAIN ) { |
---|
1096 | // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to |
---|
1097 | // make any gapes in any output because of this |
---|
1098 | |
---|
1099 | #ifdef ROAR_HAVE_USLEEP |
---|
1100 | usleep(100); // 0.1ms |
---|
1101 | #endif |
---|
1102 | |
---|
1103 | if ( stream_vio_s_write(ss, obuf, olen) == olen ) { |
---|
1104 | if ( need_to_free ) free(obuf); |
---|
1105 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
1106 | return 0; |
---|
1107 | } else if ( errno == EAGAIN ) { |
---|
1108 | ROAR_WARN("streams_send_mon(id=%i): Can not send data to client: %s", id, strerror(errno)); |
---|
1109 | return 0; |
---|
1110 | } |
---|
1111 | } |
---|
1112 | |
---|
1113 | // ug... error... delete stream! |
---|
1114 | |
---|
1115 | if ( need_to_free ) free(obuf); |
---|
1116 | streams_delete(id); |
---|
1117 | |
---|
1118 | return -1; |
---|
1119 | } |
---|
1120 | |
---|
1121 | int streams_send_filter(int id) { |
---|
1122 | int fh; |
---|
1123 | int have = 0; |
---|
1124 | int len; |
---|
1125 | struct roar_stream * s; |
---|
1126 | struct roar_stream_server * ss; |
---|
1127 | |
---|
1128 | if ( g_streams[id] == NULL ) |
---|
1129 | return -1; |
---|
1130 | |
---|
1131 | ROAR_DBG("streams_send_filter(id=%i) = ?", id); |
---|
1132 | |
---|
1133 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
1134 | |
---|
1135 | if ( (fh = s->fh) == -1 ) |
---|
1136 | return 0; |
---|
1137 | |
---|
1138 | if ( s->dir != ROAR_DIR_FILTER ) |
---|
1139 | return 0; |
---|
1140 | |
---|
1141 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
1142 | return 0; |
---|
1143 | |
---|
1144 | |
---|
1145 | ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh); |
---|
1146 | |
---|
1147 | if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) { |
---|
1148 | while ( have < g_output_buffer_len ) { |
---|
1149 | if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) { |
---|
1150 | streams_delete(id); |
---|
1151 | return -1; |
---|
1152 | } |
---|
1153 | have += len; |
---|
1154 | } |
---|
1155 | return 0; |
---|
1156 | } |
---|
1157 | |
---|
1158 | // ug... error... delete stream! |
---|
1159 | |
---|
1160 | streams_delete(id); |
---|
1161 | |
---|
1162 | return -1; |
---|
1163 | } |
---|
1164 | |
---|
1165 | |
---|
1166 | // VIO: |
---|
1167 | |
---|
1168 | ssize_t stream_vio_read (int stream, void *buf, size_t count) { |
---|
1169 | struct roar_stream_server * s = g_streams[stream]; |
---|
1170 | |
---|
1171 | if ( !s ) |
---|
1172 | return -1; |
---|
1173 | |
---|
1174 | return stream_vio_s_read(s, buf, count); |
---|
1175 | } |
---|
1176 | |
---|
1177 | ssize_t stream_vio_write(int stream, void *buf, size_t count) { |
---|
1178 | struct roar_stream_server * s = g_streams[stream]; |
---|
1179 | |
---|
1180 | if ( !s ) |
---|
1181 | return -1; |
---|
1182 | |
---|
1183 | return stream_vio_s_write(s, buf, count); |
---|
1184 | } |
---|
1185 | |
---|
1186 | |
---|
1187 | ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) { |
---|
1188 | size_t len = 0; |
---|
1189 | ssize_t r = -1; |
---|
1190 | |
---|
1191 | errno = 0; |
---|
1192 | |
---|
1193 | if ( !stream ) |
---|
1194 | return -1; |
---|
1195 | |
---|
1196 | //roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
1197 | |
---|
1198 | if ( ! stream->vio.read ) |
---|
1199 | return -1; |
---|
1200 | |
---|
1201 | while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) { |
---|
1202 | len += r; |
---|
1203 | buf += r; |
---|
1204 | count -= r; |
---|
1205 | if ( count == 0 ) |
---|
1206 | break; |
---|
1207 | } |
---|
1208 | |
---|
1209 | if ( len == 0 && r == -1 ) |
---|
1210 | return -1; |
---|
1211 | |
---|
1212 | return len; |
---|
1213 | } |
---|
1214 | |
---|
1215 | ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) { |
---|
1216 | errno = 0; |
---|
1217 | |
---|
1218 | if ( !stream ) |
---|
1219 | return -1; |
---|
1220 | |
---|
1221 | /* |
---|
1222 | if ( roar_vio_get_fh(&(stream->vio)) == -1 && ROAR_STREAM(stream)->fh != -1 ) |
---|
1223 | roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
1224 | */ |
---|
1225 | |
---|
1226 | // ROAR_WARN("stream_vio_s_write(*): writing..."); |
---|
1227 | |
---|
1228 | return roar_vio_write(&(stream->vio), buf, count); |
---|
1229 | } |
---|
1230 | |
---|
1231 | //ll |
---|