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