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 sreams_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("sreams_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("sreams_calc_delay(id=%i): VIO delay in musec: %i", id, tmp); |
---|
361 | |
---|
362 | d += tmp; |
---|
363 | } |
---|
364 | } |
---|
365 | |
---|
366 | ROAR_DBG("sreams_calc_delay(id=%i): delay in musec: %i", id, d); |
---|
367 | |
---|
368 | ss->delay = d; |
---|
369 | |
---|
370 | return 0; |
---|
371 | } |
---|
372 | |
---|
373 | int streams_get_outputbuffer (int id, void ** buffer, size_t size) { |
---|
374 | if ( g_streams[id] == NULL ) |
---|
375 | return -1; |
---|
376 | |
---|
377 | // output buffer size does never change. |
---|
378 | if ( g_streams[id]->output != NULL ) { |
---|
379 | *buffer = g_streams[id]->output; |
---|
380 | return 0; |
---|
381 | } |
---|
382 | |
---|
383 | if ( (g_streams[id]->output = malloc(size)) == NULL ) { |
---|
384 | ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno)); |
---|
385 | return -1; |
---|
386 | } |
---|
387 | |
---|
388 | *buffer = g_streams[id]->output; |
---|
389 | |
---|
390 | return 0; |
---|
391 | } |
---|
392 | |
---|
393 | int streams_fill_mixbuffer (int id, struct roar_audio_info * info) { |
---|
394 | // TODO: decide: is this the most complex, hacked, un-understadable, |
---|
395 | // un-writeable and even worse: un-readable |
---|
396 | // function in the whole project? |
---|
397 | size_t todo = ROAR_OUTPUT_CALC_OUTBUFSIZE(info); |
---|
398 | size_t needed = todo; |
---|
399 | size_t todo_in; |
---|
400 | size_t len, outlen; |
---|
401 | size_t mul = 1, div = 1; |
---|
402 | void * rest = NULL; |
---|
403 | void * in = NULL; |
---|
404 | struct roar_buffer * buf; |
---|
405 | struct roar_audio_info * stream_info; |
---|
406 | struct roar_stream_server * stream = g_streams[id]; |
---|
407 | int is_the_same = 0; |
---|
408 | |
---|
409 | if ( g_streams[id] == NULL ) |
---|
410 | return -1; |
---|
411 | |
---|
412 | if ( streams_get_outputbuffer(id, &rest, todo) == -1 ) { |
---|
413 | return -1; |
---|
414 | } |
---|
415 | |
---|
416 | if ( rest == NULL ) { |
---|
417 | return -1; |
---|
418 | } |
---|
419 | |
---|
420 | // set up stream_info |
---|
421 | |
---|
422 | stream_info = &(ROAR_STREAM(stream)->info); |
---|
423 | |
---|
424 | // calc todo_in |
---|
425 | todo_in = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info); |
---|
426 | |
---|
427 | // calc mul and div: |
---|
428 | mul = todo / todo_in; |
---|
429 | div = todo_in / todo; |
---|
430 | |
---|
431 | if ( mul == 0 ) { |
---|
432 | mul = 1; |
---|
433 | } else { |
---|
434 | div = 1; |
---|
435 | } |
---|
436 | |
---|
437 | ROAR_DBG("streams_fill_mixbuffer(*): mul=%i, div=%i", mul, div); |
---|
438 | |
---|
439 | ROAR_DBG("streams_fill_mixbuffer(*): rest=%p, todo=%i->%i (in->out)", rest, todo_in, todo); |
---|
440 | // are both (input and output) of same format? |
---|
441 | |
---|
442 | |
---|
443 | ROAR_DBG("streams_fill_mixbuffer(*): stream_info:"); |
---|
444 | roar_debug_audio_info_print(stream_info); |
---|
445 | ROAR_DBG("streams_fill_mixbuffer(*): info:"); |
---|
446 | roar_debug_audio_info_print(info); |
---|
447 | |
---|
448 | is_the_same = stream_info->rate == info->rate && stream_info->bits == info->bits && |
---|
449 | stream_info->channels == info->channels && stream_info->codec == info->codec; |
---|
450 | |
---|
451 | ROAR_DBG("streams_fill_mixbuffer(*): is_the_same=%i", is_the_same); |
---|
452 | |
---|
453 | /* How it works: |
---|
454 | * |
---|
455 | * set a counter to the number of samples we need. |
---|
456 | * loop until we have all samples done or no samples are |
---|
457 | * left in our input buffer. |
---|
458 | * If there a no samples left in the input buffer: fill the rest |
---|
459 | * of the output buffer with zeros. |
---|
460 | * |
---|
461 | * The loop: |
---|
462 | * get a buffer from the input. |
---|
463 | * if it's bigger than needed, set an offset. |
---|
464 | * The rest of the data: |
---|
465 | * 0) convert endianness (codec) from remote to local... |
---|
466 | * 1) change bits in of the samples |
---|
467 | * 2) change sample rate |
---|
468 | * 3) change the nummber of channels |
---|
469 | * 4) insert into output buffer |
---|
470 | */ |
---|
471 | |
---|
472 | /* |
---|
473 | // get our first buffer: |
---|
474 | |
---|
475 | if ( stream_shift_buffer(id, &buf) == -1 ) { |
---|
476 | return -1; |
---|
477 | } |
---|
478 | |
---|
479 | // first test for some basic simple cases... |
---|
480 | |
---|
481 | if ( buf == NULL ) { // we habe nothing in input queue |
---|
482 | // we may memset() our output buffer OR |
---|
483 | // just return with -1 so we are going to |
---|
484 | // be ignored. |
---|
485 | return -1; |
---|
486 | } |
---|
487 | */ |
---|
488 | |
---|
489 | while (todo) { // main loop |
---|
490 | ROAR_DBG("streams_fill_mixbuffer(*): looping..."); |
---|
491 | // exit loop if nothing is left, even if we need more data.. |
---|
492 | if ( stream_shift_buffer(id, &buf) == -1 ) |
---|
493 | break; |
---|
494 | if ( buf == NULL ) |
---|
495 | break; |
---|
496 | |
---|
497 | // read the data for this loop... |
---|
498 | roar_buffer_get_data(buf, &in); |
---|
499 | roar_buffer_get_len(buf, &len); |
---|
500 | |
---|
501 | ROAR_DBG("streams_fill_mixbuffer(*): len = %i", len); |
---|
502 | |
---|
503 | if ( len > todo_in ) { |
---|
504 | roar_buffer_set_offset(buf, todo_in); |
---|
505 | len = todo_in; |
---|
506 | } else { |
---|
507 | roar_buffer_set_len(buf, 0); // queue for deletation |
---|
508 | } |
---|
509 | |
---|
510 | // we now have 'len' bytes in 'in' |
---|
511 | |
---|
512 | // calc how much outlen this has... |
---|
513 | outlen = (len * mul) / div; |
---|
514 | |
---|
515 | ROAR_DBG("streams_fill_mixbuffer(*): outlen = %i, buf = %p, len = %i", outlen, in, len); |
---|
516 | |
---|
517 | if ( is_the_same ) { |
---|
518 | /* |
---|
519 | * 0) convert endianness (codec) from remote to local... |
---|
520 | * 1) change bits in of the samples |
---|
521 | * 2) change sample rate |
---|
522 | * 3) change the nummber of channels |
---|
523 | \\==> skiping,... |
---|
524 | */ |
---|
525 | // * 4) insert into output buffer |
---|
526 | ROAR_DBG("streams_fill_mixbuffer(*): memcpy: in->rest: %p -> %p", in, rest); |
---|
527 | if ( memcpy(rest, in, len) != rest ) { |
---|
528 | ROAR_ERR("streams_fill_mixbuffer(*): memcpy returned invalid pointer."); |
---|
529 | } |
---|
530 | |
---|
531 | } else { |
---|
532 | |
---|
533 | /* |
---|
534 | // * 0) convert endianness (codec) from remote to local... |
---|
535 | if ( stream_info->codec != info->codec ) { |
---|
536 | // we neet to convert... |
---|
537 | return -1; |
---|
538 | } |
---|
539 | |
---|
540 | // * 1) change bits in of the samples |
---|
541 | if ( stream_info->bits != info->bits ) { |
---|
542 | return -1; |
---|
543 | } |
---|
544 | |
---|
545 | // * 2) change sample rate |
---|
546 | if ( stream_info->rate != info->rate ) { |
---|
547 | return -1; |
---|
548 | } |
---|
549 | |
---|
550 | // * 3) change the nummber of channels |
---|
551 | if ( stream_info->channels != info->channels ) { |
---|
552 | return -1; |
---|
553 | } |
---|
554 | |
---|
555 | // * 4) insert into output buffer |
---|
556 | */ |
---|
557 | // hey! we have roar_conv() :) |
---|
558 | |
---|
559 | if ( roar_conv(rest, in, 8*len / stream_info->bits, stream_info, info) == -1 ) |
---|
560 | return -1; |
---|
561 | } |
---|
562 | |
---|
563 | if ( change_vol(rest, info->bits, rest, 8*outlen / info->bits, info->channels, &(stream->mixer)) == -1 ) |
---|
564 | return -1; |
---|
565 | |
---|
566 | // we habe outlen bytes more... |
---|
567 | todo -= outlen; |
---|
568 | rest += outlen; |
---|
569 | todo_in -= len; |
---|
570 | |
---|
571 | roar_buffer_get_len(buf, &len); |
---|
572 | ROAR_DBG("streams_fill_mixbuffer(*): New length of buffer %p is %i", buf, len); |
---|
573 | if ( len == 0 ) { |
---|
574 | roar_buffer_delete(buf, NULL); |
---|
575 | } else { |
---|
576 | stream_unshift_buffer(id, buf); |
---|
577 | } |
---|
578 | } |
---|
579 | |
---|
580 | //len = 0; |
---|
581 | //roar_buffer_get_len(buf, &len); |
---|
582 | |
---|
583 | /* |
---|
584 | if ( len > 0 ) // we still have some data in this buffer, re-inserting it to the input buffers... |
---|
585 | stream_unshift_buffer(id, buf); |
---|
586 | else |
---|
587 | buffer_delete(buf, NULL); |
---|
588 | */ |
---|
589 | |
---|
590 | ROAR_STREAM(g_streams[id])->pos = |
---|
591 | ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(g_streams[id])->pos, |
---|
592 | ROAR_OUTPUT_CALC_OUTBUFSAMP(info, needed-todo)); |
---|
593 | //ROAR_WARN("stream=%i, pos=%u", id, ((struct roar_stream*)g_streams[id])->pos); |
---|
594 | |
---|
595 | if ( todo > 0 ) { // zeroize the rest of the buffer |
---|
596 | memset(rest, 0, todo); |
---|
597 | |
---|
598 | if ( todo != ROAR_OUTPUT_CALC_OUTBUFSIZE(info) ) { |
---|
599 | if ( g_streams[id]->is_new ) { |
---|
600 | stream->pre_underruns++; |
---|
601 | } else { |
---|
602 | ROAR_WARN("streams_fill_mixbuffer(*): Underrun in stream: %u bytes missing, filling with zeros", (unsigned int)todo); |
---|
603 | stream->post_underruns++; |
---|
604 | } |
---|
605 | |
---|
606 | stream->is_new = 0; |
---|
607 | } |
---|
608 | } else { |
---|
609 | stream->is_new = 0; |
---|
610 | } |
---|
611 | |
---|
612 | return 0; |
---|
613 | } |
---|
614 | |
---|
615 | |
---|
616 | int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) { |
---|
617 | static void * bufs[ROAR_STREAMS_MAX+1]; |
---|
618 | int i; |
---|
619 | int have = 0; |
---|
620 | |
---|
621 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
622 | if ( g_streams[i] != NULL ) { |
---|
623 | if ( ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_PLAY && ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_BIDIR ) |
---|
624 | continue; |
---|
625 | |
---|
626 | if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) { |
---|
627 | ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i); |
---|
628 | ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round."); |
---|
629 | continue; |
---|
630 | } |
---|
631 | if ( streams_fill_mixbuffer(i, info) == -1 ) { |
---|
632 | ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i); |
---|
633 | continue; |
---|
634 | } |
---|
635 | |
---|
636 | // printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
637 | |
---|
638 | ROAR_DBG("streams_get_mixbuffers(*): bufs[have] = %p", bufs[have]); |
---|
639 | ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]); |
---|
640 | |
---|
641 | have++; // we have a new stream! |
---|
642 | } |
---|
643 | } |
---|
644 | |
---|
645 | bufs[have] = NULL; |
---|
646 | //printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
647 | |
---|
648 | ROAR_DBG("streams_get_mixbuffers(*): have = %i", have); |
---|
649 | |
---|
650 | *bufferlist = bufs; |
---|
651 | return have; |
---|
652 | } |
---|
653 | |
---|
654 | |
---|
655 | int stream_add_buffer (int id, struct roar_buffer * buf) { |
---|
656 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
657 | |
---|
658 | if ( g_streams[id] == NULL ) |
---|
659 | return -1; |
---|
660 | |
---|
661 | if ( g_streams[id]->buffer == NULL ) { |
---|
662 | g_streams[id]->buffer = buf; |
---|
663 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf); |
---|
664 | return 0; |
---|
665 | } |
---|
666 | |
---|
667 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
668 | return roar_buffer_add(g_streams[id]->buffer, buf); |
---|
669 | } |
---|
670 | |
---|
671 | int stream_shift_buffer (int id, struct roar_buffer ** buf) { |
---|
672 | struct roar_buffer * next; |
---|
673 | |
---|
674 | if ( g_streams[id] == NULL ) |
---|
675 | return -1; |
---|
676 | |
---|
677 | if ( g_streams[id]->buffer == NULL ) { |
---|
678 | *buf = NULL; |
---|
679 | return 0; |
---|
680 | } |
---|
681 | |
---|
682 | roar_buffer_get_next(g_streams[id]->buffer, &next); |
---|
683 | |
---|
684 | *buf = g_streams[id]->buffer; |
---|
685 | g_streams[id]->buffer = next; |
---|
686 | |
---|
687 | return 0; |
---|
688 | } |
---|
689 | int stream_unshift_buffer (int id, struct roar_buffer * buf) { |
---|
690 | if ( g_streams[id] == NULL ) |
---|
691 | return -1; |
---|
692 | |
---|
693 | if ( g_streams[id]->buffer == NULL ) { |
---|
694 | g_streams[id]->buffer = buf; |
---|
695 | return 0; |
---|
696 | } |
---|
697 | |
---|
698 | buf->next = NULL; |
---|
699 | |
---|
700 | roar_buffer_add(buf, g_streams[id]->buffer); |
---|
701 | |
---|
702 | g_streams[id]->buffer = buf; |
---|
703 | |
---|
704 | return 0; |
---|
705 | } |
---|
706 | |
---|
707 | int streams_check (int id) { |
---|
708 | int fh; |
---|
709 | ssize_t req, realreq, done; |
---|
710 | struct roar_stream * s; |
---|
711 | struct roar_stream_server * ss; |
---|
712 | struct roar_buffer * b; |
---|
713 | char * buf; |
---|
714 | |
---|
715 | if ( g_streams[id] == NULL ) |
---|
716 | return -1; |
---|
717 | |
---|
718 | ROAR_DBG("streams_check(id=%i) = ?", id); |
---|
719 | |
---|
720 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
721 | |
---|
722 | if ( (fh = s->fh) == -1 ) |
---|
723 | return 0; |
---|
724 | |
---|
725 | if ( s->dir != ROAR_DIR_PLAY && s->dir != ROAR_DIR_BIDIR ) |
---|
726 | return 0; |
---|
727 | |
---|
728 | ROAR_DBG("streams_check(id=%i): fh = %i", id, fh); |
---|
729 | |
---|
730 | req = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size |
---|
731 | req += ss->need_extra; // bytes left we sould get.... |
---|
732 | |
---|
733 | if ( roar_buffer_new(&b, req) == -1 ) { |
---|
734 | ROAR_ERR("streams_check(*): Can not alloc buffer space!"); |
---|
735 | ROAR_DBG("streams_check(*) = -1"); |
---|
736 | return -1; |
---|
737 | } |
---|
738 | |
---|
739 | roar_buffer_get_data(b, (void **)&buf); |
---|
740 | |
---|
741 | ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id); |
---|
742 | |
---|
743 | if ( ss->codecfilter == -1 ) { |
---|
744 | realreq = req; |
---|
745 | /* |
---|
746 | req = read(fh, buf, req); |
---|
747 | if ( req < realreq ) { // we can do this as the stream is in nonblocking mode! |
---|
748 | if ( (realreq = read(fh, buf+req, realreq-req)) > 0 ) |
---|
749 | req += realreq; |
---|
750 | } |
---|
751 | */ |
---|
752 | done = 0; |
---|
753 | while (req > 0 && done != realreq) { |
---|
754 | if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 ) |
---|
755 | done += req; |
---|
756 | } |
---|
757 | req = done; |
---|
758 | } else { |
---|
759 | req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req); |
---|
760 | } |
---|
761 | |
---|
762 | if ( req > 0 ) { |
---|
763 | ROAR_DBG("streams_check(id=%i): got %i bytes", id, req); |
---|
764 | |
---|
765 | roar_buffer_set_len(b, req); |
---|
766 | |
---|
767 | if ( stream_add_buffer(id, b) != -1 ) |
---|
768 | return 0; |
---|
769 | |
---|
770 | ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id); |
---|
771 | roar_buffer_free(b); |
---|
772 | } else { |
---|
773 | ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno)); |
---|
774 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
775 | if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :) |
---|
776 | #else |
---|
777 | if ( errno != EAGAIN ) { |
---|
778 | #endif |
---|
779 | ROAR_DBG("streams_check(id=%i): EOF!", id); |
---|
780 | streams_delete(id); |
---|
781 | ROAR_DBG("streams_check(id=%i) = 0", id); |
---|
782 | } |
---|
783 | roar_buffer_free(b); |
---|
784 | return 0; |
---|
785 | } |
---|
786 | |
---|
787 | |
---|
788 | ROAR_DBG("streams_check(id=%i) = -1", id); |
---|
789 | return -1; |
---|
790 | } |
---|
791 | |
---|
792 | |
---|
793 | int streams_send_mon (int id) { |
---|
794 | // int fh; |
---|
795 | struct roar_stream * s; |
---|
796 | struct roar_stream_server * ss; |
---|
797 | void * obuf; |
---|
798 | int olen; |
---|
799 | int need_to_free = 0; |
---|
800 | ssize_t ret; |
---|
801 | |
---|
802 | if ( g_streams[id] == NULL ) |
---|
803 | return -1; |
---|
804 | |
---|
805 | ROAR_DBG("streams_send_mon(id=%i) = ?", id); |
---|
806 | |
---|
807 | s = ROAR_STREAM((ss = g_streams[id])); |
---|
808 | |
---|
809 | /* |
---|
810 | if ( (fh = s->fh) == -1 ) |
---|
811 | return 0; |
---|
812 | */ |
---|
813 | |
---|
814 | if ( s->dir != ROAR_DIR_MONITOR && s->dir != ROAR_DIR_OUTPUT && s->dir != ROAR_DIR_BIDIR ) |
---|
815 | return 0; |
---|
816 | |
---|
817 | if ( s->dir == ROAR_DIR_OUTPUT && g_standby ) |
---|
818 | return 0; |
---|
819 | |
---|
820 | ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, s->fh); |
---|
821 | |
---|
822 | if ( s->info.channels != g_sa->channels || s->info.bits != g_sa->bits || |
---|
823 | s->info.rate != g_sa->rate || s->info.codec != g_sa->codec ) { |
---|
824 | olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len |
---|
825 | // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here |
---|
826 | if ( (obuf = malloc(olen)) == NULL ) |
---|
827 | return -1; |
---|
828 | |
---|
829 | need_to_free = 1; |
---|
830 | |
---|
831 | ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen); |
---|
832 | |
---|
833 | if ( roar_conv(obuf, g_output_buffer, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) { |
---|
834 | free(obuf); |
---|
835 | return -1; |
---|
836 | } |
---|
837 | } else { |
---|
838 | obuf = g_output_buffer; |
---|
839 | olen = g_output_buffer_len; |
---|
840 | } |
---|
841 | |
---|
842 | errno = 0; |
---|
843 | |
---|
844 | if ( ss->codecfilter == -1 ) { |
---|
845 | ROAR_DBG("streams_send_mon(id=%i): not a CF stream", id); |
---|
846 | if ( s->fh == -1 && roar_vio_get_fh(&(ss->vio)) == -1 ) |
---|
847 | return 0; |
---|
848 | |
---|
849 | if ( (ret = stream_vio_s_write(ss, obuf, olen)) == olen ) { |
---|
850 | if ( need_to_free ) free(obuf); |
---|
851 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
852 | return 0; |
---|
853 | } |
---|
854 | |
---|
855 | if ( ret > 0 && errno == 0 ) { |
---|
856 | ROAR_WARN("streams_send_mon(id=%i): Overrun in stream: wrote %i of %i bytes, %i bytes missing", id, ret, olen, olen-ret); |
---|
857 | if ( need_to_free ) free(obuf); |
---|
858 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), ret)*s->info.channels); |
---|
859 | return 0; |
---|
860 | } |
---|
861 | } else { |
---|
862 | errno = 0; |
---|
863 | if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen) |
---|
864 | == olen ) { |
---|
865 | if ( need_to_free ) free(obuf); |
---|
866 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
867 | return 0; |
---|
868 | } else { // we cann't retry on codec filetered streams |
---|
869 | if ( errno != EAGAIN ) { |
---|
870 | if ( need_to_free ) free(obuf); |
---|
871 | streams_delete(id); |
---|
872 | return -1; |
---|
873 | } |
---|
874 | } |
---|
875 | } |
---|
876 | |
---|
877 | if ( errno == EAGAIN ) { |
---|
878 | // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to |
---|
879 | // make any gapes in any output because of this |
---|
880 | |
---|
881 | usleep(100); // 0.1ms |
---|
882 | |
---|
883 | if ( stream_vio_s_write(ss, obuf, olen) == olen ) { |
---|
884 | if ( need_to_free ) free(obuf); |
---|
885 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
886 | return 0; |
---|
887 | } else if ( errno == EAGAIN ) { |
---|
888 | ROAR_WARN("streams_send_mon(id=%i): Can not send data to client: %s", id, strerror(errno)); |
---|
889 | return 0; |
---|
890 | } |
---|
891 | } |
---|
892 | |
---|
893 | // ug... error... delete stream! |
---|
894 | |
---|
895 | if ( need_to_free ) free(obuf); |
---|
896 | streams_delete(id); |
---|
897 | |
---|
898 | return -1; |
---|
899 | } |
---|
900 | |
---|
901 | int streams_send_filter(int id) { |
---|
902 | int fh; |
---|
903 | int have = 0; |
---|
904 | int len; |
---|
905 | struct roar_stream * s; |
---|
906 | struct roar_stream_server * ss; |
---|
907 | |
---|
908 | if ( g_streams[id] == NULL ) |
---|
909 | return -1; |
---|
910 | |
---|
911 | ROAR_DBG("streams_send_filter(id=%i) = ?", id); |
---|
912 | |
---|
913 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
914 | |
---|
915 | if ( (fh = s->fh) == -1 ) |
---|
916 | return 0; |
---|
917 | |
---|
918 | if ( s->dir != ROAR_DIR_FILTER ) |
---|
919 | return 0; |
---|
920 | |
---|
921 | ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh); |
---|
922 | |
---|
923 | if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) { |
---|
924 | while ( have < g_output_buffer_len ) { |
---|
925 | if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) { |
---|
926 | streams_delete(id); |
---|
927 | return -1; |
---|
928 | } |
---|
929 | have += len; |
---|
930 | } |
---|
931 | return 0; |
---|
932 | } |
---|
933 | |
---|
934 | // ug... error... delete stream! |
---|
935 | |
---|
936 | streams_delete(id); |
---|
937 | |
---|
938 | return -1; |
---|
939 | } |
---|
940 | |
---|
941 | |
---|
942 | // VIO: |
---|
943 | |
---|
944 | ssize_t stream_vio_read (int stream, void *buf, size_t count) { |
---|
945 | struct roar_stream_server * s = g_streams[stream]; |
---|
946 | |
---|
947 | if ( !s ) |
---|
948 | return -1; |
---|
949 | |
---|
950 | return stream_vio_s_read(s, buf, count); |
---|
951 | } |
---|
952 | |
---|
953 | ssize_t stream_vio_write(int stream, void *buf, size_t count) { |
---|
954 | struct roar_stream_server * s = g_streams[stream]; |
---|
955 | |
---|
956 | if ( !s ) |
---|
957 | return -1; |
---|
958 | |
---|
959 | return stream_vio_s_write(s, buf, count); |
---|
960 | } |
---|
961 | |
---|
962 | |
---|
963 | ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) { |
---|
964 | size_t len = 0; |
---|
965 | ssize_t r = -1; |
---|
966 | |
---|
967 | errno = 0; |
---|
968 | |
---|
969 | if ( !stream ) |
---|
970 | return -1; |
---|
971 | |
---|
972 | roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
973 | |
---|
974 | if ( ! stream->vio.read ) |
---|
975 | return -1; |
---|
976 | |
---|
977 | while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) { |
---|
978 | len += r; |
---|
979 | buf += r; |
---|
980 | count -= r; |
---|
981 | if ( count == 0 ) |
---|
982 | break; |
---|
983 | } |
---|
984 | |
---|
985 | if ( len == 0 && r == -1 ) |
---|
986 | return -1; |
---|
987 | |
---|
988 | return len; |
---|
989 | } |
---|
990 | |
---|
991 | ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) { |
---|
992 | errno = 0; |
---|
993 | |
---|
994 | if ( !stream ) |
---|
995 | return -1; |
---|
996 | |
---|
997 | if ( roar_vio_get_fh(&(stream->vio)) == -1 && ROAR_STREAM(stream)->fh != -1 ) |
---|
998 | roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
999 | |
---|
1000 | // ROAR_WARN("stream_vio_s_write(*): writing..."); |
---|
1001 | |
---|
1002 | return roar_vio_write(&(stream->vio), buf, count); |
---|
1003 | } |
---|
1004 | |
---|
1005 | //ll |
---|