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