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 | /* |
---|
75 | n->database = NULL; |
---|
76 | n->dataoff = NULL; |
---|
77 | n->datalen = 0; |
---|
78 | n->offset = 0; |
---|
79 | */ |
---|
80 | n->pos = 0; |
---|
81 | |
---|
82 | s->name = NULL; |
---|
83 | |
---|
84 | s->client = -1; |
---|
85 | s->socktype = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
86 | s->buffer = NULL; |
---|
87 | s->need_extra = 0; |
---|
88 | s->output = NULL; |
---|
89 | s->is_new = 1; |
---|
90 | s->codecfilter = -1; |
---|
91 | s->pre_underruns = 0; |
---|
92 | s->post_underruns = 0; |
---|
93 | s->delay = 0; |
---|
94 | s->codec_orgi = -1; |
---|
95 | s->primary = 0; |
---|
96 | s->ready = 0; |
---|
97 | s->outputbuffer = NULL; |
---|
98 | |
---|
99 | s->mixer.scale = 65535; |
---|
100 | s->mixer.rpg_mul = 1; |
---|
101 | s->mixer.rpg_div = 1; |
---|
102 | for (j = 0; j < ROAR_MAX_CHANNELS; j++) |
---|
103 | s->mixer.mixer[j] = 65535; |
---|
104 | |
---|
105 | #ifdef ROAR_SUPPORT_META |
---|
106 | for (j = 0; j < ROAR_META_MAX_PER_STREAM; j++) { |
---|
107 | s->meta[j].type = ROAR_META_TYPE_NONE; |
---|
108 | s->meta[j].key[0] = 0; |
---|
109 | s->meta[j].value = NULL; |
---|
110 | } |
---|
111 | #endif |
---|
112 | |
---|
113 | roar_vio_init_calls(&(s->vio)); |
---|
114 | s->driver_id = -1; |
---|
115 | s->flags = ROAR_FLAG_NONE; |
---|
116 | |
---|
117 | //roardsp_fchain_init(&(s->fc)); |
---|
118 | |
---|
119 | g_streams[i] = s; |
---|
120 | ROAR_DBG("streams_new(void): n->id=%i", n->id); |
---|
121 | ROAR_DBG("streams_new(void) = %i", i); |
---|
122 | return i; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | return -1; |
---|
127 | } |
---|
128 | |
---|
129 | int streams_delete (int id) { |
---|
130 | struct roar_stream_server * s; |
---|
131 | int prim; |
---|
132 | int no_vio_close = 0; |
---|
133 | int i; |
---|
134 | |
---|
135 | if ( (s = g_streams[id]) == NULL ) |
---|
136 | return 0; |
---|
137 | |
---|
138 | ROAR_DBG("streams_delete(id=%i) = ?", id); |
---|
139 | ROAR_DBG("streams_delete(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(s)->id); |
---|
140 | |
---|
141 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
142 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) { |
---|
143 | if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) { |
---|
144 | streams_delete(i); |
---|
145 | } else { |
---|
146 | ROAR_STREAM(g_streams[i])->pos_rel_id = -1; |
---|
147 | } |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | #ifdef ROAR_SUPPORT_META |
---|
152 | // delete meta data form other meta streams if needed |
---|
153 | if ( streams_get_flag(id, ROAR_FLAG_META) == 1 ) { |
---|
154 | ROAR_DBG("streams_delete(id=%i): deleting meta stream!", id); |
---|
155 | stream_meta_clear(id); |
---|
156 | stream_meta_finalize(id); |
---|
157 | } |
---|
158 | #endif |
---|
159 | |
---|
160 | if ( s->codecfilter != -1 ) { |
---|
161 | codecfilter_close(s->codecfilter_inst, s->codecfilter); |
---|
162 | s->codecfilter_inst = NULL; |
---|
163 | s->codecfilter = -1; |
---|
164 | } |
---|
165 | |
---|
166 | if ( s->driver_id != -1 ) { |
---|
167 | driver_closevio(&(s->vio), s->driver_id); |
---|
168 | roar_vio_init_calls(&(s->vio)); |
---|
169 | s->driver_id = -1; |
---|
170 | no_vio_close = 1; |
---|
171 | } |
---|
172 | |
---|
173 | //roardsp_fchain_uninit(&(s->fc)); |
---|
174 | |
---|
175 | if ( s->client != -1 ) { |
---|
176 | ROAR_DBG("streams_delete(id=%i): Stream is owned by client %i", id, g_streams[id]->client); |
---|
177 | client_stream_delete(s->client, id); |
---|
178 | } |
---|
179 | |
---|
180 | stream_outputbuffer_destroy(id); |
---|
181 | |
---|
182 | if ( s->buffer != NULL ) |
---|
183 | roar_buffer_free(s->buffer); |
---|
184 | |
---|
185 | if ( s->output != NULL ) |
---|
186 | free(s->output); |
---|
187 | |
---|
188 | /* |
---|
189 | if ( ROAR_STREAM(s)->fh != -1 ) |
---|
190 | close(ROAR_STREAM(s)->fh); |
---|
191 | */ |
---|
192 | |
---|
193 | if ( !no_vio_close ) |
---|
194 | roar_vio_close(&(s->vio)); |
---|
195 | |
---|
196 | prim = s->primary; |
---|
197 | |
---|
198 | if ( s->name != NULL ) |
---|
199 | free(s->name); |
---|
200 | |
---|
201 | free(s); |
---|
202 | |
---|
203 | g_streams[id] = NULL; |
---|
204 | |
---|
205 | if ( prim ) { |
---|
206 | alive = 0; |
---|
207 | clean_quit(); |
---|
208 | } |
---|
209 | |
---|
210 | ROAR_DBG("streams_delete(id=%i) = 0", id); |
---|
211 | return 0; |
---|
212 | } |
---|
213 | |
---|
214 | int streams_set_client (int id, int client) { |
---|
215 | if ( g_streams[id] == NULL ) |
---|
216 | return -1; |
---|
217 | |
---|
218 | ROAR_DBG("streams_set_client(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id); |
---|
219 | g_streams[id]->client = client; |
---|
220 | |
---|
221 | return 0; |
---|
222 | } |
---|
223 | |
---|
224 | int streams_get_client (int id) { |
---|
225 | if ( g_streams[id] == NULL ) |
---|
226 | return -1; |
---|
227 | |
---|
228 | return g_streams[id]->client; |
---|
229 | } |
---|
230 | |
---|
231 | int streams_set_dir (int id, int dir, int defaults) { |
---|
232 | struct roar_stream_server * ss; |
---|
233 | |
---|
234 | if ( (ss = g_streams[id]) == NULL ) |
---|
235 | return -1; |
---|
236 | |
---|
237 | ROAR_STREAM(ss)->dir = dir; |
---|
238 | |
---|
239 | if ( defaults ) { |
---|
240 | if ( dir <= 0 || dir >= ROAR_DIR_DIRIDS ) |
---|
241 | return -1; |
---|
242 | |
---|
243 | ROAR_DBG("streams_set_dir(*): g_config->streams[dir=%i].flags = 0x%0.4x", dir, g_config->streams[dir].flags); |
---|
244 | |
---|
245 | if ( streams_set_flag(id, g_config->streams[dir].flags) == -1 ) { |
---|
246 | ROAR_DBG("streams_set_dir(*) = -1 // can not set stream flags"); |
---|
247 | return -1; |
---|
248 | } |
---|
249 | |
---|
250 | ss->mixer.scale = g_config->streams[dir].mixer.scale; |
---|
251 | ss->mixer.rpg_mul = g_config->streams[dir].mixer.rpg_mul; |
---|
252 | ss->mixer.rpg_div = g_config->streams[dir].mixer.rpg_div; |
---|
253 | } |
---|
254 | |
---|
255 | ROAR_DBG("streams_set_dir(*) = 0"); |
---|
256 | return 0; |
---|
257 | } |
---|
258 | |
---|
259 | int streams_set_fh (int id, int fh) { |
---|
260 | struct roar_stream_server * ss; |
---|
261 | struct roar_stream * s; |
---|
262 | int dir; |
---|
263 | |
---|
264 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
265 | return -1; |
---|
266 | |
---|
267 | ROAR_DBG("streams_set_fh(id=%i): g_streams[id]->id=%i", id, s->id); |
---|
268 | |
---|
269 | s->fh = fh; |
---|
270 | |
---|
271 | ROAR_DBG("streams_set_fh(id=%i, fh=%i): driverID=%i", id, fh, ss->driver_id); |
---|
272 | |
---|
273 | if ( ss->driver_id == -1 && fh != -2 ) |
---|
274 | roar_vio_set_fh(&(ss->vio), fh); |
---|
275 | |
---|
276 | if ( codecfilter_open(&(ss->codecfilter_inst), &(ss->codecfilter), NULL, |
---|
277 | s->info.codec, ss) == -1 ) { |
---|
278 | return streams_delete(id); // TODO: FIXME: is this correct? shoudn't we return -1 in any case here? |
---|
279 | } |
---|
280 | |
---|
281 | if ( fh == -2 ) { |
---|
282 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
283 | if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_READ_FH, &fh) == -1 ) { |
---|
284 | fh = -2; |
---|
285 | } else { |
---|
286 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
287 | if ( fh < 0 ) { |
---|
288 | fh = -2; |
---|
289 | } else { |
---|
290 | s->fh = fh; |
---|
291 | } |
---|
292 | } |
---|
293 | } |
---|
294 | |
---|
295 | if ( fh == -1 || fh == -2 ) { // yes, this is valid, indecats full vio! |
---|
296 | ss->ready = 1; |
---|
297 | return 0; |
---|
298 | } |
---|
299 | |
---|
300 | // roar_socket_recvbuf(fh, ROAR_OUTPUT_CALC_OUTBUFSIZE( &(ROAR_STREAM(g_streams[id])->info) )); // set recv buffer to minimum |
---|
301 | |
---|
302 | dir = ROAR_STREAM(ss)->dir; |
---|
303 | |
---|
304 | switch (dir) { |
---|
305 | case ROAR_DIR_MONITOR: |
---|
306 | case ROAR_DIR_RECORD: |
---|
307 | case ROAR_DIR_OUTPUT: |
---|
308 | case ROAR_DIR_MIDI_OUT: |
---|
309 | case ROAR_DIR_LIGHT_OUT: |
---|
310 | ROAR_SHUTDOWN(fh, SHUT_RD); |
---|
311 | break; |
---|
312 | } |
---|
313 | |
---|
314 | if ( dir == ROAR_DIR_FILTER ) { |
---|
315 | ss->ready = 1; |
---|
316 | return 0; |
---|
317 | } else { |
---|
318 | if ( roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK) == -1 ) |
---|
319 | return -1; |
---|
320 | |
---|
321 | ss->ready = 1; |
---|
322 | return 0; |
---|
323 | } |
---|
324 | } |
---|
325 | |
---|
326 | int streams_get_fh (int id) { |
---|
327 | if ( id < 0 ) |
---|
328 | return -1; |
---|
329 | |
---|
330 | if ( g_streams[id] == NULL ) |
---|
331 | return -1; |
---|
332 | |
---|
333 | return ROAR_STREAM(g_streams[id])->fh; |
---|
334 | } |
---|
335 | |
---|
336 | int streams_get (int id, struct roar_stream_server ** stream) { |
---|
337 | if ( g_streams[id] == NULL ) |
---|
338 | return -1; |
---|
339 | |
---|
340 | *stream = g_streams[id]; |
---|
341 | |
---|
342 | return 0; |
---|
343 | } |
---|
344 | |
---|
345 | int streams_set_socktype (int id, int socktype) { |
---|
346 | if ( g_streams[id] == NULL ) |
---|
347 | return -1; |
---|
348 | |
---|
349 | g_streams[id]->socktype = socktype; |
---|
350 | |
---|
351 | return 0; |
---|
352 | } |
---|
353 | |
---|
354 | int streams_get_socktype (int id) { |
---|
355 | if ( g_streams[id] == NULL ) |
---|
356 | return -1; |
---|
357 | |
---|
358 | return g_streams[id]->socktype; |
---|
359 | } |
---|
360 | |
---|
361 | int streams_set_primary (int id, int prim) { |
---|
362 | if ( g_streams[id] == NULL ) |
---|
363 | return -1; |
---|
364 | |
---|
365 | g_streams[id]->primary = prim; |
---|
366 | |
---|
367 | return 0; |
---|
368 | } |
---|
369 | |
---|
370 | int streams_mark_primary (int id) { |
---|
371 | return streams_set_primary(id, 1); |
---|
372 | } |
---|
373 | |
---|
374 | int streams_set_sync (int id, int sync) { |
---|
375 | int fh = streams_get_fh(id); |
---|
376 | |
---|
377 | if ( fh != -1 ) { |
---|
378 | if ( roar_socket_nonblock(fh, sync ? ROAR_SOCKET_BLOCK : ROAR_SOCKET_NONBLOCK) == -1 ) |
---|
379 | return -1; |
---|
380 | |
---|
381 | #ifdef ROAR_FDATASYNC |
---|
382 | ROAR_FDATASYNC(fh); |
---|
383 | #endif |
---|
384 | |
---|
385 | return 0; |
---|
386 | } else { |
---|
387 | return roar_vio_nonblock(&(g_streams[id]->vio), sync); |
---|
388 | } |
---|
389 | } |
---|
390 | |
---|
391 | int streams_set_mmap (int id, int reset) { |
---|
392 | int use = !reset; |
---|
393 | |
---|
394 | if ( g_streams[id] == NULL ) |
---|
395 | return -1; |
---|
396 | |
---|
397 | return roar_vio_ctl(&(g_streams[id]->vio), ROAR_VIO_CTL_SET_UMMAP, &use); |
---|
398 | } |
---|
399 | |
---|
400 | int streams_set_flag (int id, int flag) { |
---|
401 | if ( g_streams[id] == NULL ) |
---|
402 | return -1; |
---|
403 | |
---|
404 | if ( flag & ROAR_FLAG_MMAP ) |
---|
405 | if ( streams_set_mmap(id, 0) == -1 ) |
---|
406 | flag -= ROAR_FLAG_MMAP; |
---|
407 | |
---|
408 | if ( flag & ROAR_FLAG_PRIMARY ) { |
---|
409 | streams_set_primary(id, 1); |
---|
410 | flag -= ROAR_FLAG_PRIMARY; |
---|
411 | } |
---|
412 | |
---|
413 | if ( flag & ROAR_FLAG_SYNC ) { |
---|
414 | switch (ROAR_STREAM(g_streams[id])->dir) { |
---|
415 | // for this stream types the flag is used in the subsystem: |
---|
416 | case ROAR_DIR_BRIDGE: |
---|
417 | case ROAR_DIR_MIDI_OUT: |
---|
418 | break; |
---|
419 | |
---|
420 | // normal behavor (vio blocking): |
---|
421 | default: |
---|
422 | if ( streams_set_sync(id, 1) == -1 ) |
---|
423 | flag -= ROAR_FLAG_SYNC; |
---|
424 | } |
---|
425 | } |
---|
426 | |
---|
427 | if ( flag & ROAR_FLAG_HWMIXER ) { // currently not supported -> ignored |
---|
428 | g_streams[id]->flags |= flag; |
---|
429 | if ( streams_set_mixer(id) == -1 ) { |
---|
430 | g_streams[id]->flags -= flag; |
---|
431 | return -1; |
---|
432 | } |
---|
433 | } |
---|
434 | |
---|
435 | g_streams[id]->flags |= flag; |
---|
436 | |
---|
437 | #ifdef ROAR_SUPPORT_META |
---|
438 | if ( flag & ROAR_FLAG_META ) |
---|
439 | stream_meta_finalize(id); |
---|
440 | #endif |
---|
441 | |
---|
442 | return 0; |
---|
443 | } |
---|
444 | |
---|
445 | int streams_reset_flag (int id, int flag) { |
---|
446 | if ( g_streams[id] == NULL ) |
---|
447 | return -1; |
---|
448 | |
---|
449 | if ( flag & ROAR_FLAG_MMAP ) |
---|
450 | if ( streams_set_mmap(id, 1) == -1 ) |
---|
451 | flag -= ROAR_FLAG_MMAP; |
---|
452 | |
---|
453 | if ( flag & ROAR_FLAG_PRIMARY ) { |
---|
454 | streams_set_primary(id, 0); |
---|
455 | flag -= ROAR_FLAG_PRIMARY; |
---|
456 | } |
---|
457 | |
---|
458 | if ( flag & ROAR_FLAG_SYNC ) { |
---|
459 | streams_set_sync(id, 0); |
---|
460 | } |
---|
461 | |
---|
462 | g_streams[id]->flags |= flag; |
---|
463 | g_streams[id]->flags -= flag; |
---|
464 | |
---|
465 | return 0; |
---|
466 | } |
---|
467 | |
---|
468 | int streams_get_flag (int id, int flag) { |
---|
469 | if ( g_streams[id] == NULL ) |
---|
470 | return -1; |
---|
471 | |
---|
472 | return g_streams[id]->flags & flag ? 1 : 0; |
---|
473 | } |
---|
474 | |
---|
475 | int streams_set_name (int id, char * name) { |
---|
476 | char * str; |
---|
477 | |
---|
478 | if ( g_streams[id] == NULL ) |
---|
479 | return -1; |
---|
480 | |
---|
481 | if ( (str = strdup(name)) == NULL ) |
---|
482 | return -1; |
---|
483 | |
---|
484 | if ( g_streams[id]->name != NULL ) |
---|
485 | free(g_streams[id]->name); |
---|
486 | |
---|
487 | g_streams[id]->name = str; |
---|
488 | |
---|
489 | return 0; |
---|
490 | } |
---|
491 | |
---|
492 | char * streams_get_name (int id) { |
---|
493 | if ( g_streams[id] == NULL ) |
---|
494 | return NULL; |
---|
495 | |
---|
496 | return g_streams[id]->name; |
---|
497 | } |
---|
498 | |
---|
499 | |
---|
500 | int streams_calc_delay (int id) { |
---|
501 | struct roar_stream_server * ss; |
---|
502 | struct roar_stream * s; |
---|
503 | register uint_least32_t d = 0; |
---|
504 | uint_least32_t t[1]; |
---|
505 | uint64_t tmp; |
---|
506 | |
---|
507 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
508 | return -1; |
---|
509 | |
---|
510 | if ( ss->codecfilter != -1 ) { |
---|
511 | if ( codecfilter_delay(ss->codecfilter_inst, ss->codecfilter, t) != -1 ) |
---|
512 | d += *t; |
---|
513 | } |
---|
514 | |
---|
515 | if ( ss->vio.ctl != NULL ) { |
---|
516 | if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_DELAY, t) != -1 ) { // *t is in byte |
---|
517 | ROAR_DBG("streams_calc_delay(id=%i): VIO delay in byte: %i", id, *t); |
---|
518 | tmp = *t; |
---|
519 | tmp *= 1000000; // musec per sec |
---|
520 | tmp /= s->info.rate * s->info.channels * (s->info.bits/8); |
---|
521 | ROAR_DBG("streams_calc_delay(id=%i): VIO delay in musec: %i", id, tmp); |
---|
522 | |
---|
523 | d += tmp; |
---|
524 | } |
---|
525 | } |
---|
526 | |
---|
527 | ROAR_DBG("streams_calc_delay(id=%i): delay in musec: %i", id, d); |
---|
528 | |
---|
529 | ss->delay = d; |
---|
530 | |
---|
531 | return 0; |
---|
532 | } |
---|
533 | |
---|
534 | int streams_set_mixer (int id) { |
---|
535 | struct roar_stream_server * ss; |
---|
536 | |
---|
537 | if ( (ss = g_streams[id]) == NULL ) |
---|
538 | return -1; |
---|
539 | |
---|
540 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) |
---|
541 | return 0; |
---|
542 | |
---|
543 | if ( ss->driver_id == -1 ) |
---|
544 | return 0; |
---|
545 | |
---|
546 | return driver_set_volume(id, &(ss->mixer)); |
---|
547 | } |
---|
548 | |
---|
549 | int streams_ctl (int id, int_least32_t cmd, void * data) { |
---|
550 | struct roar_stream_server * ss; |
---|
551 | int_least32_t comp; |
---|
552 | |
---|
553 | if ( (ss = g_streams[id]) == NULL ) |
---|
554 | return -1; |
---|
555 | |
---|
556 | comp = cmd & ROAR_STREAM_CTL_COMPMASK; |
---|
557 | |
---|
558 | cmd &= ~comp; |
---|
559 | |
---|
560 | ROAR_DBG("streams_ctl(id=%i, cmd=?, data=%p): comp=0x%.8x, cmd=0x%.4x", id, data, comp, cmd); |
---|
561 | |
---|
562 | switch (comp) { |
---|
563 | case ROAR_STREAM_CTL_COMP_BASE: |
---|
564 | break; |
---|
565 | case ROAR_STREAM_CTL_COMP_CF: |
---|
566 | return codecfilter_ctl(ss->codecfilter_inst, ss->codecfilter, cmd, data); |
---|
567 | break; |
---|
568 | case ROAR_STREAM_CTL_COMP_DRV: |
---|
569 | break; |
---|
570 | default: |
---|
571 | return -1; |
---|
572 | } |
---|
573 | |
---|
574 | return -1; |
---|
575 | } |
---|
576 | |
---|
577 | int streams_get_outputbuffer (int id, void ** buffer, size_t size) { |
---|
578 | if ( g_streams[id] == NULL ) |
---|
579 | return -1; |
---|
580 | |
---|
581 | // output buffer size does never change. |
---|
582 | if ( g_streams[id]->output != NULL ) { |
---|
583 | *buffer = g_streams[id]->output; |
---|
584 | return 0; |
---|
585 | } |
---|
586 | |
---|
587 | if ( (g_streams[id]->output = malloc(size)) == NULL ) { |
---|
588 | ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno)); |
---|
589 | return -1; |
---|
590 | } |
---|
591 | |
---|
592 | *buffer = g_streams[id]->output; |
---|
593 | |
---|
594 | return 0; |
---|
595 | } |
---|
596 | |
---|
597 | int streams_fill_mixbuffer (int id, struct roar_audio_info * info) { |
---|
598 | // TODO: decide: is this the most complex, hacked, un-understadable, |
---|
599 | // un-writeable and even worse: un-readable |
---|
600 | // function in the whole project? |
---|
601 | size_t todo = ROAR_OUTPUT_CALC_OUTBUFSIZE(info); |
---|
602 | size_t needed = todo; |
---|
603 | size_t todo_in; |
---|
604 | size_t len, outlen; |
---|
605 | size_t mul = 1, div = 1; |
---|
606 | void * rest = NULL; |
---|
607 | void * in = NULL; |
---|
608 | struct roar_buffer * buf; |
---|
609 | struct roar_audio_info * stream_info; |
---|
610 | struct roar_stream_server * stream = g_streams[id]; |
---|
611 | int is_the_same = 0; |
---|
612 | |
---|
613 | if ( g_streams[id] == NULL ) |
---|
614 | return -1; |
---|
615 | |
---|
616 | if ( streams_get_outputbuffer(id, &rest, todo) == -1 ) { |
---|
617 | return -1; |
---|
618 | } |
---|
619 | |
---|
620 | if ( rest == NULL ) { |
---|
621 | return -1; |
---|
622 | } |
---|
623 | |
---|
624 | // set up stream_info |
---|
625 | |
---|
626 | stream_info = &(ROAR_STREAM(stream)->info); |
---|
627 | |
---|
628 | // calc todo_in |
---|
629 | todo_in = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info); |
---|
630 | |
---|
631 | if ( todo_in == 0 ) { |
---|
632 | ROAR_WARN("streams_fill_mixbuffer(id=%i, info=%p{...}): todo_in == 0, this should not happen!", id, info); |
---|
633 | return -1; |
---|
634 | } |
---|
635 | |
---|
636 | // calc mul and div: |
---|
637 | mul = todo / todo_in; |
---|
638 | div = todo_in / todo; |
---|
639 | |
---|
640 | if ( mul == 0 ) { |
---|
641 | mul = 1; |
---|
642 | } else { |
---|
643 | div = 1; |
---|
644 | } |
---|
645 | |
---|
646 | ROAR_DBG("streams_fill_mixbuffer(*): mul=%i, div=%i", mul, div); |
---|
647 | |
---|
648 | ROAR_DBG("streams_fill_mixbuffer(*): rest=%p, todo=%i->%i (in->out)", rest, todo_in, todo); |
---|
649 | // are both (input and output) of same format? |
---|
650 | |
---|
651 | |
---|
652 | ROAR_DBG("streams_fill_mixbuffer(*): stream_info:"); |
---|
653 | roar_debug_audio_info_print(stream_info); |
---|
654 | ROAR_DBG("streams_fill_mixbuffer(*): info:"); |
---|
655 | roar_debug_audio_info_print(info); |
---|
656 | |
---|
657 | is_the_same = stream_info->rate == info->rate && stream_info->bits == info->bits && |
---|
658 | stream_info->channels == info->channels && stream_info->codec == info->codec; |
---|
659 | |
---|
660 | ROAR_DBG("streams_fill_mixbuffer(*): is_the_same=%i", is_the_same); |
---|
661 | |
---|
662 | /* How it works: |
---|
663 | * |
---|
664 | * set a counter to the number of samples we need. |
---|
665 | * loop until we have all samples done or no samples are |
---|
666 | * left in our input buffer. |
---|
667 | * If there a no samples left in the input buffer: fill the rest |
---|
668 | * of the output buffer with zeros. |
---|
669 | * |
---|
670 | * The loop: |
---|
671 | * get a buffer from the input. |
---|
672 | * if it's bigger than needed, set an offset. |
---|
673 | * The rest of the data: |
---|
674 | * 0) convert endianness (codec) from remote to local... |
---|
675 | * 1) change bits in of the samples |
---|
676 | * 2) change sample rate |
---|
677 | * 3) change the nummber of channels |
---|
678 | * 4) insert into output buffer |
---|
679 | */ |
---|
680 | |
---|
681 | /* |
---|
682 | // get our first buffer: |
---|
683 | |
---|
684 | if ( stream_shift_buffer(id, &buf) == -1 ) { |
---|
685 | return -1; |
---|
686 | } |
---|
687 | |
---|
688 | // first test for some basic simple cases... |
---|
689 | |
---|
690 | if ( buf == NULL ) { // we habe nothing in input queue |
---|
691 | // we may memset() our output buffer OR |
---|
692 | // just return with -1 so we are going to |
---|
693 | // be ignored. |
---|
694 | return -1; |
---|
695 | } |
---|
696 | */ |
---|
697 | |
---|
698 | while (todo) { // main loop |
---|
699 | ROAR_DBG("streams_fill_mixbuffer(*): looping..."); |
---|
700 | // exit loop if nothing is left, even if we need more data.. |
---|
701 | if ( stream_shift_buffer(id, &buf) == -1 ) |
---|
702 | break; |
---|
703 | if ( buf == NULL ) |
---|
704 | break; |
---|
705 | |
---|
706 | // read the data for this loop... |
---|
707 | roar_buffer_get_data(buf, &in); |
---|
708 | roar_buffer_get_len(buf, &len); |
---|
709 | |
---|
710 | ROAR_DBG("streams_fill_mixbuffer(*): len = %i", len); |
---|
711 | |
---|
712 | if ( len > todo_in ) { |
---|
713 | roar_buffer_set_offset(buf, todo_in); |
---|
714 | len = todo_in; |
---|
715 | } else { |
---|
716 | roar_buffer_set_len(buf, 0); // queue for deletation |
---|
717 | } |
---|
718 | |
---|
719 | // we now have 'len' bytes in 'in' |
---|
720 | |
---|
721 | // calc how much outlen this has... |
---|
722 | outlen = (len * mul) / div; |
---|
723 | |
---|
724 | ROAR_DBG("streams_fill_mixbuffer(*): outlen = %i, buf = %p, len = %i", outlen, in, len); |
---|
725 | |
---|
726 | if ( is_the_same ) { |
---|
727 | /* |
---|
728 | * 0) convert endianness (codec) from remote to local... |
---|
729 | * 1) change bits in of the samples |
---|
730 | * 2) change sample rate |
---|
731 | * 3) change the nummber of channels |
---|
732 | \\==> skiping,... |
---|
733 | */ |
---|
734 | // * 4) insert into output buffer |
---|
735 | ROAR_DBG("streams_fill_mixbuffer(*): memcpy: in->rest: %p -> %p", in, rest); |
---|
736 | if ( memcpy(rest, in, len) != rest ) { |
---|
737 | ROAR_ERR("streams_fill_mixbuffer(*): memcpy returned invalid pointer."); |
---|
738 | } |
---|
739 | |
---|
740 | } else { |
---|
741 | |
---|
742 | /* |
---|
743 | // * 0) convert endianness (codec) from remote to local... |
---|
744 | if ( stream_info->codec != info->codec ) { |
---|
745 | // we neet to convert... |
---|
746 | return -1; |
---|
747 | } |
---|
748 | |
---|
749 | // * 1) change bits in of the samples |
---|
750 | if ( stream_info->bits != info->bits ) { |
---|
751 | return -1; |
---|
752 | } |
---|
753 | |
---|
754 | // * 2) change sample rate |
---|
755 | if ( stream_info->rate != info->rate ) { |
---|
756 | return -1; |
---|
757 | } |
---|
758 | |
---|
759 | // * 3) change the nummber of channels |
---|
760 | if ( stream_info->channels != info->channels ) { |
---|
761 | return -1; |
---|
762 | } |
---|
763 | |
---|
764 | // * 4) insert into output buffer |
---|
765 | */ |
---|
766 | // hey! we have roar_conv() :) |
---|
767 | |
---|
768 | if ( roar_conv(rest, in, 8*len / stream_info->bits, stream_info, info) == -1 ) |
---|
769 | return -1; |
---|
770 | } |
---|
771 | |
---|
772 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) { |
---|
773 | if ( change_vol(rest, info->bits, rest, 8*outlen / info->bits, info->channels, &(stream->mixer)) == -1 ) |
---|
774 | return -1; |
---|
775 | } |
---|
776 | |
---|
777 | // we habe outlen bytes more... |
---|
778 | todo -= outlen; |
---|
779 | rest += outlen; |
---|
780 | todo_in -= len; |
---|
781 | |
---|
782 | roar_buffer_get_len(buf, &len); |
---|
783 | ROAR_DBG("streams_fill_mixbuffer(*): New length of buffer %p is %i", buf, len); |
---|
784 | if ( len == 0 ) { |
---|
785 | roar_buffer_delete(buf, NULL); |
---|
786 | } else { |
---|
787 | stream_unshift_buffer(id, buf); |
---|
788 | } |
---|
789 | } |
---|
790 | |
---|
791 | //len = 0; |
---|
792 | //roar_buffer_get_len(buf, &len); |
---|
793 | |
---|
794 | /* |
---|
795 | if ( len > 0 ) // we still have some data in this buffer, re-inserting it to the input buffers... |
---|
796 | stream_unshift_buffer(id, buf); |
---|
797 | else |
---|
798 | buffer_delete(buf, NULL); |
---|
799 | */ |
---|
800 | |
---|
801 | ROAR_STREAM(g_streams[id])->pos = |
---|
802 | ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(g_streams[id])->pos, |
---|
803 | ROAR_OUTPUT_CALC_OUTBUFSAMP(info, needed-todo)); |
---|
804 | //ROAR_WARN("stream=%i, pos=%u", id, ((struct roar_stream*)g_streams[id])->pos); |
---|
805 | |
---|
806 | if ( todo > 0 ) { // zeroize the rest of the buffer |
---|
807 | memset(rest, 0, todo); |
---|
808 | |
---|
809 | if ( todo != ROAR_OUTPUT_CALC_OUTBUFSIZE(info) ) { |
---|
810 | if ( g_streams[id]->is_new ) { |
---|
811 | stream->pre_underruns++; |
---|
812 | } else { |
---|
813 | ROAR_WARN("streams_fill_mixbuffer(*): Underrun in stream: %u bytes missing, filling with zeros", (unsigned int)todo); |
---|
814 | stream->post_underruns++; |
---|
815 | } |
---|
816 | |
---|
817 | stream->is_new = 0; |
---|
818 | } |
---|
819 | } else { |
---|
820 | stream->is_new = 0; |
---|
821 | } |
---|
822 | |
---|
823 | return 0; |
---|
824 | } |
---|
825 | |
---|
826 | int streams_fill_mixbuffer2 (int id, struct roar_audio_info * info) { |
---|
827 | size_t outlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(info); |
---|
828 | void * outdata; |
---|
829 | size_t inlen; |
---|
830 | size_t inlen_got; |
---|
831 | void * indata = NULL; |
---|
832 | size_t buflen; |
---|
833 | void * bufdata = NULL; |
---|
834 | struct roar_buffer * bufbuf = NULL; |
---|
835 | int is_the_same = 0; |
---|
836 | struct roar_audio_info * stream_info; |
---|
837 | struct roar_stream * s; |
---|
838 | struct roar_stream_server * ss; |
---|
839 | |
---|
840 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
841 | return -1; |
---|
842 | |
---|
843 | // set up stream_info |
---|
844 | stream_info = &(s->info); |
---|
845 | |
---|
846 | // calc todo_in |
---|
847 | inlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info); |
---|
848 | |
---|
849 | buflen = ROAR_OUTPUT_CALC_OUTBUFSIZE_MAX(info, stream_info); |
---|
850 | |
---|
851 | if ( inlen == 0 ) { |
---|
852 | ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=%p{...}): inlen == 0, this should not happen!", id, info); |
---|
853 | return -1; |
---|
854 | } |
---|
855 | |
---|
856 | if ( streams_get_outputbuffer(id, &outdata, outlen) == -1 ) { |
---|
857 | return -1; |
---|
858 | } |
---|
859 | |
---|
860 | if ( outdata == NULL ) { |
---|
861 | return -1; |
---|
862 | } |
---|
863 | |
---|
864 | ROAR_DBG("streams_fill_mixbuffer2(*): outdata=%p, len=%i->%i (in->out)", outdata, inlen, outlen); |
---|
865 | |
---|
866 | is_the_same = stream_info->rate == info->rate && stream_info->bits == info->bits && |
---|
867 | stream_info->channels == info->channels && stream_info->codec == info->codec; |
---|
868 | |
---|
869 | ROAR_DBG("streams_fill_mixbuffer2(*): is_the_same=%i", is_the_same); |
---|
870 | |
---|
871 | if ( !is_the_same && buflen > outlen ) { |
---|
872 | /* |
---|
873 | // this is not supported at the moment |
---|
874 | memset(outdata, 0, outlen); |
---|
875 | return -1; |
---|
876 | */ |
---|
877 | |
---|
878 | if ( roar_buffer_new(&bufbuf, buflen) == -1 ) |
---|
879 | return -1; |
---|
880 | |
---|
881 | if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 ) |
---|
882 | return -1; |
---|
883 | indata = bufdata; |
---|
884 | } else { |
---|
885 | indata = outdata; |
---|
886 | bufdata = outdata; |
---|
887 | } |
---|
888 | |
---|
889 | inlen_got = inlen; |
---|
890 | |
---|
891 | ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=...): inlen_got=%u", id, inlen_got); |
---|
892 | |
---|
893 | if ( stream_shift_out_buffer(id, indata, &inlen_got) == -1 ) { |
---|
894 | if ( ss->is_new ) { |
---|
895 | ss->pre_underruns++; |
---|
896 | } else { |
---|
897 | ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=...): underrun in stream", id); |
---|
898 | ss->post_underruns++; |
---|
899 | } |
---|
900 | memset(outdata, 0, outlen); |
---|
901 | return 0; |
---|
902 | } |
---|
903 | |
---|
904 | if ( ss->is_new ) { |
---|
905 | ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=...): stream state: new->old", id); |
---|
906 | } |
---|
907 | |
---|
908 | ss->is_new = 0; |
---|
909 | |
---|
910 | if ( is_the_same ) { |
---|
911 | if ( indata != outdata ) |
---|
912 | memcpy(outdata, indata, inlen); |
---|
913 | |
---|
914 | if ( inlen < outlen ) |
---|
915 | memset(outdata+inlen, 0, outlen-inlen); |
---|
916 | } else { |
---|
917 | // if ( roar_conv(outdata, indata, (8*inlen_got*info->rate)/(stream_info->rate * stream_info->bits), stream_info, info) == -1 ) { |
---|
918 | if ( roar_conv2(bufdata, indata, inlen, stream_info, info, buflen) == -1 ) { |
---|
919 | if ( bufbuf != NULL ) |
---|
920 | roar_buffer_free(bufbuf); |
---|
921 | return -1; |
---|
922 | } |
---|
923 | |
---|
924 | // memset(outdata, 0, outlen); |
---|
925 | } |
---|
926 | |
---|
927 | if ( bufbuf != NULL ) { |
---|
928 | memcpy(outdata, bufdata, outlen); |
---|
929 | roar_buffer_free(bufbuf); |
---|
930 | } |
---|
931 | |
---|
932 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) { |
---|
933 | if ( change_vol(outdata, info->bits, outdata, 8*outlen / info->bits, info->channels, &(ss->mixer)) == -1 ) |
---|
934 | return -1; |
---|
935 | } |
---|
936 | |
---|
937 | if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) ) { |
---|
938 | // we can ignore errors here: |
---|
939 | if ( stream_outputbuffer_request(id, &bufbuf, outlen) == 0 ) { |
---|
940 | if ( roar_buffer_get_data(bufbuf, &bufdata) != -1 ) |
---|
941 | memcpy(bufdata, outdata, outlen); |
---|
942 | } |
---|
943 | } |
---|
944 | |
---|
945 | |
---|
946 | return 0; |
---|
947 | } |
---|
948 | |
---|
949 | |
---|
950 | int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) { |
---|
951 | static void * bufs[ROAR_STREAMS_MAX+1]; |
---|
952 | int i; |
---|
953 | int have = 0; |
---|
954 | |
---|
955 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
956 | if ( g_streams[i] != NULL ) { |
---|
957 | if ( ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_PLAY && ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_BIDIR ) |
---|
958 | continue; |
---|
959 | |
---|
960 | if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) { |
---|
961 | ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i); |
---|
962 | ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round."); |
---|
963 | continue; |
---|
964 | } |
---|
965 | if ( streams_fill_mixbuffer2(i, info) == -1 ) { |
---|
966 | ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i); |
---|
967 | continue; |
---|
968 | } |
---|
969 | |
---|
970 | // printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
971 | |
---|
972 | ROAR_DBG("streams_get_mixbuffers(*): bufs[have] = %p", bufs[have]); |
---|
973 | ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]); |
---|
974 | |
---|
975 | if ( !streams_get_flag(i, ROAR_FLAG_MUTE) ) |
---|
976 | have++; // we have a new stream! |
---|
977 | } |
---|
978 | } |
---|
979 | |
---|
980 | bufs[have] = NULL; |
---|
981 | //printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
982 | |
---|
983 | ROAR_DBG("streams_get_mixbuffers(*): have = %i", have); |
---|
984 | |
---|
985 | *bufferlist = bufs; |
---|
986 | return have; |
---|
987 | } |
---|
988 | |
---|
989 | |
---|
990 | int stream_add_buffer (int id, struct roar_buffer * buf) { |
---|
991 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
992 | |
---|
993 | if ( g_streams[id] == NULL ) |
---|
994 | return -1; |
---|
995 | |
---|
996 | if ( g_streams[id]->buffer == NULL ) { |
---|
997 | g_streams[id]->buffer = buf; |
---|
998 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf); |
---|
999 | return 0; |
---|
1000 | } |
---|
1001 | |
---|
1002 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
1003 | return roar_buffer_add(g_streams[id]->buffer, buf); |
---|
1004 | } |
---|
1005 | |
---|
1006 | int stream_shift_out_buffer (int id, void * data, size_t * len) { |
---|
1007 | if ( g_streams[id] == NULL ) |
---|
1008 | return -1; |
---|
1009 | |
---|
1010 | if ( g_streams[id]->buffer == NULL ) |
---|
1011 | return -1; |
---|
1012 | |
---|
1013 | return roar_buffer_shift_out(&(g_streams[id]->buffer), data, len); |
---|
1014 | } |
---|
1015 | |
---|
1016 | int stream_shift_buffer (int id, struct roar_buffer ** buf) { |
---|
1017 | struct roar_buffer * next; |
---|
1018 | |
---|
1019 | if ( g_streams[id] == NULL ) |
---|
1020 | return -1; |
---|
1021 | |
---|
1022 | if ( g_streams[id]->buffer == NULL ) { |
---|
1023 | *buf = NULL; |
---|
1024 | return 0; |
---|
1025 | } |
---|
1026 | |
---|
1027 | roar_buffer_get_next(g_streams[id]->buffer, &next); |
---|
1028 | |
---|
1029 | *buf = g_streams[id]->buffer; |
---|
1030 | g_streams[id]->buffer = next; |
---|
1031 | |
---|
1032 | return 0; |
---|
1033 | } |
---|
1034 | int stream_unshift_buffer (int id, struct roar_buffer * buf) { |
---|
1035 | if ( g_streams[id] == NULL ) |
---|
1036 | return -1; |
---|
1037 | |
---|
1038 | if ( g_streams[id]->buffer == NULL ) { |
---|
1039 | g_streams[id]->buffer = buf; |
---|
1040 | return 0; |
---|
1041 | } |
---|
1042 | |
---|
1043 | buf->next = NULL; |
---|
1044 | |
---|
1045 | roar_buffer_add(buf, g_streams[id]->buffer); |
---|
1046 | |
---|
1047 | g_streams[id]->buffer = buf; |
---|
1048 | |
---|
1049 | return 0; |
---|
1050 | } |
---|
1051 | |
---|
1052 | int stream_outputbuffer_request(int id, struct roar_buffer ** buf, size_t len) { |
---|
1053 | register struct roar_stream_server * ss; |
---|
1054 | size_t buflen; |
---|
1055 | void * bufdata; |
---|
1056 | int ret; |
---|
1057 | |
---|
1058 | if ( (ss = g_streams[id]) == NULL ) |
---|
1059 | return -1; |
---|
1060 | |
---|
1061 | if ( buf != NULL ) /* just be be sure */ |
---|
1062 | *buf = NULL; |
---|
1063 | |
---|
1064 | if ( ss->outputbuffer != NULL ) { |
---|
1065 | if ( roar_buffer_get_len(ss->outputbuffer, &buflen) == 0 ) { |
---|
1066 | if ( buflen == len ) { |
---|
1067 | if ( buf != NULL ) |
---|
1068 | *buf = ss->outputbuffer; |
---|
1069 | return 0; |
---|
1070 | } else if ( buflen > len ) { |
---|
1071 | if ( roar_buffer_set_len(ss->outputbuffer, len) == 0 ) { |
---|
1072 | if ( buf != NULL ) |
---|
1073 | *buf = ss->outputbuffer; |
---|
1074 | return 0; |
---|
1075 | } |
---|
1076 | } |
---|
1077 | } |
---|
1078 | |
---|
1079 | // if the buffer is not suitable: |
---|
1080 | |
---|
1081 | ret = roar_buffer_free(ss->outputbuffer); |
---|
1082 | ss->outputbuffer = NULL; |
---|
1083 | if ( ret == -1 ) |
---|
1084 | return ret; |
---|
1085 | } |
---|
1086 | |
---|
1087 | if ( roar_buffer_new(&(ss->outputbuffer), len) == -1 ) |
---|
1088 | return -1; |
---|
1089 | |
---|
1090 | if ( roar_buffer_get_data(ss->outputbuffer, &bufdata) == -1 ) { |
---|
1091 | roar_buffer_free(ss->outputbuffer); |
---|
1092 | ss->outputbuffer = NULL; |
---|
1093 | return -1; |
---|
1094 | } |
---|
1095 | |
---|
1096 | memset(bufdata, 0, len); |
---|
1097 | |
---|
1098 | if ( buf != NULL ) |
---|
1099 | *buf = ss->outputbuffer; |
---|
1100 | |
---|
1101 | return 0; |
---|
1102 | } |
---|
1103 | |
---|
1104 | int stream_outputbuffer_destroy(int id) { |
---|
1105 | int ret; |
---|
1106 | register struct roar_stream_server * ss; |
---|
1107 | |
---|
1108 | if ( (ss = g_streams[id]) == NULL ) |
---|
1109 | return -1; |
---|
1110 | |
---|
1111 | if ( ss->outputbuffer != NULL ) { |
---|
1112 | ret = roar_buffer_free(ss->outputbuffer); |
---|
1113 | ss->outputbuffer = NULL; |
---|
1114 | return ret; |
---|
1115 | } |
---|
1116 | |
---|
1117 | return 0; |
---|
1118 | } |
---|
1119 | |
---|
1120 | int streams_check (int id) { |
---|
1121 | int fh; |
---|
1122 | int i; |
---|
1123 | ssize_t req, realreq, done; |
---|
1124 | struct roar_stream * s; |
---|
1125 | struct roar_stream_server * ss; |
---|
1126 | struct roar_buffer * b; |
---|
1127 | char * buf; |
---|
1128 | |
---|
1129 | if ( g_streams[id] == NULL ) |
---|
1130 | return -1; |
---|
1131 | |
---|
1132 | ROAR_DBG("streams_check(id=%i) = ?", id); |
---|
1133 | |
---|
1134 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
1135 | |
---|
1136 | if ( (fh = s->fh) == -1 ) |
---|
1137 | return 0; |
---|
1138 | |
---|
1139 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
1140 | return 0; |
---|
1141 | |
---|
1142 | switch (s->dir) { |
---|
1143 | case ROAR_DIR_LIGHT_IN: |
---|
1144 | return light_check_stream(id); |
---|
1145 | break; |
---|
1146 | case ROAR_DIR_MIDI_IN: |
---|
1147 | return midi_check_stream(id); |
---|
1148 | break; |
---|
1149 | case ROAR_DIR_PLAY: |
---|
1150 | case ROAR_DIR_BIDIR: |
---|
1151 | break; |
---|
1152 | default: |
---|
1153 | return 0; |
---|
1154 | break; |
---|
1155 | } |
---|
1156 | |
---|
1157 | ROAR_DBG("streams_check(id=%i): fh = %i", id, fh); |
---|
1158 | |
---|
1159 | req = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size |
---|
1160 | req += ss->need_extra; // bytes left we sould get.... |
---|
1161 | |
---|
1162 | if ( roar_buffer_new(&b, req) == -1 ) { |
---|
1163 | ROAR_ERR("streams_check(*): Can not alloc buffer space!"); |
---|
1164 | ROAR_DBG("streams_check(*) = -1"); |
---|
1165 | return -1; |
---|
1166 | } |
---|
1167 | |
---|
1168 | roar_buffer_get_data(b, (void **)&buf); |
---|
1169 | |
---|
1170 | ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id); |
---|
1171 | |
---|
1172 | if ( ss->codecfilter == -1 ) { |
---|
1173 | realreq = req; |
---|
1174 | /* |
---|
1175 | req = read(fh, buf, req); |
---|
1176 | if ( req < realreq ) { // we can do this as the stream is in nonblocking mode! |
---|
1177 | if ( (realreq = read(fh, buf+req, realreq-req)) > 0 ) |
---|
1178 | req += realreq; |
---|
1179 | } |
---|
1180 | */ |
---|
1181 | done = 0; |
---|
1182 | while (req > 0 && done != realreq) { |
---|
1183 | if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 ) |
---|
1184 | done += req; |
---|
1185 | } |
---|
1186 | req = done; |
---|
1187 | |
---|
1188 | roar_buffer_get_data(b, (void **)&buf); |
---|
1189 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
1190 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) { |
---|
1191 | if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) { |
---|
1192 | if ( stream_vio_write(i, buf, req) != req ) { |
---|
1193 | streams_delete(i); |
---|
1194 | } |
---|
1195 | } |
---|
1196 | } |
---|
1197 | } |
---|
1198 | } else { |
---|
1199 | req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req); |
---|
1200 | } |
---|
1201 | |
---|
1202 | if ( req > 0 ) { |
---|
1203 | ROAR_DBG("streams_check(id=%i): got %i bytes", id, req); |
---|
1204 | |
---|
1205 | roar_buffer_set_len(b, req); |
---|
1206 | |
---|
1207 | if ( stream_add_buffer(id, b) != -1 ) |
---|
1208 | return 0; |
---|
1209 | |
---|
1210 | ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id); |
---|
1211 | roar_buffer_free(b); |
---|
1212 | } else { |
---|
1213 | ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno)); |
---|
1214 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
1215 | if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :) |
---|
1216 | #else |
---|
1217 | if ( errno != EAGAIN ) { |
---|
1218 | #endif |
---|
1219 | ROAR_DBG("streams_check(id=%i): EOF!", id); |
---|
1220 | streams_delete(id); |
---|
1221 | ROAR_DBG("streams_check(id=%i) = 0", id); |
---|
1222 | } |
---|
1223 | roar_buffer_free(b); |
---|
1224 | return 0; |
---|
1225 | } |
---|
1226 | |
---|
1227 | |
---|
1228 | ROAR_DBG("streams_check(id=%i) = -1", id); |
---|
1229 | return -1; |
---|
1230 | } |
---|
1231 | |
---|
1232 | |
---|
1233 | #define _return(x) return (x) |
---|
1234 | int streams_send_mon (int id) { |
---|
1235 | // int fh; |
---|
1236 | struct roar_stream * s; |
---|
1237 | struct roar_stream_server * ss; |
---|
1238 | struct roar_buffer * bufbuf = NULL; |
---|
1239 | struct roar_remove_state removalstate; |
---|
1240 | void * ip; |
---|
1241 | void * obuf; |
---|
1242 | int olen; |
---|
1243 | int is_the_same = 1; |
---|
1244 | int is_vol_eq = 1; |
---|
1245 | int antiecho = 0; |
---|
1246 | ssize_t ret; |
---|
1247 | |
---|
1248 | if ( g_streams[id] == NULL ) |
---|
1249 | return -1; |
---|
1250 | |
---|
1251 | ROAR_DBG("streams_send_mon(id=%i) = ?", id); |
---|
1252 | |
---|
1253 | s = ROAR_STREAM((ss = g_streams[id])); |
---|
1254 | |
---|
1255 | /* |
---|
1256 | if ( (fh = s->fh) == -1 ) |
---|
1257 | return 0; |
---|
1258 | */ |
---|
1259 | |
---|
1260 | if ( !ss->ready ) |
---|
1261 | return 0; |
---|
1262 | |
---|
1263 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
1264 | return 0; |
---|
1265 | |
---|
1266 | switch (s->dir) { |
---|
1267 | case ROAR_DIR_LIGHT_OUT: |
---|
1268 | return light_send_stream(id); |
---|
1269 | break; |
---|
1270 | case ROAR_DIR_MIDI_OUT: |
---|
1271 | return midi_send_stream(id); |
---|
1272 | break; |
---|
1273 | case ROAR_DIR_OUTPUT: |
---|
1274 | if ( g_standby ) |
---|
1275 | return 0; |
---|
1276 | case ROAR_DIR_MONITOR: |
---|
1277 | case ROAR_DIR_BIDIR: |
---|
1278 | break; |
---|
1279 | |
---|
1280 | default: |
---|
1281 | return 0; |
---|
1282 | break; |
---|
1283 | } |
---|
1284 | |
---|
1285 | |
---|
1286 | ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, s->fh); |
---|
1287 | |
---|
1288 | if ( s->info.channels != g_sa->channels || s->info.bits != g_sa->bits || |
---|
1289 | s->info.rate != g_sa->rate || s->info.codec != g_sa->codec ) |
---|
1290 | is_the_same = 0; |
---|
1291 | |
---|
1292 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) { |
---|
1293 | is_vol_eq = need_vol_change(g_sa->channels, &(ss->mixer)) ? 0 : 1; |
---|
1294 | } |
---|
1295 | |
---|
1296 | if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) ) |
---|
1297 | antiecho = 1; |
---|
1298 | |
---|
1299 | if ( !is_the_same || !is_vol_eq || antiecho ) { |
---|
1300 | olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len |
---|
1301 | // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here |
---|
1302 | if ( stream_outputbuffer_request(id, &bufbuf, olen) == -1 ) |
---|
1303 | return -1; |
---|
1304 | |
---|
1305 | if ( roar_buffer_get_data(bufbuf, &obuf) == -1 ) { |
---|
1306 | _return(-1); |
---|
1307 | } |
---|
1308 | |
---|
1309 | ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen); |
---|
1310 | } else { |
---|
1311 | obuf = g_output_buffer; |
---|
1312 | olen = g_output_buffer_len; |
---|
1313 | } |
---|
1314 | |
---|
1315 | ip = g_output_buffer; |
---|
1316 | |
---|
1317 | if ( antiecho ) { |
---|
1318 | if ( roar_remove_init(&removalstate) == -1 ) { |
---|
1319 | _return(-1); |
---|
1320 | } |
---|
1321 | |
---|
1322 | if ( roar_remove_so(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->bits, &removalstate) == -1 ) { |
---|
1323 | _return(-1); |
---|
1324 | } |
---|
1325 | } |
---|
1326 | |
---|
1327 | if ( !is_vol_eq ) { |
---|
1328 | if ( change_vol(obuf, g_sa->bits, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->channels, &(ss->mixer)) == -1 ) { |
---|
1329 | _return(-1); |
---|
1330 | } |
---|
1331 | |
---|
1332 | ip = obuf; |
---|
1333 | } |
---|
1334 | |
---|
1335 | if ( !is_the_same ) { |
---|
1336 | if ( roar_conv(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) { |
---|
1337 | _return(-1); |
---|
1338 | } |
---|
1339 | } |
---|
1340 | |
---|
1341 | errno = 0; |
---|
1342 | |
---|
1343 | if ( ss->codecfilter == -1 ) { |
---|
1344 | ROAR_DBG("streams_send_mon(id=%i): not a CF stream", id); |
---|
1345 | if ( s->fh == -1 && roar_vio_get_fh(&(ss->vio)) == -1 ) { |
---|
1346 | _return(0); |
---|
1347 | } |
---|
1348 | |
---|
1349 | if ( (ret = stream_vio_s_write(ss, obuf, olen)) == olen ) { |
---|
1350 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
1351 | _return(0); |
---|
1352 | } |
---|
1353 | |
---|
1354 | if ( ret > 0 && errno == 0 ) { |
---|
1355 | 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); |
---|
1356 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), ret)*s->info.channels); |
---|
1357 | _return(0); |
---|
1358 | } |
---|
1359 | } else { |
---|
1360 | errno = 0; |
---|
1361 | if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen) |
---|
1362 | == olen ) { |
---|
1363 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
1364 | _return(0); |
---|
1365 | } else { // we cann't retry on codec filetered streams |
---|
1366 | if ( errno != EAGAIN ) { |
---|
1367 | streams_delete(id); |
---|
1368 | _return(-1); |
---|
1369 | } |
---|
1370 | } |
---|
1371 | } |
---|
1372 | |
---|
1373 | if ( errno == EAGAIN ) { |
---|
1374 | // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to |
---|
1375 | // make any gapes in any output because of this |
---|
1376 | |
---|
1377 | #ifdef ROAR_HAVE_USLEEP |
---|
1378 | usleep(100); // 0.1ms |
---|
1379 | #endif |
---|
1380 | |
---|
1381 | if ( stream_vio_s_write(ss, obuf, olen) == olen ) { |
---|
1382 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
1383 | _return(0); |
---|
1384 | } else if ( errno == EAGAIN ) { |
---|
1385 | ROAR_WARN("streams_send_mon(id=%i): Can not send data to client: %s", id, strerror(errno)); |
---|
1386 | _return(0); |
---|
1387 | } |
---|
1388 | } |
---|
1389 | |
---|
1390 | // ug... error... delete stream! |
---|
1391 | |
---|
1392 | streams_delete(id); |
---|
1393 | |
---|
1394 | _return(-1); |
---|
1395 | } |
---|
1396 | #undef _return |
---|
1397 | |
---|
1398 | int streams_send_filter(int id) { |
---|
1399 | int fh; |
---|
1400 | int have = 0; |
---|
1401 | int len; |
---|
1402 | struct roar_stream * s; |
---|
1403 | struct roar_stream_server * ss; |
---|
1404 | |
---|
1405 | if ( g_streams[id] == NULL ) |
---|
1406 | return -1; |
---|
1407 | |
---|
1408 | ROAR_DBG("streams_send_filter(id=%i) = ?", id); |
---|
1409 | |
---|
1410 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
1411 | |
---|
1412 | if ( (fh = s->fh) == -1 ) |
---|
1413 | return 0; |
---|
1414 | |
---|
1415 | if ( s->dir != ROAR_DIR_FILTER ) |
---|
1416 | return 0; |
---|
1417 | |
---|
1418 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
1419 | return 0; |
---|
1420 | |
---|
1421 | |
---|
1422 | ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh); |
---|
1423 | |
---|
1424 | if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) { |
---|
1425 | while ( have < g_output_buffer_len ) { |
---|
1426 | if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) { |
---|
1427 | streams_delete(id); |
---|
1428 | return -1; |
---|
1429 | } |
---|
1430 | have += len; |
---|
1431 | } |
---|
1432 | return 0; |
---|
1433 | } |
---|
1434 | |
---|
1435 | // ug... error... delete stream! |
---|
1436 | |
---|
1437 | streams_delete(id); |
---|
1438 | |
---|
1439 | return -1; |
---|
1440 | } |
---|
1441 | |
---|
1442 | |
---|
1443 | // VIO: |
---|
1444 | |
---|
1445 | ssize_t stream_vio_read (int stream, void *buf, size_t count) { |
---|
1446 | struct roar_stream_server * s = g_streams[stream]; |
---|
1447 | |
---|
1448 | if ( !s ) |
---|
1449 | return -1; |
---|
1450 | |
---|
1451 | return stream_vio_s_read(s, buf, count); |
---|
1452 | } |
---|
1453 | |
---|
1454 | ssize_t stream_vio_write(int stream, void *buf, size_t count) { |
---|
1455 | struct roar_stream_server * s = g_streams[stream]; |
---|
1456 | |
---|
1457 | if ( !s ) |
---|
1458 | return -1; |
---|
1459 | |
---|
1460 | return stream_vio_s_write(s, buf, count); |
---|
1461 | } |
---|
1462 | |
---|
1463 | |
---|
1464 | ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) { |
---|
1465 | size_t len = 0; |
---|
1466 | ssize_t r = -1; |
---|
1467 | |
---|
1468 | errno = 0; |
---|
1469 | |
---|
1470 | if ( !stream ) |
---|
1471 | return -1; |
---|
1472 | |
---|
1473 | //roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
1474 | |
---|
1475 | if ( ! stream->vio.read ) |
---|
1476 | return -1; |
---|
1477 | |
---|
1478 | while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) { |
---|
1479 | len += r; |
---|
1480 | buf += r; |
---|
1481 | count -= r; |
---|
1482 | if ( count == 0 ) |
---|
1483 | break; |
---|
1484 | } |
---|
1485 | |
---|
1486 | if ( len == 0 && r == -1 ) |
---|
1487 | return -1; |
---|
1488 | |
---|
1489 | return len; |
---|
1490 | } |
---|
1491 | |
---|
1492 | ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) { |
---|
1493 | errno = 0; |
---|
1494 | |
---|
1495 | if ( !stream ) |
---|
1496 | return -1; |
---|
1497 | |
---|
1498 | /* |
---|
1499 | if ( roar_vio_get_fh(&(stream->vio)) == -1 && ROAR_STREAM(stream)->fh != -1 ) |
---|
1500 | roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
1501 | */ |
---|
1502 | |
---|
1503 | // ROAR_WARN("stream_vio_s_write(*): writing..."); |
---|
1504 | |
---|
1505 | return roar_vio_write(&(stream->vio), buf, count); |
---|
1506 | } |
---|
1507 | |
---|
1508 | //ll |
---|