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