1 | //streams.c: |
---|
2 | |
---|
3 | #include "roard.h" |
---|
4 | |
---|
5 | int streams_init (void) { |
---|
6 | int i; |
---|
7 | |
---|
8 | for (i = 0; i < ROAR_STREAMS_MAX; i++) |
---|
9 | g_streams[i] = NULL; |
---|
10 | |
---|
11 | return 0; |
---|
12 | } |
---|
13 | |
---|
14 | int streams_free (void) { |
---|
15 | int i; |
---|
16 | |
---|
17 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
18 | if ( g_streams[i] != NULL ) { |
---|
19 | streams_delete(i); |
---|
20 | } |
---|
21 | } |
---|
22 | |
---|
23 | return 0; |
---|
24 | } |
---|
25 | |
---|
26 | |
---|
27 | int streams_new (void) { |
---|
28 | int i, j; |
---|
29 | struct roar_stream * n = NULL; |
---|
30 | struct roar_stream_server * s = NULL; |
---|
31 | |
---|
32 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
33 | if ( g_streams[i] == NULL ) { |
---|
34 | s = ROAR_STREAM_SERVER(n = ROAR_STREAM(malloc(sizeof(struct roar_stream_server)))); |
---|
35 | if ( n == NULL ) { |
---|
36 | ROAR_ERR("streams_new(void): can not allocate memory for new stream: %s", strerror(errno)); |
---|
37 | ROAR_DBG("streams_new(void) = -1"); |
---|
38 | return -1; |
---|
39 | } |
---|
40 | |
---|
41 | n->id = i; |
---|
42 | n->fh = -1; |
---|
43 | // n->pos_rel_id = i; |
---|
44 | n->database = NULL; |
---|
45 | n->dataoff = NULL; |
---|
46 | n->datalen = 0; |
---|
47 | n->offset = 0; |
---|
48 | n->pos = 0; |
---|
49 | |
---|
50 | s->client = -1; |
---|
51 | s->socktype = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
52 | s->buffer = NULL; |
---|
53 | s->need_extra = 0; |
---|
54 | s->output = NULL; |
---|
55 | s->is_new = 1; |
---|
56 | s->codecfilter = -1; |
---|
57 | s->pre_underruns = 0; |
---|
58 | s->post_underruns = 0; |
---|
59 | |
---|
60 | s->mixer.scale = 65535; |
---|
61 | s->mixer.rpg_mul = 1; |
---|
62 | s->mixer.rpg_div = 1; |
---|
63 | for (j = 0; j < ROAR_MAX_CHANNELS; j++) |
---|
64 | s->mixer.mixer[j] = 65535; |
---|
65 | |
---|
66 | for (j = 0; j < ROAR_META_MAX_PER_STREAM; j++) { |
---|
67 | s->meta[j].type = ROAR_META_TYPE_NONE; |
---|
68 | s->meta[j].key[0] = 0; |
---|
69 | s->meta[j].value = NULL; |
---|
70 | } |
---|
71 | |
---|
72 | g_streams[i] = s; |
---|
73 | ROAR_DBG("streams_new(void): n->id=%i", n->id); |
---|
74 | ROAR_DBG("streams_new(void) = %i", i); |
---|
75 | return i; |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | return -1; |
---|
80 | } |
---|
81 | |
---|
82 | int streams_delete (int id) { |
---|
83 | if ( g_streams[id] == NULL ) |
---|
84 | return 0; |
---|
85 | |
---|
86 | ROAR_DBG("streams_delete(id=%i) = ?", id); |
---|
87 | ROAR_DBG("streams_delete(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id); |
---|
88 | |
---|
89 | if ( g_streams[id]->codecfilter != -1 ) { |
---|
90 | codecfilter_close(g_streams[id]->codecfilter_inst, g_streams[id]->codecfilter); |
---|
91 | g_streams[id]->codecfilter_inst = NULL; |
---|
92 | g_streams[id]->codecfilter = -1; |
---|
93 | } |
---|
94 | |
---|
95 | if ( g_streams[id]->client != -1 ) { |
---|
96 | ROAR_DBG("streams_delete(id=%i): Stream is owned by client %i", id, g_streams[id]->client); |
---|
97 | client_stream_delete(g_streams[id]->client, id); |
---|
98 | } |
---|
99 | |
---|
100 | if ( g_streams[id]->buffer != NULL ) |
---|
101 | roar_buffer_free(g_streams[id]->buffer); |
---|
102 | |
---|
103 | if ( g_streams[id]->output != NULL ) |
---|
104 | free(g_streams[id]->output); |
---|
105 | |
---|
106 | if ( ROAR_STREAM(g_streams[id])->fh != -1 ) |
---|
107 | close(ROAR_STREAM(g_streams[id])->fh); |
---|
108 | |
---|
109 | free(g_streams[id]); |
---|
110 | |
---|
111 | g_streams[id] = NULL; |
---|
112 | |
---|
113 | ROAR_DBG("streams_delete(id=%i) = 0", id); |
---|
114 | return 0; |
---|
115 | } |
---|
116 | |
---|
117 | int streams_set_client (int id, int client) { |
---|
118 | if ( g_streams[id] == NULL ) |
---|
119 | return -1; |
---|
120 | |
---|
121 | ROAR_DBG("streams_set_client(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id); |
---|
122 | g_streams[id]->client = client; |
---|
123 | |
---|
124 | return 0; |
---|
125 | } |
---|
126 | |
---|
127 | int streams_set_fh (int id, int fh) { |
---|
128 | int dir; |
---|
129 | |
---|
130 | if ( g_streams[id] == NULL ) |
---|
131 | return -1; |
---|
132 | |
---|
133 | ROAR_DBG("streams_set_fh(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id); |
---|
134 | |
---|
135 | ((struct roar_stream *)g_streams[id])->fh = fh; |
---|
136 | |
---|
137 | codecfilter_open(&(g_streams[id]->codecfilter_inst), &(g_streams[id]->codecfilter), NULL, |
---|
138 | ROAR_STREAM(g_streams[id])->info.codec, g_streams[id]); |
---|
139 | |
---|
140 | dir = ROAR_STREAM(g_streams[id])->dir; |
---|
141 | |
---|
142 | if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) { |
---|
143 | shutdown(fh, SHUT_RD); |
---|
144 | } |
---|
145 | |
---|
146 | if ( dir == ROAR_DIR_FILTER ) { |
---|
147 | return 0; |
---|
148 | } else { |
---|
149 | return roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK); |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | int streams_get_fh (int id) { |
---|
154 | if ( id < 0 ) |
---|
155 | return -1; |
---|
156 | |
---|
157 | if ( g_streams[id] == NULL ) |
---|
158 | return -1; |
---|
159 | |
---|
160 | return ((struct roar_stream *)g_streams[id])->fh; |
---|
161 | } |
---|
162 | |
---|
163 | int streams_get (int id, struct roar_stream_server ** stream) { |
---|
164 | if ( g_streams[id] == NULL ) |
---|
165 | return -1; |
---|
166 | |
---|
167 | *stream = g_streams[id]; |
---|
168 | |
---|
169 | return 0; |
---|
170 | } |
---|
171 | |
---|
172 | int streams_set_socktype (int id, int socktype) { |
---|
173 | if ( g_streams[id] == NULL ) |
---|
174 | return -1; |
---|
175 | |
---|
176 | g_streams[id]->socktype = socktype; |
---|
177 | |
---|
178 | return 0; |
---|
179 | } |
---|
180 | |
---|
181 | int streams_get_socktype (int id) { |
---|
182 | if ( g_streams[id] == NULL ) |
---|
183 | return -1; |
---|
184 | |
---|
185 | return g_streams[id]->socktype; |
---|
186 | } |
---|
187 | |
---|
188 | int streams_get_outputbuffer (int id, void ** buffer, size_t size) { |
---|
189 | if ( g_streams[id] == NULL ) |
---|
190 | return -1; |
---|
191 | |
---|
192 | // output buffer size does never change. |
---|
193 | if ( g_streams[id]->output != NULL ) { |
---|
194 | *buffer = g_streams[id]->output; |
---|
195 | return 0; |
---|
196 | } |
---|
197 | |
---|
198 | if ( (g_streams[id]->output = malloc(size)) == NULL ) { |
---|
199 | ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno)); |
---|
200 | return -1; |
---|
201 | } |
---|
202 | |
---|
203 | *buffer = g_streams[id]->output; |
---|
204 | |
---|
205 | return 0; |
---|
206 | } |
---|
207 | |
---|
208 | int streams_fill_mixbuffer (int id, struct roar_audio_info * info) { |
---|
209 | // TODO: decide: is this the most complex, hacked, un-understadable, |
---|
210 | // un-writeable and even worse: un-readable |
---|
211 | // function in the whole project? |
---|
212 | size_t todo = ROAR_OUTPUT_CALC_OUTBUFSIZE(info); |
---|
213 | size_t needed = todo; |
---|
214 | size_t todo_in; |
---|
215 | size_t len, outlen; |
---|
216 | size_t mul = 1, div = 1; |
---|
217 | void * rest = NULL; |
---|
218 | void * in = NULL; |
---|
219 | struct roar_buffer * buf; |
---|
220 | struct roar_audio_info * stream_info; |
---|
221 | struct roar_stream_server * stream = g_streams[id]; |
---|
222 | int is_the_same = 0; |
---|
223 | |
---|
224 | if ( g_streams[id] == NULL ) |
---|
225 | return -1; |
---|
226 | |
---|
227 | if ( streams_get_outputbuffer(id, &rest, todo) == -1 ) { |
---|
228 | return -1; |
---|
229 | } |
---|
230 | |
---|
231 | if ( rest == NULL ) { |
---|
232 | return -1; |
---|
233 | } |
---|
234 | |
---|
235 | // set up stream_info |
---|
236 | |
---|
237 | stream_info = &(ROAR_STREAM(stream)->info); |
---|
238 | |
---|
239 | // calc todo_in |
---|
240 | todo_in = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info); |
---|
241 | |
---|
242 | // calc mul and div: |
---|
243 | mul = todo / todo_in; |
---|
244 | div = todo_in / todo; |
---|
245 | |
---|
246 | if ( mul == 0 ) { |
---|
247 | mul = 1; |
---|
248 | } else { |
---|
249 | div = 1; |
---|
250 | } |
---|
251 | |
---|
252 | ROAR_DBG("streams_fill_mixbuffer(*): mul=%i, div=%i", mul, div); |
---|
253 | |
---|
254 | ROAR_DBG("streams_fill_mixbuffer(*): rest=%p, todo=%i->%i (in->out)", rest, todo_in, todo); |
---|
255 | // are both (input and output) of same format? |
---|
256 | |
---|
257 | |
---|
258 | ROAR_DBG("streams_fill_mixbuffer(*): stream_info:"); |
---|
259 | roar_debug_audio_info_print(stream_info); |
---|
260 | ROAR_DBG("streams_fill_mixbuffer(*): info:"); |
---|
261 | roar_debug_audio_info_print(info); |
---|
262 | |
---|
263 | is_the_same = stream_info->rate == info->rate && stream_info->bits == info->bits && |
---|
264 | stream_info->channels == info->channels && stream_info->codec == info->codec; |
---|
265 | |
---|
266 | ROAR_DBG("streams_fill_mixbuffer(*): is_the_same=%i", is_the_same); |
---|
267 | |
---|
268 | /* How it works: |
---|
269 | * |
---|
270 | * set a counter to the number of samples we need. |
---|
271 | * loop until we have all samples done or no samples are |
---|
272 | * left in our input buffer. |
---|
273 | * If there a no samples left in the input buffer: fill the rest |
---|
274 | * of the output buffer with zeros. |
---|
275 | * |
---|
276 | * The loop: |
---|
277 | * get a buffer from the input. |
---|
278 | * if it's bigger than needed, set an offset. |
---|
279 | * The rest of the data: |
---|
280 | * 0) convert endianness (codec) from remote to local... |
---|
281 | * 1) change bits in of the samples |
---|
282 | * 2) change sample rate |
---|
283 | * 3) change the nummber of channels |
---|
284 | * 4) insert into output buffer |
---|
285 | */ |
---|
286 | |
---|
287 | /* |
---|
288 | // get our first buffer: |
---|
289 | |
---|
290 | if ( stream_shift_buffer(id, &buf) == -1 ) { |
---|
291 | return -1; |
---|
292 | } |
---|
293 | |
---|
294 | // first test for some basic simple cases... |
---|
295 | |
---|
296 | if ( buf == NULL ) { // we habe nothing in input queue |
---|
297 | // we may memset() our output buffer OR |
---|
298 | // just return with -1 so we are going to |
---|
299 | // be ignored. |
---|
300 | return -1; |
---|
301 | } |
---|
302 | */ |
---|
303 | |
---|
304 | while (todo) { // main loop |
---|
305 | ROAR_DBG("streams_fill_mixbuffer(*): looping..."); |
---|
306 | // exit loop if nothing is left, even if we need more data.. |
---|
307 | if ( stream_shift_buffer(id, &buf) == -1 ) |
---|
308 | break; |
---|
309 | if ( buf == NULL ) |
---|
310 | break; |
---|
311 | |
---|
312 | // read the data for this loop... |
---|
313 | roar_buffer_get_data(buf, &in); |
---|
314 | roar_buffer_get_len(buf, &len); |
---|
315 | |
---|
316 | ROAR_DBG("streams_fill_mixbuffer(*): len = %i", len); |
---|
317 | |
---|
318 | if ( len > todo_in ) { |
---|
319 | roar_buffer_set_offset(buf, todo_in); |
---|
320 | len = todo_in; |
---|
321 | } else { |
---|
322 | roar_buffer_set_len(buf, 0); // queue for deletation |
---|
323 | } |
---|
324 | |
---|
325 | // we now have 'len' bytes in 'in' |
---|
326 | |
---|
327 | // calc how much outlen this has... |
---|
328 | outlen = (len * mul) / div; |
---|
329 | |
---|
330 | ROAR_DBG("streams_fill_mixbuffer(*): outlen = %i, buf = %p, len = %i", outlen, in, len); |
---|
331 | |
---|
332 | if ( is_the_same ) { |
---|
333 | /* |
---|
334 | * 0) convert endianness (codec) from remote to local... |
---|
335 | * 1) change bits in of the samples |
---|
336 | * 2) change sample rate |
---|
337 | * 3) change the nummber of channels |
---|
338 | \\==> skiping,... |
---|
339 | */ |
---|
340 | // * 4) insert into output buffer |
---|
341 | ROAR_DBG("streams_fill_mixbuffer(*): memcpy: in->rest: %p -> %p", in, rest); |
---|
342 | if ( memcpy(rest, in, len) != rest ) { |
---|
343 | ROAR_ERR("streams_fill_mixbuffer(*): memcpy returned invalid pointer."); |
---|
344 | } |
---|
345 | |
---|
346 | } else { |
---|
347 | |
---|
348 | /* |
---|
349 | // * 0) convert endianness (codec) from remote to local... |
---|
350 | if ( stream_info->codec != info->codec ) { |
---|
351 | // we neet to convert... |
---|
352 | return -1; |
---|
353 | } |
---|
354 | |
---|
355 | // * 1) change bits in of the samples |
---|
356 | if ( stream_info->bits != info->bits ) { |
---|
357 | return -1; |
---|
358 | } |
---|
359 | |
---|
360 | // * 2) change sample rate |
---|
361 | if ( stream_info->rate != info->rate ) { |
---|
362 | return -1; |
---|
363 | } |
---|
364 | |
---|
365 | // * 3) change the nummber of channels |
---|
366 | if ( stream_info->channels != info->channels ) { |
---|
367 | return -1; |
---|
368 | } |
---|
369 | |
---|
370 | // * 4) insert into output buffer |
---|
371 | */ |
---|
372 | // hey! we have roar_conv() :) |
---|
373 | |
---|
374 | if ( roar_conv(rest, in, 8*len / stream_info->bits, stream_info, info) == -1 ) |
---|
375 | return -1; |
---|
376 | } |
---|
377 | |
---|
378 | if ( change_vol(rest, info->bits, rest, 8*outlen / info->bits, info->channels, &(stream->mixer)) == -1 ) |
---|
379 | return -1; |
---|
380 | |
---|
381 | // we habe outlen bytes more... |
---|
382 | todo -= outlen; |
---|
383 | rest += outlen; |
---|
384 | todo_in -= len; |
---|
385 | |
---|
386 | roar_buffer_get_len(buf, &len); |
---|
387 | ROAR_DBG("streams_fill_mixbuffer(*): New length of buffer %p is %i", buf, len); |
---|
388 | if ( len == 0 ) { |
---|
389 | roar_buffer_delete(buf, NULL); |
---|
390 | } else { |
---|
391 | stream_unshift_buffer(id, buf); |
---|
392 | } |
---|
393 | } |
---|
394 | |
---|
395 | //len = 0; |
---|
396 | //roar_buffer_get_len(buf, &len); |
---|
397 | |
---|
398 | /* |
---|
399 | if ( len > 0 ) // we still have some data in this buffer, re-inserting it to the input buffers... |
---|
400 | stream_unshift_buffer(id, buf); |
---|
401 | else |
---|
402 | buffer_delete(buf, NULL); |
---|
403 | */ |
---|
404 | |
---|
405 | ((struct roar_stream*)g_streams[id])->pos = |
---|
406 | ROAR_MATH_OVERFLOW_ADD(((struct roar_stream*)g_streams[id])->pos, |
---|
407 | ROAR_OUTPUT_CALC_OUTBUFSAMP(info, needed-todo)); |
---|
408 | //ROAR_WARN("stream=%i, pos=%u", id, ((struct roar_stream*)g_streams[id])->pos); |
---|
409 | |
---|
410 | if ( todo > 0 ) { // zeroize the rest of the buffer |
---|
411 | memset(rest, 0, todo); |
---|
412 | |
---|
413 | if ( todo != ROAR_OUTPUT_CALC_OUTBUFSIZE(info) ) { |
---|
414 | if ( g_streams[id]->is_new ) { |
---|
415 | stream->pre_underruns++; |
---|
416 | } else { |
---|
417 | ROAR_WARN("streams_fill_mixbuffer(*): Underrun in stream: %i bytes missing, filling with zeros", todo); |
---|
418 | stream->post_underruns++; |
---|
419 | } |
---|
420 | |
---|
421 | stream->is_new = 0; |
---|
422 | } |
---|
423 | } else { |
---|
424 | stream->is_new = 0; |
---|
425 | } |
---|
426 | |
---|
427 | return 0; |
---|
428 | } |
---|
429 | |
---|
430 | |
---|
431 | int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) { |
---|
432 | static void * bufs[ROAR_STREAMS_MAX+1]; |
---|
433 | int i; |
---|
434 | int have = 0; |
---|
435 | |
---|
436 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
437 | if ( g_streams[i] != NULL ) { |
---|
438 | if ( ((struct roar_stream *)g_streams[i])->dir != ROAR_DIR_PLAY ) |
---|
439 | continue; |
---|
440 | |
---|
441 | if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) { |
---|
442 | ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i); |
---|
443 | ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round."); |
---|
444 | continue; |
---|
445 | } |
---|
446 | if ( streams_fill_mixbuffer(i, info) == -1 ) { |
---|
447 | ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i); |
---|
448 | continue; |
---|
449 | } |
---|
450 | |
---|
451 | // printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
452 | |
---|
453 | ROAR_DBG("streams_get_mixbuffers(*): bufs[have] = %p", bufs[have]); |
---|
454 | ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]); |
---|
455 | |
---|
456 | have++; // we have a new stream! |
---|
457 | } |
---|
458 | } |
---|
459 | |
---|
460 | bufs[have] = NULL; |
---|
461 | //printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
462 | |
---|
463 | ROAR_DBG("streams_get_mixbuffers(*): have = %i", have); |
---|
464 | |
---|
465 | *bufferlist = bufs; |
---|
466 | return 0; |
---|
467 | } |
---|
468 | |
---|
469 | |
---|
470 | int stream_add_buffer (int id, struct roar_buffer * buf) { |
---|
471 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
472 | |
---|
473 | if ( g_streams[id] == NULL ) |
---|
474 | return -1; |
---|
475 | |
---|
476 | if ( g_streams[id]->buffer == NULL ) { |
---|
477 | g_streams[id]->buffer = buf; |
---|
478 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf); |
---|
479 | return 0; |
---|
480 | } |
---|
481 | |
---|
482 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
483 | return roar_buffer_add(g_streams[id]->buffer, buf); |
---|
484 | } |
---|
485 | |
---|
486 | int stream_shift_buffer (int id, struct roar_buffer ** buf) { |
---|
487 | struct roar_buffer * next; |
---|
488 | |
---|
489 | if ( g_streams[id] == NULL ) |
---|
490 | return -1; |
---|
491 | |
---|
492 | if ( g_streams[id]->buffer == NULL ) { |
---|
493 | *buf = NULL; |
---|
494 | return 0; |
---|
495 | } |
---|
496 | |
---|
497 | roar_buffer_get_next(g_streams[id]->buffer, &next); |
---|
498 | |
---|
499 | *buf = g_streams[id]->buffer; |
---|
500 | g_streams[id]->buffer = next; |
---|
501 | |
---|
502 | return 0; |
---|
503 | } |
---|
504 | int stream_unshift_buffer (int id, struct roar_buffer * buf) { |
---|
505 | if ( g_streams[id] == NULL ) |
---|
506 | return -1; |
---|
507 | |
---|
508 | if ( g_streams[id]->buffer == NULL ) { |
---|
509 | g_streams[id]->buffer = buf; |
---|
510 | return 0; |
---|
511 | } |
---|
512 | |
---|
513 | buf->next = NULL; |
---|
514 | |
---|
515 | roar_buffer_add(buf, g_streams[id]->buffer); |
---|
516 | |
---|
517 | g_streams[id]->buffer = buf; |
---|
518 | |
---|
519 | return 0; |
---|
520 | } |
---|
521 | |
---|
522 | int streams_check (int id) { |
---|
523 | int fh; |
---|
524 | ssize_t req, realreq, done; |
---|
525 | struct roar_stream * s; |
---|
526 | struct roar_stream_server * ss; |
---|
527 | struct roar_buffer * b; |
---|
528 | char * buf; |
---|
529 | |
---|
530 | if ( g_streams[id] == NULL ) |
---|
531 | return -1; |
---|
532 | |
---|
533 | ROAR_DBG("streams_check(id=%i) = ?", id); |
---|
534 | |
---|
535 | s = (struct roar_stream *) (ss = g_streams[id]); |
---|
536 | |
---|
537 | if ( (fh = s->fh) == -1 ) |
---|
538 | return 0; |
---|
539 | |
---|
540 | if ( s->dir != ROAR_DIR_PLAY ) |
---|
541 | return 0; |
---|
542 | |
---|
543 | ROAR_DBG("streams_check(id=%i): fh = %i", id, fh); |
---|
544 | |
---|
545 | req = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size |
---|
546 | req += ss->need_extra; // bytes left we sould get.... |
---|
547 | |
---|
548 | if ( roar_buffer_new(&b, req) == -1 ) { |
---|
549 | ROAR_ERR("streams_check(*): Can not alloc buffer space!"); |
---|
550 | ROAR_DBG("streams_check(*) = -1"); |
---|
551 | return -1; |
---|
552 | } |
---|
553 | |
---|
554 | roar_buffer_get_data(b, (void **)&buf); |
---|
555 | |
---|
556 | ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id); |
---|
557 | |
---|
558 | if ( ss->codecfilter == -1 ) { |
---|
559 | realreq = req; |
---|
560 | /* |
---|
561 | req = read(fh, buf, req); |
---|
562 | if ( req < realreq ) { // we can do this as the stream is in nonblocking mode! |
---|
563 | if ( (realreq = read(fh, buf+req, realreq-req)) > 0 ) |
---|
564 | req += realreq; |
---|
565 | } |
---|
566 | */ |
---|
567 | done = 0; |
---|
568 | while (req > 0 && done != realreq) { |
---|
569 | if ( (req = read(fh, buf+done, realreq-done)) > 0 ) |
---|
570 | done += req; |
---|
571 | } |
---|
572 | req = done; |
---|
573 | } else { |
---|
574 | req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req); |
---|
575 | } |
---|
576 | |
---|
577 | if ( req > 0 ) { |
---|
578 | ROAR_DBG("streams_check(id=%i): got %i bytes", id, req); |
---|
579 | |
---|
580 | roar_buffer_set_len(b, req); |
---|
581 | |
---|
582 | if ( stream_add_buffer(id, b) != -1 ) |
---|
583 | return 0; |
---|
584 | |
---|
585 | ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id); |
---|
586 | roar_buffer_free(b); |
---|
587 | } else { |
---|
588 | ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno)); |
---|
589 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
590 | if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :) |
---|
591 | #else |
---|
592 | if ( errno != EAGAIN ) { |
---|
593 | #endif |
---|
594 | ROAR_DBG("streams_check(id=%i): EOF!", id); |
---|
595 | streams_delete(id); |
---|
596 | ROAR_DBG("streams_check(id=%i) = 0", id); |
---|
597 | } |
---|
598 | roar_buffer_free(b); |
---|
599 | return 0; |
---|
600 | } |
---|
601 | |
---|
602 | |
---|
603 | ROAR_DBG("streams_check(id=%i) = -1", id); |
---|
604 | return -1; |
---|
605 | } |
---|
606 | |
---|
607 | |
---|
608 | int streams_send_mon (int id) { |
---|
609 | int fh; |
---|
610 | struct roar_stream * s; |
---|
611 | struct roar_stream_server * ss; |
---|
612 | |
---|
613 | if ( g_streams[id] == NULL ) |
---|
614 | return -1; |
---|
615 | |
---|
616 | ROAR_DBG("streams_send_mon(id=%i) = ?", id); |
---|
617 | |
---|
618 | s = (struct roar_stream *) (ss = g_streams[id]); |
---|
619 | |
---|
620 | if ( (fh = s->fh) == -1 ) |
---|
621 | return 0; |
---|
622 | |
---|
623 | if ( s->dir != ROAR_DIR_MONITOR ) |
---|
624 | return 0; |
---|
625 | |
---|
626 | ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, fh); |
---|
627 | |
---|
628 | if ( write(fh, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) |
---|
629 | return 0; |
---|
630 | |
---|
631 | if ( errno == EAGAIN ) { |
---|
632 | // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to |
---|
633 | // make any gapes in any output because of this |
---|
634 | |
---|
635 | usleep(100); // 0.1ms |
---|
636 | |
---|
637 | if ( write(fh, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) |
---|
638 | return 0; |
---|
639 | } |
---|
640 | |
---|
641 | // ug... error... delete stream! |
---|
642 | |
---|
643 | streams_delete(id); |
---|
644 | |
---|
645 | return -1; |
---|
646 | } |
---|
647 | |
---|
648 | int streams_send_filter(int id) { |
---|
649 | int fh; |
---|
650 | int have = 0; |
---|
651 | int len; |
---|
652 | struct roar_stream * s; |
---|
653 | struct roar_stream_server * ss; |
---|
654 | |
---|
655 | if ( g_streams[id] == NULL ) |
---|
656 | return -1; |
---|
657 | |
---|
658 | ROAR_DBG("streams_send_filter(id=%i) = ?", id); |
---|
659 | |
---|
660 | s = (struct roar_stream *) (ss = g_streams[id]); |
---|
661 | |
---|
662 | if ( (fh = s->fh) == -1 ) |
---|
663 | return 0; |
---|
664 | |
---|
665 | if ( s->dir != ROAR_DIR_FILTER ) |
---|
666 | return 0; |
---|
667 | |
---|
668 | ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh); |
---|
669 | |
---|
670 | if ( write(fh, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) { |
---|
671 | while ( have < g_output_buffer_len ) { |
---|
672 | if ( (len = read(fh, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) { |
---|
673 | streams_delete(id); |
---|
674 | return -1; |
---|
675 | } |
---|
676 | have += len; |
---|
677 | } |
---|
678 | return 0; |
---|
679 | } |
---|
680 | |
---|
681 | // ug... error... delete stream! |
---|
682 | |
---|
683 | streams_delete(id); |
---|
684 | |
---|
685 | return -1; |
---|
686 | } |
---|
687 | |
---|
688 | //ll |
---|