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