1 | //streams.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010 |
---|
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, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | #include "roard.h" |
---|
27 | |
---|
28 | #define _CHECK_SID_RET(id,ret) if ( (id) < 0 || (id) > ROAR_STREAMS_MAX || g_streams[(id)] == NULL ) return (ret) |
---|
29 | #define _CHECK_SID(id) _CHECK_SID_RET((id), -1) |
---|
30 | |
---|
31 | int streams_thru_num = 0; |
---|
32 | int streams_recsource_id = -1; |
---|
33 | |
---|
34 | int streams_init (void) { |
---|
35 | int i; |
---|
36 | |
---|
37 | for (i = 0; i < ROAR_STREAMS_MAX; i++) |
---|
38 | g_streams[i] = NULL; |
---|
39 | |
---|
40 | return 0; |
---|
41 | } |
---|
42 | |
---|
43 | int streams_free (void) { |
---|
44 | int i; |
---|
45 | |
---|
46 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
47 | if ( g_streams[i] != NULL ) { |
---|
48 | streams_delete(i); |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | return 0; |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | int streams_new (void) { |
---|
57 | int i, j; |
---|
58 | struct roar_stream * n = NULL; |
---|
59 | struct roar_stream_server * s = NULL; |
---|
60 | |
---|
61 | #ifdef ROAR_SUPPORT_LISTEN |
---|
62 | if ( g_terminate && !g_no_listen ) // don't accept new streams in case of termination state |
---|
63 | return -1; |
---|
64 | #else |
---|
65 | if ( g_terminate ) // don't accept new streams in case of termination state |
---|
66 | return -1; |
---|
67 | #endif |
---|
68 | |
---|
69 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
70 | if ( g_streams[i] == NULL ) { |
---|
71 | s = ROAR_STREAM_SERVER(n = ROAR_STREAM(roar_mm_malloc(sizeof(struct roar_stream_server)))); |
---|
72 | if ( n == NULL ) { |
---|
73 | ROAR_ERR("streams_new(void): can not allocate memory for new stream: %s", strerror(errno)); |
---|
74 | ROAR_DBG("streams_new(void) = -1"); |
---|
75 | return -1; |
---|
76 | } |
---|
77 | |
---|
78 | memset(s, 0, sizeof(struct roar_stream_server)); |
---|
79 | |
---|
80 | n->id = i; |
---|
81 | n->fh = -1; |
---|
82 | n->pos_rel_id = -1; |
---|
83 | /* |
---|
84 | n->database = NULL; |
---|
85 | n->dataoff = NULL; |
---|
86 | n->datalen = 0; |
---|
87 | n->offset = 0; |
---|
88 | */ |
---|
89 | n->pos = 0; |
---|
90 | |
---|
91 | s->name = NULL; |
---|
92 | |
---|
93 | s->state = ROAR_STREAMSTATE_INITING; |
---|
94 | |
---|
95 | s->client = -1; |
---|
96 | s->socktype = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
97 | s->buffer = NULL; |
---|
98 | s->need_extra = 0; |
---|
99 | s->output = NULL; |
---|
100 | s->is_new = 1; |
---|
101 | s->codecfilter = -1; |
---|
102 | s->pre_underruns = 0; |
---|
103 | s->post_underruns = 0; |
---|
104 | s->delay = 0; |
---|
105 | s->codec_orgi = -1; |
---|
106 | s->primary = 0; |
---|
107 | s->ready = 0; |
---|
108 | s->outputbuffer = NULL; |
---|
109 | s->prethru = NULL; |
---|
110 | s->mixer_stream = -1; |
---|
111 | s->role = ROAR_ROLE_UNKNOWN; |
---|
112 | s->parent_stream = -1; |
---|
113 | |
---|
114 | s->mixer.scale = 65535; |
---|
115 | s->mixer.rpg_mul = 1; |
---|
116 | s->mixer.rpg_div = 1; |
---|
117 | for (j = 0; j < ROAR_MAX_CHANNELS; j++) |
---|
118 | s->mixer.mixer[j] = 65535; |
---|
119 | |
---|
120 | #ifdef ROAR_SUPPORT_META |
---|
121 | for (j = 0; j < ROAR_META_MAX_PER_STREAM; j++) { |
---|
122 | s->meta[j].type = ROAR_META_TYPE_NONE; |
---|
123 | s->meta[j].key[0] = 0; |
---|
124 | s->meta[j].value = NULL; |
---|
125 | } |
---|
126 | #endif |
---|
127 | |
---|
128 | roar_vio_init_calls(&(s->vio)); |
---|
129 | roar_vio_init_calls(&(s->jumbo)); |
---|
130 | s->viop = &(s->vio); |
---|
131 | s->driver_id = -1; |
---|
132 | s->flags = ROAR_FLAG_NONE; |
---|
133 | |
---|
134 | //roardsp_fchain_init(&(s->fc)); |
---|
135 | |
---|
136 | g_streams[i] = s; |
---|
137 | counters_inc(streams, 1); |
---|
138 | ROAR_INFO("streams_new(void): New stream %i created (state=initing)", ROAR_DBG_INFO_VERBOSE, i); |
---|
139 | ROAR_DBG("streams_new(void): n->id=%i", n->id); |
---|
140 | ROAR_DBG("streams_new(void) = %i", i); |
---|
141 | return i; |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | return -1; |
---|
146 | } |
---|
147 | |
---|
148 | int streams_delete (int id) { |
---|
149 | struct roar_stream_server * s; |
---|
150 | int prim; |
---|
151 | int no_vio_close = 0; |
---|
152 | int i; |
---|
153 | int client; |
---|
154 | |
---|
155 | _CHECK_SID(id); |
---|
156 | |
---|
157 | if ( (s = g_streams[id]) == NULL ) |
---|
158 | return 0; |
---|
159 | |
---|
160 | ROAR_DBG("streams_delete(id=%i) = ?", id); |
---|
161 | ROAR_DBG("streams_delete(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(s)->id); |
---|
162 | |
---|
163 | // in case we are allready closing it... |
---|
164 | if ( s->state == ROAR_STREAMSTATE_CLOSING ) |
---|
165 | return 0; |
---|
166 | |
---|
167 | ROAR_INFO("streams_delete(id=%i): stream state: %s->closing", ROAR_DBG_INFO_VERBOSE, id, roar_streamstate2str(s->state)); |
---|
168 | s->state = ROAR_STREAMSTATE_CLOSING; |
---|
169 | |
---|
170 | counters_inc(streams, -1); |
---|
171 | |
---|
172 | if ( streams_get_flag(id, ROAR_FLAG_RECSOURCE) == 1 ) |
---|
173 | streams_reset_flag(id, ROAR_FLAG_RECSOURCE); |
---|
174 | |
---|
175 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
176 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) { |
---|
177 | switch (ROAR_STREAM(g_streams[i])->dir) { |
---|
178 | case ROAR_DIR_THRU: |
---|
179 | case ROAR_DIR_RAW_IN: |
---|
180 | if ( i != id ) |
---|
181 | streams_delete(i); |
---|
182 | break; |
---|
183 | default: |
---|
184 | if ( streams_get_flag(i, ROAR_FLAG_VIRTUAL) == 1 ) { |
---|
185 | if ( i != id ) { |
---|
186 | ROAR_DBG("streams_delete(id=%i): Deleting virtual child stream %i", id, i); |
---|
187 | streams_delete(i); |
---|
188 | } |
---|
189 | } else { |
---|
190 | ROAR_STREAM(g_streams[i])->pos_rel_id = -1; |
---|
191 | } |
---|
192 | } |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | if ( ROAR_STREAM(s)->dir == ROAR_DIR_THRU ) |
---|
197 | streams_thru_num--; |
---|
198 | |
---|
199 | if ( streams_get_flag(id, ROAR_FLAG_VIRTUAL) == 1 ) { |
---|
200 | // we un-group the stream here to avoid a client deleting the parent deleting the client deleting ... |
---|
201 | i = ROAR_STREAM(s)->pos_rel_id; |
---|
202 | if ( i != -1 ) { |
---|
203 | ROAR_STREAM(s)->pos_rel_id = -1; |
---|
204 | client = streams_get_client(id); |
---|
205 | streams_set_client(id, -1); |
---|
206 | ROAR_DBG("streams_delete(id=%i): Stream has flag virtual, notifying parent stream %i", id, i); |
---|
207 | streams_ctl(i, ROAR_CODECFILTER_CTL_VIRTUAL_DELETE|ROAR_STREAM_CTL_TYPE_INT, &id); |
---|
208 | ROAR_DBG("streams_delete(id=%i): Notify send to stream %i", id, i); |
---|
209 | streams_set_client(id, client); |
---|
210 | ROAR_STREAM(s)->pos_rel_id = i; |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | #ifdef ROAR_SUPPORT_META |
---|
215 | // delete meta data form other meta streams if needed |
---|
216 | if ( streams_get_flag(id, ROAR_FLAG_META) == 1 ) { |
---|
217 | ROAR_DBG("streams_delete(id=%i): deleting meta stream!", id); |
---|
218 | stream_meta_clear(id); |
---|
219 | stream_meta_finalize(id); |
---|
220 | } |
---|
221 | #endif |
---|
222 | |
---|
223 | if ( s->codecfilter != -1 ) { |
---|
224 | codecfilter_close(s->codecfilter_inst, s->codecfilter); |
---|
225 | s->codecfilter_inst = NULL; |
---|
226 | s->codecfilter = -1; |
---|
227 | } |
---|
228 | |
---|
229 | if ( s->driver_id != -1 ) { |
---|
230 | driver_closevio(&(s->vio), s->driver_id); |
---|
231 | roar_vio_init_calls(&(s->vio)); |
---|
232 | s->driver_id = -1; |
---|
233 | no_vio_close = 1; |
---|
234 | } |
---|
235 | |
---|
236 | //roardsp_fchain_uninit(&(s->fc)); |
---|
237 | |
---|
238 | if ( s->client != -1 ) { |
---|
239 | ROAR_DBG("streams_delete(id=%i): Stream is owned by client %i", id, g_streams[id]->client); |
---|
240 | client_stream_delete(s->client, id); |
---|
241 | } |
---|
242 | |
---|
243 | stream_outputbuffer_destroy(id); |
---|
244 | stream_prethru_destroy(id); |
---|
245 | |
---|
246 | if ( s->buffer != NULL ) |
---|
247 | roar_buffer_free(s->buffer); |
---|
248 | |
---|
249 | if ( s->output != NULL ) |
---|
250 | free(s->output); |
---|
251 | |
---|
252 | /* |
---|
253 | if ( ROAR_STREAM(s)->fh != -1 ) |
---|
254 | close(ROAR_STREAM(s)->fh); |
---|
255 | */ |
---|
256 | |
---|
257 | if ( !no_vio_close ) |
---|
258 | roar_vio_close(s->viop); |
---|
259 | |
---|
260 | prim = s->primary; |
---|
261 | |
---|
262 | if ( s->name != NULL ) |
---|
263 | free(s->name); |
---|
264 | |
---|
265 | roar_mm_free(s); |
---|
266 | |
---|
267 | g_streams[id] = NULL; |
---|
268 | |
---|
269 | if ( prim && alive ) { |
---|
270 | alive = 0; |
---|
271 | clean_quit(); |
---|
272 | } |
---|
273 | |
---|
274 | ROAR_INFO("streams_delete(id=%i): stream deleted", ROAR_DBG_INFO_VERBOSE, id); |
---|
275 | ROAR_DBG("streams_delete(id=%i) = 0", id); |
---|
276 | return 0; |
---|
277 | } |
---|
278 | |
---|
279 | int streams_set_client (int id, int client) { |
---|
280 | |
---|
281 | _CHECK_SID(id); |
---|
282 | |
---|
283 | ROAR_DBG("streams_set_client(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id); |
---|
284 | g_streams[id]->client = client; |
---|
285 | |
---|
286 | return 0; |
---|
287 | } |
---|
288 | |
---|
289 | int streams_get_client (int id) { |
---|
290 | _CHECK_SID(id); |
---|
291 | |
---|
292 | return g_streams[id]->client; |
---|
293 | } |
---|
294 | |
---|
295 | int streams_set_dir (int id, int dir, int defaults) { |
---|
296 | struct roar_stream_server * ss; |
---|
297 | |
---|
298 | _CHECK_SID(id); |
---|
299 | |
---|
300 | if ( (ss = g_streams[id]) == NULL ) |
---|
301 | return -1; |
---|
302 | |
---|
303 | ROAR_STREAM(ss)->dir = dir; |
---|
304 | |
---|
305 | if ( dir == ROAR_DIR_THRU ) |
---|
306 | streams_thru_num++; |
---|
307 | |
---|
308 | if ( defaults ) { |
---|
309 | if ( dir <= 0 || dir >= ROAR_DIR_DIRIDS ) |
---|
310 | return -1; |
---|
311 | |
---|
312 | ROAR_DBG("streams_set_dir(*): g_config->streams[dir=%i].flags = 0x%.4x", dir, g_config->streams[dir].flags); |
---|
313 | |
---|
314 | if ( streams_set_flag(id, g_config->streams[dir].flags) == -1 ) { |
---|
315 | ROAR_DBG("streams_set_dir(*) = -1 // can not set stream flags"); |
---|
316 | return -1; |
---|
317 | } |
---|
318 | |
---|
319 | ss->mixer.scale = g_config->streams[dir].mixer.scale; |
---|
320 | ss->mixer.rpg_mul = g_config->streams[dir].mixer.rpg_mul; |
---|
321 | ss->mixer.rpg_div = g_config->streams[dir].mixer.rpg_div; |
---|
322 | } |
---|
323 | |
---|
324 | if ( dir != ROAR_DIR_MIXING ) { |
---|
325 | switch (streams_get_subsys(id)) { |
---|
326 | case ROAR_SUBSYS_WAVEFORM: |
---|
327 | streams_set_mixer_stream(id, g_waveform_mixer.stream); |
---|
328 | roardsp_chanlist_init(ss->chanmap.in, ROAR_STREAM(ss)->info.channels, ROARDSP_CHANLIST_MAP_ROARAUDIO); |
---|
329 | break; |
---|
330 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
331 | case ROAR_SUBSYS_MIDI: |
---|
332 | roardsp_chanlist_init(ss->chanmap.in, ROAR_STREAM(ss)->info.channels, ROARDSP_CHANLIST_MAP_MIDI); |
---|
333 | streams_set_mixer_stream(id, g_midi_mixer.stream); |
---|
334 | break; |
---|
335 | #endif |
---|
336 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
337 | case ROAR_SUBSYS_LIGHT: |
---|
338 | streams_set_mixer_stream(id, g_light_mixer.stream); |
---|
339 | break; |
---|
340 | #endif |
---|
341 | } |
---|
342 | |
---|
343 | memcpy(ss->chanmap.out, ss->chanmap.in, sizeof(ss->chanmap.out)); |
---|
344 | streams_set_map(id, NULL, 0); |
---|
345 | } else { |
---|
346 | streams_set_mixer_stream(id, id); |
---|
347 | } |
---|
348 | |
---|
349 | ROAR_DBG("streams_set_dir(*) = 0"); |
---|
350 | return 0; |
---|
351 | } |
---|
352 | |
---|
353 | int streams_get_dir (int id) { |
---|
354 | struct roar_stream_server * ss; |
---|
355 | |
---|
356 | _CHECK_SID(id); |
---|
357 | |
---|
358 | if ( (ss = g_streams[id]) == NULL ) |
---|
359 | return -1; |
---|
360 | |
---|
361 | return ROAR_STREAM(ss)->dir; |
---|
362 | } |
---|
363 | |
---|
364 | int streams_set_mixer_stream(int id, int mixer) { |
---|
365 | struct roar_stream_server * ss; |
---|
366 | |
---|
367 | _CHECK_SID(id); |
---|
368 | |
---|
369 | if ( (ss = g_streams[id]) == NULL ) |
---|
370 | return -1; |
---|
371 | |
---|
372 | ss->mixer_stream = mixer; |
---|
373 | |
---|
374 | return 0; |
---|
375 | } |
---|
376 | |
---|
377 | int streams_get_mixer_stream(int id, int mixer) { |
---|
378 | struct roar_stream_server * ss; |
---|
379 | |
---|
380 | _CHECK_SID(id); |
---|
381 | |
---|
382 | if ( (ss = g_streams[id]) == NULL ) |
---|
383 | return -1; |
---|
384 | |
---|
385 | return ss->mixer_stream; |
---|
386 | } |
---|
387 | |
---|
388 | int streams_set_role (int id, int role) { |
---|
389 | struct roar_stream_server * ss; |
---|
390 | |
---|
391 | _CHECK_SID(id); |
---|
392 | |
---|
393 | if ( (ss = g_streams[id]) == NULL ) |
---|
394 | return -1; |
---|
395 | |
---|
396 | ss->role = role; |
---|
397 | |
---|
398 | return 0; |
---|
399 | } |
---|
400 | |
---|
401 | int streams_get_subsys (int id) { |
---|
402 | struct roar_stream_server * ss; |
---|
403 | |
---|
404 | _CHECK_SID(id); |
---|
405 | |
---|
406 | if ( (ss = g_streams[id]) == NULL ) |
---|
407 | return -1; |
---|
408 | |
---|
409 | switch (ROAR_STREAM(ss)->dir) { |
---|
410 | case ROAR_DIR_PLAY: |
---|
411 | case ROAR_DIR_RECORD: |
---|
412 | case ROAR_DIR_MONITOR: |
---|
413 | case ROAR_DIR_FILTER: |
---|
414 | case ROAR_DIR_OUTPUT: |
---|
415 | case ROAR_DIR_BIDIR: |
---|
416 | return ROAR_SUBSYS_WAVEFORM; |
---|
417 | break; |
---|
418 | case ROAR_DIR_MIDI_IN: |
---|
419 | case ROAR_DIR_MIDI_OUT: |
---|
420 | return ROAR_SUBSYS_MIDI; |
---|
421 | break; |
---|
422 | case ROAR_DIR_LIGHT_IN: |
---|
423 | case ROAR_DIR_LIGHT_OUT: |
---|
424 | return ROAR_SUBSYS_LIGHT; |
---|
425 | break; |
---|
426 | case ROAR_DIR_RAW_IN: |
---|
427 | case ROAR_DIR_RAW_OUT: |
---|
428 | return ROAR_SUBSYS_RAW; |
---|
429 | break; |
---|
430 | case ROAR_DIR_COMPLEX_IN: |
---|
431 | case ROAR_DIR_COMPLEX_OUT: |
---|
432 | return ROAR_SUBSYS_COMPLEX; |
---|
433 | break; |
---|
434 | case ROAR_DIR_THRU: |
---|
435 | return streams_get_subsys(ROAR_STREAM(ss)->pos_rel_id); |
---|
436 | break; |
---|
437 | } |
---|
438 | |
---|
439 | return -1; |
---|
440 | } |
---|
441 | |
---|
442 | int streams_get_ssdir (int id) { |
---|
443 | struct roar_stream_server * ss; |
---|
444 | |
---|
445 | _CHECK_SID(id); |
---|
446 | |
---|
447 | if ( (ss = g_streams[id]) == NULL ) |
---|
448 | return -1; |
---|
449 | |
---|
450 | switch (ROAR_STREAM(ss)->dir) { |
---|
451 | case ROAR_DIR_PLAY: |
---|
452 | case ROAR_DIR_MIDI_IN: |
---|
453 | case ROAR_DIR_LIGHT_IN: |
---|
454 | case ROAR_DIR_RAW_IN: |
---|
455 | case ROAR_DIR_COMPLEX_IN: |
---|
456 | return STREAM_DIR_IN; |
---|
457 | break; |
---|
458 | case ROAR_DIR_RECORD: |
---|
459 | case ROAR_DIR_MONITOR: |
---|
460 | case ROAR_DIR_OUTPUT: |
---|
461 | case ROAR_DIR_MIDI_OUT: |
---|
462 | case ROAR_DIR_LIGHT_OUT: |
---|
463 | case ROAR_DIR_RAW_OUT: |
---|
464 | case ROAR_DIR_COMPLEX_OUT: |
---|
465 | return STREAM_DIR_OUT; |
---|
466 | break; |
---|
467 | case ROAR_DIR_MIXING: |
---|
468 | return STREAM_DIR_NONE; |
---|
469 | break; |
---|
470 | case ROAR_DIR_FILTER: |
---|
471 | case ROAR_DIR_BIDIR: |
---|
472 | return STREAM_DIR_BIDIR; |
---|
473 | break; |
---|
474 | case ROAR_DIR_THRU: |
---|
475 | return streams_get_ssdir(ROAR_STREAM(ss)->pos_rel_id); |
---|
476 | break; |
---|
477 | } |
---|
478 | |
---|
479 | return -1; |
---|
480 | } |
---|
481 | |
---|
482 | #define _err() streams_delete(id); return -1; |
---|
483 | int streams_new_virtual (int parent, struct roar_stream_server ** stream) { |
---|
484 | struct roar_stream_server * parent_ss, * ss; |
---|
485 | struct roar_stream * parent_s , * s; |
---|
486 | int id = -1; |
---|
487 | int client, dir; |
---|
488 | |
---|
489 | if ( streams_get(parent, &parent_ss) == -1 ) |
---|
490 | return -1; |
---|
491 | |
---|
492 | if ( (client = streams_get_client(parent)) == -1 ) |
---|
493 | return -1; |
---|
494 | |
---|
495 | if ( (dir = streams_get_dir(parent)) == -1 ) |
---|
496 | return -1; |
---|
497 | |
---|
498 | if ( (id = streams_new()) == -1 ) { |
---|
499 | return -1; |
---|
500 | } |
---|
501 | |
---|
502 | if ( client_stream_add(client, id) == -1 ) { |
---|
503 | _err(); |
---|
504 | } |
---|
505 | |
---|
506 | if ( streams_get(id, &ss) == -1 ) { |
---|
507 | _err(); |
---|
508 | } |
---|
509 | |
---|
510 | if ( streams_set_dir(id, dir, 1) == -1 ) { |
---|
511 | _err(); |
---|
512 | } |
---|
513 | |
---|
514 | s = ROAR_STREAM( ss); |
---|
515 | parent_s = ROAR_STREAM(parent_ss); |
---|
516 | |
---|
517 | s->pos_rel_id = parent; |
---|
518 | |
---|
519 | if ( streams_set_rawflag(id, ROAR_FLAG_VIRTUAL) == -1 ) { |
---|
520 | _err(); |
---|
521 | } |
---|
522 | |
---|
523 | if ( stream != NULL ) |
---|
524 | *stream = ss; |
---|
525 | |
---|
526 | return id; |
---|
527 | } |
---|
528 | #undef _err |
---|
529 | |
---|
530 | int streams_set_fh (int id, int fh) { |
---|
531 | struct roar_stream_server * ss; |
---|
532 | struct roar_stream * s; |
---|
533 | int dir; |
---|
534 | int nonblock = 1; |
---|
535 | |
---|
536 | _CHECK_SID(id); |
---|
537 | |
---|
538 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
539 | return -1; |
---|
540 | |
---|
541 | if ( ss->ready ) { |
---|
542 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = -1 // try to set fh for stream which is already ready", id, fh); |
---|
543 | return -1; |
---|
544 | } |
---|
545 | |
---|
546 | dir = ROAR_STREAM(ss)->dir; |
---|
547 | |
---|
548 | ROAR_DBG("streams_set_fh(id=%i, fh=%i): g_streams[id]->id=%i", id, fh, s->id); |
---|
549 | |
---|
550 | s->fh = fh; |
---|
551 | |
---|
552 | ROAR_DBG("streams_set_fh(id=%i, fh=%i): driverID=%i", id, fh, ss->driver_id); |
---|
553 | |
---|
554 | if ( ss->driver_id == -1 && fh != -2 ) { |
---|
555 | #ifndef ROAR_TARGET_WIN32 |
---|
556 | roar_vio_set_fh(&(ss->vio), fh); |
---|
557 | #else |
---|
558 | roar_vio_open_fh_socket(&(ss->vio), fh); |
---|
559 | #endif |
---|
560 | } |
---|
561 | |
---|
562 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
563 | |
---|
564 | switch (dir) { |
---|
565 | case ROAR_DIR_THRU: |
---|
566 | case ROAR_DIR_RAW_IN: |
---|
567 | case ROAR_DIR_RAW_OUT: |
---|
568 | break; |
---|
569 | default: |
---|
570 | if ( codecfilter_open(&(ss->codecfilter_inst), &(ss->codecfilter), NULL, |
---|
571 | s->info.codec, ss) == -1 ) { |
---|
572 | streams_delete(id); // TODO: FIXME: is this correct? shoudn't we return -1 in any case here? |
---|
573 | return -1; |
---|
574 | } |
---|
575 | break; |
---|
576 | } |
---|
577 | |
---|
578 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
579 | |
---|
580 | if ( fh == -2 ) { |
---|
581 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
582 | if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_READ_FH, &fh) == -1 ) { |
---|
583 | fh = -2; |
---|
584 | } else { |
---|
585 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
586 | if ( fh < 0 ) { |
---|
587 | fh = -2; |
---|
588 | } else { |
---|
589 | s->fh = fh; |
---|
590 | } |
---|
591 | } |
---|
592 | } |
---|
593 | |
---|
594 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
595 | |
---|
596 | if ( fh == -1 || fh == -2 ) { // yes, this is valid, indecats full vio! |
---|
597 | ss->ready = 1; |
---|
598 | ROAR_INFO("streams_set_fh(id=%i, fh=%i): stream state: %s->new", ROAR_DBG_INFO_VERBOSE, id, fh, roar_streamstate2str(ss->state)); |
---|
599 | ss->state = ROAR_STREAMSTATE_NEW; |
---|
600 | return 0; |
---|
601 | } |
---|
602 | |
---|
603 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
604 | |
---|
605 | // roar_socket_recvbuf(fh, ROAR_OUTPUT_CALC_OUTBUFSIZE( &(ROAR_STREAM(g_streams[id])->info) )); // set recv buffer to minimum |
---|
606 | |
---|
607 | switch (dir) { |
---|
608 | case ROAR_DIR_MONITOR: |
---|
609 | case ROAR_DIR_RECORD: |
---|
610 | case ROAR_DIR_OUTPUT: |
---|
611 | case ROAR_DIR_MIDI_OUT: |
---|
612 | case ROAR_DIR_LIGHT_OUT: |
---|
613 | case ROAR_DIR_RAW_OUT: |
---|
614 | ROAR_SHUTDOWN(fh, SHUT_RD); |
---|
615 | break; |
---|
616 | } |
---|
617 | |
---|
618 | if ( dir >= ROAR_DIR_DIRIDS ) |
---|
619 | return -1; |
---|
620 | |
---|
621 | if ( g_config->streams[dir].flags & ROAR_FLAG_SYNC ) { |
---|
622 | switch (dir) { |
---|
623 | case ROAR_DIR_BRIDGE: |
---|
624 | case ROAR_DIR_MIDI_OUT: |
---|
625 | break; |
---|
626 | default: |
---|
627 | nonblock = 0; |
---|
628 | break; |
---|
629 | } |
---|
630 | } |
---|
631 | |
---|
632 | |
---|
633 | if ( !nonblock ) { |
---|
634 | ss->ready = 1; |
---|
635 | ROAR_INFO("streams_set_fh(id=%i, fh=%i): stream state: %s->new", ROAR_DBG_INFO_VERBOSE, id, fh, roar_streamstate2str(ss->state)); |
---|
636 | ss->state = ROAR_STREAMSTATE_NEW; |
---|
637 | |
---|
638 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = 0", id, fh); |
---|
639 | return 0; |
---|
640 | } else { |
---|
641 | #ifndef ROAR_TARGET_WIN32 |
---|
642 | if ( roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK) == -1 ) |
---|
643 | return -1; |
---|
644 | #endif |
---|
645 | |
---|
646 | ss->ready = 1; |
---|
647 | ROAR_INFO("streams_set_fh(id=%i, fh=%i): stream state: %s->new", ROAR_DBG_INFO_VERBOSE, id, fh, roar_streamstate2str(ss->state)); |
---|
648 | ss->state = ROAR_STREAMSTATE_NEW; |
---|
649 | |
---|
650 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = 0", id, fh); |
---|
651 | return 0; |
---|
652 | } |
---|
653 | } |
---|
654 | |
---|
655 | int streams_get_fh (int id) { |
---|
656 | _CHECK_SID(id); |
---|
657 | |
---|
658 | return ROAR_STREAM(g_streams[id])->fh; |
---|
659 | } |
---|
660 | |
---|
661 | int streams_set_null_io(int id) { |
---|
662 | struct roar_stream_server * ss; |
---|
663 | struct roar_stream * s; |
---|
664 | |
---|
665 | _CHECK_SID(id); |
---|
666 | |
---|
667 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
668 | return -1; |
---|
669 | |
---|
670 | s->fh = -1; |
---|
671 | |
---|
672 | return 0; |
---|
673 | } |
---|
674 | |
---|
675 | int streams_get (int id, struct roar_stream_server ** stream) { |
---|
676 | _CHECK_SID(id); |
---|
677 | |
---|
678 | *stream = g_streams[id]; |
---|
679 | |
---|
680 | return 0; |
---|
681 | } |
---|
682 | |
---|
683 | int streams_set_socktype (int id, int socktype) { |
---|
684 | _CHECK_SID(id); |
---|
685 | |
---|
686 | g_streams[id]->socktype = socktype; |
---|
687 | |
---|
688 | return 0; |
---|
689 | } |
---|
690 | |
---|
691 | int streams_get_socktype (int id) { |
---|
692 | _CHECK_SID(id); |
---|
693 | |
---|
694 | return g_streams[id]->socktype; |
---|
695 | } |
---|
696 | |
---|
697 | int streams_is_ready (int id) { |
---|
698 | _CHECK_SID(id); |
---|
699 | |
---|
700 | return g_streams[id]->ready; |
---|
701 | } |
---|
702 | |
---|
703 | int streams_set_primary (int id, int prim) { |
---|
704 | _CHECK_SID(id); |
---|
705 | |
---|
706 | g_streams[id]->primary = prim; |
---|
707 | |
---|
708 | return 0; |
---|
709 | } |
---|
710 | |
---|
711 | int streams_mark_primary (int id) { |
---|
712 | return streams_set_primary(id, 1); |
---|
713 | } |
---|
714 | |
---|
715 | int streams_set_sync (int id, int sync) { |
---|
716 | int fh = streams_get_fh(id); |
---|
717 | |
---|
718 | _CHECK_SID(id); |
---|
719 | |
---|
720 | if ( fh != -1 ) { |
---|
721 | if ( roar_socket_nonblock(fh, sync ? ROAR_SOCKET_BLOCK : ROAR_SOCKET_NONBLOCK) == -1 ) |
---|
722 | return -1; |
---|
723 | |
---|
724 | #ifdef ROAR_FDATASYNC |
---|
725 | ROAR_FDATASYNC(fh); |
---|
726 | #endif |
---|
727 | |
---|
728 | return 0; |
---|
729 | } else { |
---|
730 | return roar_vio_nonblock(&(g_streams[id]->vio), sync); |
---|
731 | } |
---|
732 | } |
---|
733 | |
---|
734 | int streams_set_mmap (int id, int reset) { |
---|
735 | int use = !reset; |
---|
736 | |
---|
737 | _CHECK_SID(id); |
---|
738 | |
---|
739 | return roar_vio_ctl(&(g_streams[id]->vio), ROAR_VIO_CTL_SET_UMMAP, &use); |
---|
740 | } |
---|
741 | |
---|
742 | int streams_set_flag (int id, int flag) { |
---|
743 | int parent; |
---|
744 | |
---|
745 | _CHECK_SID(id); |
---|
746 | |
---|
747 | if ( flag & ROAR_FLAG_IMMUTABLE ) |
---|
748 | flag |= ROAR_FLAG_PRIMARY; |
---|
749 | |
---|
750 | if ( flag & ROAR_FLAG_MMAP ) |
---|
751 | if ( streams_set_mmap(id, 0) == -1 ) |
---|
752 | flag -= ROAR_FLAG_MMAP; |
---|
753 | |
---|
754 | if ( flag & ROAR_FLAG_PRIMARY ) { |
---|
755 | streams_set_primary(id, 1); |
---|
756 | flag -= ROAR_FLAG_PRIMARY; |
---|
757 | } |
---|
758 | |
---|
759 | if ( flag & ROAR_FLAG_VIRTUAL ) { |
---|
760 | if ( (parent = ROAR_STREAM(g_streams[id])->pos_rel_id) == -1 ) |
---|
761 | return -1; |
---|
762 | |
---|
763 | if ( streams_ctl(parent, ROAR_CODECFILTER_CTL_VIRTUAL_NEW|ROAR_STREAM_CTL_TYPE_INT, &id) == -1 ) { |
---|
764 | // flag -= ROAR_FLAG_VIRTUAL; |
---|
765 | return -1; |
---|
766 | } |
---|
767 | |
---|
768 | if ( client_stream_move(streams_get_client(parent), id) == -1 ) { |
---|
769 | return -1; |
---|
770 | } |
---|
771 | } |
---|
772 | |
---|
773 | if ( flag & ROAR_FLAG_SYNC ) { |
---|
774 | switch (ROAR_STREAM(g_streams[id])->dir) { |
---|
775 | // for this stream types the flag is used in the subsystem: |
---|
776 | case ROAR_DIR_BRIDGE: |
---|
777 | case ROAR_DIR_MIDI_OUT: |
---|
778 | break; |
---|
779 | |
---|
780 | // normal behavor (vio blocking): |
---|
781 | default: |
---|
782 | // the fh is updated as soon as the fh get ready in case the default ask to set sync |
---|
783 | if ( !g_streams[id]->ready && !(g_config->streams[ROAR_STREAM(g_streams[id])->dir].flags & ROAR_FLAG_SYNC) ) { |
---|
784 | if ( streams_set_sync(id, 1) == -1 ) |
---|
785 | flag -= ROAR_FLAG_SYNC; |
---|
786 | } |
---|
787 | } |
---|
788 | } |
---|
789 | |
---|
790 | if ( flag & ROAR_FLAG_HWMIXER ) { // currently not supported -> ignored |
---|
791 | g_streams[id]->flags |= flag; |
---|
792 | if ( streams_set_mixer(id) == -1 ) { |
---|
793 | g_streams[id]->flags -= flag; |
---|
794 | return -1; |
---|
795 | } |
---|
796 | } |
---|
797 | |
---|
798 | if ( flag & ROAR_FLAG_RECSOURCE ) { |
---|
799 | if ( streams_recsource_id != -1 ) { |
---|
800 | if ( streams_reset_flag(streams_recsource_id, ROAR_FLAG_RECSOURCE) == -1 ) |
---|
801 | return -1; |
---|
802 | } |
---|
803 | |
---|
804 | streams_recsource_id = id; |
---|
805 | } |
---|
806 | |
---|
807 | g_streams[id]->flags |= flag; |
---|
808 | |
---|
809 | #ifdef ROAR_SUPPORT_META |
---|
810 | if ( flag & ROAR_FLAG_META ) |
---|
811 | stream_meta_finalize(id); |
---|
812 | #endif |
---|
813 | |
---|
814 | return 0; |
---|
815 | } |
---|
816 | |
---|
817 | int streams_set_rawflag (int id, int flag) { |
---|
818 | _CHECK_SID(id); |
---|
819 | |
---|
820 | g_streams[id]->flags |= flag; |
---|
821 | |
---|
822 | return 0; |
---|
823 | } |
---|
824 | |
---|
825 | int streams_reset_flag (int id, int flag) { |
---|
826 | _CHECK_SID(id); |
---|
827 | |
---|
828 | if ( g_streams[id]->flags & ROAR_FLAG_IMMUTABLE ) { |
---|
829 | flag |= ROAR_FLAG_PRIMARY; |
---|
830 | flag -= ROAR_FLAG_PRIMARY; |
---|
831 | } |
---|
832 | |
---|
833 | if ( flag & ROAR_FLAG_RECSOURCE ) |
---|
834 | if ( streams_recsource_id == id ) |
---|
835 | streams_recsource_id = -1; |
---|
836 | |
---|
837 | if ( flag & ROAR_FLAG_MMAP ) |
---|
838 | if ( streams_set_mmap(id, 1) == -1 ) |
---|
839 | flag -= ROAR_FLAG_MMAP; |
---|
840 | |
---|
841 | if ( flag & ROAR_FLAG_PRIMARY ) { |
---|
842 | streams_set_primary(id, 0); |
---|
843 | flag -= ROAR_FLAG_PRIMARY; |
---|
844 | } |
---|
845 | |
---|
846 | if ( flag & ROAR_FLAG_SYNC ) { |
---|
847 | // we refuse to reset the flag on FILTER streams |
---|
848 | if ( streams_get_dir(id) == ROAR_DIR_FILTER ) { |
---|
849 | // flags -= ROAR_FLAG_SYNC; |
---|
850 | return -1; |
---|
851 | } else { |
---|
852 | streams_set_sync(id, 0); |
---|
853 | } |
---|
854 | } |
---|
855 | |
---|
856 | g_streams[id]->flags |= flag; |
---|
857 | g_streams[id]->flags -= flag; |
---|
858 | |
---|
859 | return 0; |
---|
860 | } |
---|
861 | |
---|
862 | int streams_get_flag (int id, int flag) { |
---|
863 | _CHECK_SID(id); |
---|
864 | |
---|
865 | return g_streams[id]->flags & flag ? 1 : 0; |
---|
866 | } |
---|
867 | |
---|
868 | int streams_set_name (int id, char * name) { |
---|
869 | char * str; |
---|
870 | |
---|
871 | _CHECK_SID(id); |
---|
872 | |
---|
873 | if ( (str = strdup(name)) == NULL ) |
---|
874 | return -1; |
---|
875 | |
---|
876 | if ( g_streams[id]->name != NULL ) |
---|
877 | free(g_streams[id]->name); |
---|
878 | |
---|
879 | g_streams[id]->name = str; |
---|
880 | |
---|
881 | return 0; |
---|
882 | } |
---|
883 | |
---|
884 | char * streams_get_name (int id) { |
---|
885 | _CHECK_SID_RET(id, NULL); |
---|
886 | |
---|
887 | return g_streams[id]->name; |
---|
888 | } |
---|
889 | |
---|
890 | |
---|
891 | int streams_calc_delay (int id) { |
---|
892 | struct roar_stream_server * ss; |
---|
893 | struct roar_stream * s; |
---|
894 | register uint_least32_t d = 0; |
---|
895 | uint_least32_t t[1]; |
---|
896 | uint64_t tmp; |
---|
897 | |
---|
898 | _CHECK_SID(id); |
---|
899 | |
---|
900 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
901 | return -1; |
---|
902 | |
---|
903 | // mixer store there value in ss->delay directly |
---|
904 | if ( s->dir == ROAR_DIR_MIXING ) |
---|
905 | return 0; |
---|
906 | |
---|
907 | #if 0 |
---|
908 | // this only confuses the user... |
---|
909 | if ( ss->mixer_stream != id ) { |
---|
910 | if ( streams_calc_delay(ss->mixer_stream) != -1 ) { |
---|
911 | d += g_streams[ss->mixer_stream]->delay; // in case we can calc the delay |
---|
912 | // the stream must exist, so no check here |
---|
913 | } |
---|
914 | } |
---|
915 | #endif |
---|
916 | |
---|
917 | if ( ss->codecfilter != -1 ) { |
---|
918 | if ( codecfilter_delay(ss->codecfilter_inst, ss->codecfilter, t) != -1 ) |
---|
919 | d += *t; |
---|
920 | } |
---|
921 | |
---|
922 | if ( ss->vio.ctl != NULL ) { |
---|
923 | if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_DELAY, t) != -1 ) { // *t is in byte |
---|
924 | ROAR_DBG("streams_calc_delay(id=%i): VIO delay in byte: %i", id, *t); |
---|
925 | tmp = *t; |
---|
926 | tmp *= 1000000; // musec per sec |
---|
927 | tmp /= s->info.rate * s->info.channels * (s->info.bits/8); |
---|
928 | ROAR_DBG("streams_calc_delay(id=%i): VIO delay in musec: %llu", id, tmp); |
---|
929 | |
---|
930 | d += tmp; |
---|
931 | } |
---|
932 | } |
---|
933 | |
---|
934 | ROAR_DBG("streams_calc_delay(id=%i): delay in musec: %i", id, d); |
---|
935 | |
---|
936 | ss->delay = d; |
---|
937 | |
---|
938 | return 0; |
---|
939 | } |
---|
940 | |
---|
941 | int streams_set_mixer (int id) { |
---|
942 | struct roar_stream_server * ss; |
---|
943 | struct roar_stream_server * pmss; |
---|
944 | int i; |
---|
945 | int subsys; |
---|
946 | |
---|
947 | _CHECK_SID(id); |
---|
948 | |
---|
949 | if ( (ss = g_streams[id]) == NULL ) |
---|
950 | return -1; |
---|
951 | |
---|
952 | if ( streams_get_flag(id, ROAR_FLAG_PASSMIXER) == 1 ) { |
---|
953 | if ( (subsys = streams_get_subsys(id)) == -1 ) |
---|
954 | return -1; |
---|
955 | |
---|
956 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
957 | if ( (pmss = g_streams[i]) != NULL ) { |
---|
958 | if ( streams_get_flag(i, ROAR_FLAG_PASSMIXER) == 1 ) { |
---|
959 | if ( streams_get_subsys(i) == subsys ) { |
---|
960 | if ( &(pmss->mixer) != &(ss->mixer) ) { |
---|
961 | memcpy(&(pmss->mixer), &(ss->mixer), sizeof(struct roar_mixer_settings)); |
---|
962 | } |
---|
963 | |
---|
964 | // update hwmixers and the like but do not set mixer value recrusivly. |
---|
965 | streams_reset_flag(i, ROAR_FLAG_PASSMIXER); |
---|
966 | streams_set_mixer(i); |
---|
967 | streams_set_flag(i, ROAR_FLAG_PASSMIXER); |
---|
968 | } |
---|
969 | } |
---|
970 | } |
---|
971 | } |
---|
972 | } |
---|
973 | |
---|
974 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) |
---|
975 | return 0; |
---|
976 | |
---|
977 | if ( ss->driver_id == -1 ) |
---|
978 | return 0; |
---|
979 | |
---|
980 | return driver_set_volume(id, &(ss->mixer)); |
---|
981 | } |
---|
982 | |
---|
983 | int streams_set_map (int id, char * map, size_t len) { |
---|
984 | struct roar_stream_server * ss; |
---|
985 | int ssdir; |
---|
986 | |
---|
987 | _CHECK_SID(id); |
---|
988 | |
---|
989 | if ( (ss = g_streams[id]) == NULL ) |
---|
990 | return -1; |
---|
991 | |
---|
992 | if ( map != NULL ) { |
---|
993 | if ( ROAR_STREAM(ss)->info.channels != len ) |
---|
994 | return -1; |
---|
995 | } |
---|
996 | |
---|
997 | ssdir = streams_get_ssdir(id); |
---|
998 | |
---|
999 | switch (ssdir) { |
---|
1000 | case STREAM_DIR_IN: |
---|
1001 | case STREAM_DIR_NONE: |
---|
1002 | memset(ss->chanmap.in, 0, sizeof(ss->chanmap.in)); |
---|
1003 | |
---|
1004 | if ( map != NULL ) |
---|
1005 | memcpy(ss->chanmap.in, map, len); |
---|
1006 | |
---|
1007 | roardsp_chanmap_calc(&(ss->chanmap), ROARDSP_CHANMAP_MAP, 0); |
---|
1008 | break; |
---|
1009 | case STREAM_DIR_OUT: |
---|
1010 | memset(ss->chanmap.out, 0, sizeof(ss->chanmap.out)); |
---|
1011 | |
---|
1012 | if ( map != NULL ) |
---|
1013 | memcpy(ss->chanmap.out, map, len); |
---|
1014 | |
---|
1015 | roardsp_chanmap_calc(&(ss->chanmap), ROARDSP_CHANMAP_MAP, 0); |
---|
1016 | break; |
---|
1017 | default: |
---|
1018 | return -1; |
---|
1019 | } |
---|
1020 | |
---|
1021 | return 0; |
---|
1022 | } |
---|
1023 | |
---|
1024 | int streams_ltm_ctl (int id, int mt, int window, int cmd) { |
---|
1025 | struct roar_stream_server * ss; |
---|
1026 | struct roar_stream_ltm * ltm; |
---|
1027 | int i; |
---|
1028 | |
---|
1029 | _CHECK_SID(id); |
---|
1030 | |
---|
1031 | // currently we only support the WORKBLOCK window |
---|
1032 | if ( window != ROAR_LTM_WIN_WORKBLOCK ) |
---|
1033 | return -1; |
---|
1034 | |
---|
1035 | // currently we only support ROAR_LTM_MT_RMS |
---|
1036 | if ( (mt | ROAR_LTM_MT_RMS) - ROAR_LTM_MT_RMS ) |
---|
1037 | return -1; |
---|
1038 | |
---|
1039 | if ( (ss = g_streams[id]) == NULL ) |
---|
1040 | return -1; |
---|
1041 | |
---|
1042 | switch (cmd) { |
---|
1043 | case ROAR_LTM_SST_NOOP: |
---|
1044 | return 0; |
---|
1045 | break; |
---|
1046 | case ROAR_LTM_SST_REGISTER: |
---|
1047 | // test if the window is already registered: |
---|
1048 | for (i = 0; i < MAX_LTM_WINDOWS_PER_STREAM; i++) { |
---|
1049 | ltm = &(ss->ltm[i]); |
---|
1050 | if ( ltm->refc && ltm->window == window ) { |
---|
1051 | ltm->refc++; |
---|
1052 | ltm->mt |= mt; |
---|
1053 | return 0; |
---|
1054 | } |
---|
1055 | } |
---|
1056 | |
---|
1057 | // register |
---|
1058 | for (i = 0; i < MAX_LTM_WINDOWS_PER_STREAM; i++) { |
---|
1059 | ltm = &(ss->ltm[i]); |
---|
1060 | if ( ltm->refc == 0 ) { |
---|
1061 | memset(ltm, 0, sizeof(struct roar_stream_ltm)); |
---|
1062 | ltm->refc = 1; |
---|
1063 | ltm->window = window; |
---|
1064 | ltm->parent_window = -1; |
---|
1065 | ltm->mt = mt; |
---|
1066 | ltm->channels = ROAR_STREAM(ss)->info.channels; |
---|
1067 | if ( (ltm->cur = roar_mm_malloc(ltm->channels*sizeof(struct roar_ltm_vals))) == NULL ) { |
---|
1068 | ltm->refc = 0; // reset |
---|
1069 | return -1; |
---|
1070 | } |
---|
1071 | memset(ltm->cur, 0, ltm->channels*sizeof(struct roar_ltm_vals)); |
---|
1072 | return 0; |
---|
1073 | } |
---|
1074 | } |
---|
1075 | return -1; |
---|
1076 | break; |
---|
1077 | case ROAR_LTM_SST_UNREGISTER: |
---|
1078 | for (i = 0; i < MAX_LTM_WINDOWS_PER_STREAM; i++) { |
---|
1079 | ltm = &(ss->ltm[i]); |
---|
1080 | if ( ltm->refc && ltm->window == window ) { |
---|
1081 | ltm->refc--; |
---|
1082 | if ( ! ltm->refc ) { |
---|
1083 | roar_mm_free(ltm->cur); |
---|
1084 | return 0; |
---|
1085 | } |
---|
1086 | } |
---|
1087 | } |
---|
1088 | return -1; |
---|
1089 | break; |
---|
1090 | default: |
---|
1091 | return -1; |
---|
1092 | break; |
---|
1093 | } |
---|
1094 | } |
---|
1095 | |
---|
1096 | int streams_ltm_calc (int id, struct roar_audio_info * info, void * data, size_t len) { |
---|
1097 | struct roar_stream_server * ss; |
---|
1098 | struct roar_stream_ltm * ltm = NULL; |
---|
1099 | int64_t rmsbuf_real[8]; |
---|
1100 | int64_t * rmsbuf = rmsbuf_real; |
---|
1101 | size_t samples = 0; |
---|
1102 | int i; |
---|
1103 | |
---|
1104 | _CHECK_SID(id); |
---|
1105 | |
---|
1106 | if ( data == NULL || info == NULL ) |
---|
1107 | return -1; |
---|
1108 | |
---|
1109 | if ( info->codec != ROAR_CODEC_DEFAULT ) |
---|
1110 | return -1; |
---|
1111 | |
---|
1112 | if ( (ss = g_streams[id]) == NULL ) |
---|
1113 | return -1; |
---|
1114 | |
---|
1115 | for (i = 0; i < MAX_LTM_WINDOWS_PER_STREAM; i++) { |
---|
1116 | if ( ss->ltm[i].refc && ss->ltm[i].window == ROAR_LTM_WIN_WORKBLOCK ) { |
---|
1117 | ltm = &(ss->ltm[i]); |
---|
1118 | break; |
---|
1119 | } |
---|
1120 | } |
---|
1121 | |
---|
1122 | if ( ltm == NULL ) |
---|
1123 | return -1; |
---|
1124 | |
---|
1125 | // TODO: support change of number of channels |
---|
1126 | if ( ltm->channels != info->channels ) |
---|
1127 | return -1; |
---|
1128 | |
---|
1129 | if ( len == 0 ) { |
---|
1130 | memset(ltm->cur, 0, ltm->channels * sizeof(struct roar_ltm_vals)); |
---|
1131 | return 0; |
---|
1132 | } |
---|
1133 | |
---|
1134 | // TODO: support more channels then rmsbuf_real has space for |
---|
1135 | if ( ltm->channels > (sizeof(rmsbuf_real)/sizeof(*rmsbuf_real)) ) { |
---|
1136 | return -1; |
---|
1137 | } |
---|
1138 | |
---|
1139 | samples = len / info->bits; |
---|
1140 | |
---|
1141 | if ( ltm->mt & ROAR_LTM_MT_RMS ) { |
---|
1142 | if ( roar_rms2_1_b_n(data, samples, rmsbuf, ltm->channels, info->bits) == -1 ) |
---|
1143 | return -1; |
---|
1144 | |
---|
1145 | for (i = 0; i < ltm->channels; i++) { |
---|
1146 | ltm->cur[i].rms = rmsbuf[i] << (64 - info->bits*2); |
---|
1147 | } |
---|
1148 | |
---|
1149 | ROAR_DBG("streams_ltm_calc(id=%i,...): rmsbuf[0]=%lli", id, (long long int)rmsbuf[0]); |
---|
1150 | } |
---|
1151 | |
---|
1152 | return 0; |
---|
1153 | } |
---|
1154 | |
---|
1155 | struct roar_stream_ltm * streams_lzm_get(int id, int mt, int window) { |
---|
1156 | struct roar_stream_server * ss; |
---|
1157 | struct roar_stream_ltm * ltm = NULL; |
---|
1158 | int i; |
---|
1159 | |
---|
1160 | _CHECK_SID(id); |
---|
1161 | |
---|
1162 | if ( (ss = g_streams[id]) == NULL ) |
---|
1163 | return -1; |
---|
1164 | |
---|
1165 | for (i = 0; i < MAX_LTM_WINDOWS_PER_STREAM; i++) { |
---|
1166 | if ( ss->ltm[i].refc && ss->ltm[i].window == window ) { |
---|
1167 | ltm = &(ss->ltm[i]); |
---|
1168 | break; |
---|
1169 | } |
---|
1170 | } |
---|
1171 | |
---|
1172 | if ( ltm == NULL ) |
---|
1173 | return NULL; |
---|
1174 | |
---|
1175 | if ( (ltm->mt & mt) != mt ) |
---|
1176 | return NULL; |
---|
1177 | |
---|
1178 | return ltm; |
---|
1179 | } |
---|
1180 | |
---|
1181 | |
---|
1182 | int streams_ctl (int id, int_least32_t cmd, void * data) { |
---|
1183 | struct roar_stream_server * ss; |
---|
1184 | int_least32_t comp; |
---|
1185 | |
---|
1186 | _CHECK_SID(id); |
---|
1187 | |
---|
1188 | if ( (ss = g_streams[id]) == NULL ) |
---|
1189 | return -1; |
---|
1190 | |
---|
1191 | comp = cmd & ROAR_STREAM_CTL_COMPMASK; |
---|
1192 | |
---|
1193 | cmd &= ~comp; |
---|
1194 | |
---|
1195 | ROAR_DBG("streams_ctl(id=%i, cmd=?, data=%p): comp=0x%.8x, cmd=0x%.4x", id, data, comp, cmd); |
---|
1196 | |
---|
1197 | switch (comp) { |
---|
1198 | case ROAR_STREAM_CTL_COMP_BASE: |
---|
1199 | break; |
---|
1200 | case ROAR_STREAM_CTL_COMP_CF: |
---|
1201 | return codecfilter_ctl(ss->codecfilter_inst, ss->codecfilter, cmd, data); |
---|
1202 | break; |
---|
1203 | case ROAR_STREAM_CTL_COMP_DRV: |
---|
1204 | break; |
---|
1205 | default: |
---|
1206 | return -1; |
---|
1207 | } |
---|
1208 | |
---|
1209 | return -1; |
---|
1210 | } |
---|
1211 | |
---|
1212 | int streams_get_outputbuffer (int id, void ** buffer, size_t size) { |
---|
1213 | _CHECK_SID(id); |
---|
1214 | |
---|
1215 | // output buffer size does never change. |
---|
1216 | if ( g_streams[id]->output != NULL ) { |
---|
1217 | *buffer = g_streams[id]->output; |
---|
1218 | return 0; |
---|
1219 | } |
---|
1220 | |
---|
1221 | if ( (g_streams[id]->output = malloc(size)) == NULL ) { |
---|
1222 | ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno)); |
---|
1223 | return -1; |
---|
1224 | } |
---|
1225 | |
---|
1226 | *buffer = g_streams[id]->output; |
---|
1227 | |
---|
1228 | return 0; |
---|
1229 | } |
---|
1230 | |
---|
1231 | int streams_fill_mixbuffer2 (int id, struct roar_audio_info * info) { |
---|
1232 | size_t outlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(info); |
---|
1233 | void * outdata; |
---|
1234 | size_t inlen; |
---|
1235 | size_t inlen_got; |
---|
1236 | void * indata = NULL; |
---|
1237 | size_t buflen; |
---|
1238 | void * bufdata = NULL; |
---|
1239 | struct roar_buffer * bufbuf = NULL; |
---|
1240 | int is_the_same = 0; |
---|
1241 | struct roar_audio_info * stream_info; |
---|
1242 | struct roar_stream * s; |
---|
1243 | struct roar_stream_server * ss; |
---|
1244 | |
---|
1245 | _CHECK_SID(id); |
---|
1246 | |
---|
1247 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
1248 | return -1; |
---|
1249 | |
---|
1250 | // set up stream_info |
---|
1251 | stream_info = &(s->info); |
---|
1252 | |
---|
1253 | // calc todo_in |
---|
1254 | inlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info); |
---|
1255 | |
---|
1256 | buflen = ROAR_OUTPUT_CALC_OUTBUFSIZE_MAX(info, stream_info); |
---|
1257 | |
---|
1258 | ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=%p{...}): inlen=%lu, buflen=%lu", id, info, (unsigned long)inlen, (unsigned long)buflen); |
---|
1259 | |
---|
1260 | if ( inlen == 0 ) { |
---|
1261 | ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=%p{...}): inlen == 0, this should not happen!", id, info); |
---|
1262 | return -1; |
---|
1263 | } |
---|
1264 | |
---|
1265 | if ( streams_get_outputbuffer(id, &outdata, outlen) == -1 ) { |
---|
1266 | return -1; |
---|
1267 | } |
---|
1268 | |
---|
1269 | if ( outdata == NULL ) { |
---|
1270 | return -1; |
---|
1271 | } |
---|
1272 | |
---|
1273 | ROAR_DBG("streams_fill_mixbuffer2(*): outdata=%p, len=%i->%i (in->out)", outdata, inlen, outlen); |
---|
1274 | |
---|
1275 | is_the_same = stream_info->rate == info->rate && stream_info->bits == info->bits && |
---|
1276 | stream_info->channels == info->channels && stream_info->codec == info->codec; |
---|
1277 | |
---|
1278 | ROAR_DBG("streams_fill_mixbuffer2(*): is_the_same=%i", is_the_same); |
---|
1279 | |
---|
1280 | if ( !is_the_same && buflen > outlen ) { |
---|
1281 | /* |
---|
1282 | // this is not supported at the moment |
---|
1283 | memset(outdata, 0, outlen); |
---|
1284 | return -1; |
---|
1285 | */ |
---|
1286 | |
---|
1287 | if ( roar_buffer_new(&bufbuf, buflen) == -1 ) |
---|
1288 | return -1; |
---|
1289 | |
---|
1290 | if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 ) |
---|
1291 | return -1; |
---|
1292 | indata = bufdata; |
---|
1293 | } else { |
---|
1294 | indata = outdata; |
---|
1295 | bufdata = outdata; |
---|
1296 | } |
---|
1297 | |
---|
1298 | inlen_got = inlen; |
---|
1299 | |
---|
1300 | ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=...): inlen_got=%u", id, inlen_got); |
---|
1301 | |
---|
1302 | if ( stream_shift_out_buffer(id, indata, &inlen_got) == -1 ) { |
---|
1303 | if ( ss->is_new ) { |
---|
1304 | ss->pre_underruns++; |
---|
1305 | } else { |
---|
1306 | ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=...): underrun in stream", id); |
---|
1307 | ss->post_underruns++; |
---|
1308 | } |
---|
1309 | memset(outdata, 0, outlen); |
---|
1310 | return 0; |
---|
1311 | } |
---|
1312 | |
---|
1313 | ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=...): inlen_got=%u", id, inlen_got); |
---|
1314 | |
---|
1315 | if ( ss->is_new ) { |
---|
1316 | ss->state = ROAR_STREAMSTATE_OLD; |
---|
1317 | ROAR_INFO("streams_fill_mixbuffer2(id=%i, info=...): stream state: new->old", ROAR_DBG_INFO_VERBOSE, id); |
---|
1318 | } |
---|
1319 | |
---|
1320 | ss->is_new = 0; |
---|
1321 | |
---|
1322 | // check channel map: |
---|
1323 | #if 0 |
---|
1324 | if ( roardsp_chanmap_mappcm(indata, indata, inlen, stream_info->channels, &(ss->chanmap), stream_info->bits) == -1 ) { |
---|
1325 | if ( bufbuf != NULL ) |
---|
1326 | roar_buffer_free(bufbuf); |
---|
1327 | return -1; |
---|
1328 | } |
---|
1329 | #endif |
---|
1330 | |
---|
1331 | // TODO: this only works in case a we have a amp for the given stream parameters. |
---|
1332 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) && !streams_get_flag(id, ROAR_FLAG_PASSMIXER) ) { |
---|
1333 | ROAR_DBG("streams_fill_mixbuffer2(*): CALL roar_amp_pcm(*)..."); |
---|
1334 | if ( roar_amp_pcm(indata, stream_info->bits, indata, 8*inlen / stream_info->bits, stream_info->channels, &(ss->mixer)) == -1 ) |
---|
1335 | return -1; |
---|
1336 | } |
---|
1337 | |
---|
1338 | // check codec, bits, channels, rate... |
---|
1339 | if ( is_the_same ) { |
---|
1340 | if ( indata != outdata ) |
---|
1341 | memcpy(outdata, indata, inlen); |
---|
1342 | |
---|
1343 | if ( inlen < outlen ) |
---|
1344 | memset(outdata+inlen, 0, outlen-inlen); |
---|
1345 | } else { |
---|
1346 | // if ( roar_conv(outdata, indata, (8*inlen_got*info->rate)/(stream_info->rate * stream_info->bits), stream_info, info) == -1 ) { |
---|
1347 | ROAR_DBG("streams_fill_mixbuffer2(*): CALL roar_conv2(*)..."); |
---|
1348 | if ( roar_conv2(bufdata, indata, inlen, stream_info, info, outlen) == -1 ) { |
---|
1349 | if ( bufbuf != NULL ) |
---|
1350 | roar_buffer_free(bufbuf); |
---|
1351 | return -1; |
---|
1352 | } |
---|
1353 | |
---|
1354 | // memset(outdata, 0, outlen); |
---|
1355 | } |
---|
1356 | |
---|
1357 | if ( bufbuf != NULL ) { |
---|
1358 | memcpy(outdata, bufdata, outlen); |
---|
1359 | roar_buffer_free(bufbuf); |
---|
1360 | } |
---|
1361 | |
---|
1362 | streams_ltm_calc(id, stream_info, outdata, outlen); |
---|
1363 | |
---|
1364 | if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) ) { |
---|
1365 | ROAR_DBG("streams_fill_mixbuffer2(*): Calcing antiecho..."); |
---|
1366 | // we can ignore errors here: |
---|
1367 | if ( stream_outputbuffer_request(id, &bufbuf, buflen) == 0 ) { |
---|
1368 | if ( roar_buffer_get_data(bufbuf, &bufdata) != -1 ) |
---|
1369 | memcpy(bufdata, outdata, outlen); |
---|
1370 | } |
---|
1371 | } |
---|
1372 | |
---|
1373 | ROAR_DBG("streams_fill_mixbuffer2(*): inlen_got=%llu, stream_info={.bits=%i, .rate=%i,...}", (long long unsigned int)inlen_got, stream_info->bits, stream_info->rate); |
---|
1374 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, (inlen_got*8)/stream_info->bits); |
---|
1375 | |
---|
1376 | ROAR_DBG("streams_fill_mixbuffer2(*) = 0"); |
---|
1377 | return 0; |
---|
1378 | } |
---|
1379 | |
---|
1380 | |
---|
1381 | int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) { |
---|
1382 | static void * bufs[ROAR_STREAMS_MAX+1]; |
---|
1383 | int i; |
---|
1384 | int have = 0; |
---|
1385 | int dir; |
---|
1386 | |
---|
1387 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
1388 | if ( g_streams[i] != NULL ) { |
---|
1389 | dir = streams_get_dir(i); |
---|
1390 | |
---|
1391 | switch (dir) { |
---|
1392 | case ROAR_DIR_PLAY: |
---|
1393 | case ROAR_DIR_BIDIR: |
---|
1394 | break; |
---|
1395 | case ROAR_DIR_BRIDGE: |
---|
1396 | if ( g_streams[i]->buffer == NULL ) |
---|
1397 | continue; |
---|
1398 | break; |
---|
1399 | default: |
---|
1400 | continue; |
---|
1401 | } |
---|
1402 | |
---|
1403 | if ( streams_get_flag(i, ROAR_FLAG_PAUSE) ) |
---|
1404 | continue; |
---|
1405 | |
---|
1406 | if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) { |
---|
1407 | ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i); |
---|
1408 | ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round."); |
---|
1409 | continue; |
---|
1410 | } |
---|
1411 | if ( streams_fill_mixbuffer2(i, info) == -1 ) { |
---|
1412 | ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i); |
---|
1413 | continue; |
---|
1414 | } |
---|
1415 | |
---|
1416 | // printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
1417 | |
---|
1418 | ROAR_DBG("streams_get_mixbuffers(*): bufs[have] = %p", bufs[have]); |
---|
1419 | ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]); |
---|
1420 | |
---|
1421 | if ( !streams_get_flag(i, ROAR_FLAG_MUTE) ) |
---|
1422 | have++; // we have a new stream! |
---|
1423 | } |
---|
1424 | } |
---|
1425 | |
---|
1426 | bufs[have] = NULL; |
---|
1427 | //printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
1428 | |
---|
1429 | ROAR_DBG("streams_get_mixbuffers(*): have = %i", have); |
---|
1430 | |
---|
1431 | *bufferlist = bufs; |
---|
1432 | return have; |
---|
1433 | } |
---|
1434 | |
---|
1435 | |
---|
1436 | int stream_add_buffer (int id, struct roar_buffer * buf) { |
---|
1437 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
1438 | |
---|
1439 | _CHECK_SID(id); |
---|
1440 | |
---|
1441 | if ( g_streams[id]->buffer == NULL ) { |
---|
1442 | g_streams[id]->buffer = buf; |
---|
1443 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf); |
---|
1444 | return 0; |
---|
1445 | } |
---|
1446 | |
---|
1447 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
1448 | return roar_buffer_add(g_streams[id]->buffer, buf); |
---|
1449 | } |
---|
1450 | |
---|
1451 | int stream_shift_out_buffer (int id, void * data, size_t * len) { |
---|
1452 | _CHECK_SID(id); |
---|
1453 | |
---|
1454 | if ( g_streams[id]->buffer == NULL ) |
---|
1455 | return -1; |
---|
1456 | |
---|
1457 | return roar_buffer_shift_out(&(g_streams[id]->buffer), data, len); |
---|
1458 | } |
---|
1459 | |
---|
1460 | int stream_shift_buffer (int id, struct roar_buffer ** buf) { |
---|
1461 | struct roar_buffer * next; |
---|
1462 | |
---|
1463 | _CHECK_SID(id); |
---|
1464 | |
---|
1465 | if ( g_streams[id]->buffer == NULL ) { |
---|
1466 | *buf = NULL; |
---|
1467 | return 0; |
---|
1468 | } |
---|
1469 | |
---|
1470 | roar_buffer_get_next(g_streams[id]->buffer, &next); |
---|
1471 | |
---|
1472 | *buf = g_streams[id]->buffer; |
---|
1473 | g_streams[id]->buffer = next; |
---|
1474 | |
---|
1475 | return 0; |
---|
1476 | } |
---|
1477 | int stream_unshift_buffer (int id, struct roar_buffer * buf) { |
---|
1478 | _CHECK_SID(id); |
---|
1479 | |
---|
1480 | if ( g_streams[id]->buffer == NULL ) { |
---|
1481 | g_streams[id]->buffer = buf; |
---|
1482 | return 0; |
---|
1483 | } |
---|
1484 | |
---|
1485 | buf->next = NULL; |
---|
1486 | |
---|
1487 | roar_buffer_add(buf, g_streams[id]->buffer); |
---|
1488 | |
---|
1489 | g_streams[id]->buffer = buf; |
---|
1490 | |
---|
1491 | return 0; |
---|
1492 | } |
---|
1493 | |
---|
1494 | int stream_outputbuffer_request(int id, struct roar_buffer ** buf, size_t len) { |
---|
1495 | register struct roar_stream_server * ss; |
---|
1496 | size_t buflen; |
---|
1497 | void * bufdata; |
---|
1498 | int ret; |
---|
1499 | |
---|
1500 | _CHECK_SID(id); |
---|
1501 | |
---|
1502 | if ( (ss = g_streams[id]) == NULL ) |
---|
1503 | return -1; |
---|
1504 | |
---|
1505 | if ( buf != NULL ) /* just be be sure */ |
---|
1506 | *buf = NULL; |
---|
1507 | |
---|
1508 | if ( ss->outputbuffer != NULL ) { |
---|
1509 | if ( roar_buffer_get_len(ss->outputbuffer, &buflen) == 0 ) { |
---|
1510 | if ( buflen == len ) { |
---|
1511 | if ( buf != NULL ) |
---|
1512 | *buf = ss->outputbuffer; |
---|
1513 | return 0; |
---|
1514 | } else if ( buflen > len ) { |
---|
1515 | if ( roar_buffer_set_len(ss->outputbuffer, len) == 0 ) { |
---|
1516 | if ( buf != NULL ) |
---|
1517 | *buf = ss->outputbuffer; |
---|
1518 | return 0; |
---|
1519 | } |
---|
1520 | } |
---|
1521 | } |
---|
1522 | |
---|
1523 | // if the buffer is not suitable: |
---|
1524 | |
---|
1525 | ret = roar_buffer_free(ss->outputbuffer); |
---|
1526 | ss->outputbuffer = NULL; |
---|
1527 | if ( ret == -1 ) |
---|
1528 | return ret; |
---|
1529 | } |
---|
1530 | |
---|
1531 | if ( roar_buffer_new(&(ss->outputbuffer), len) == -1 ) |
---|
1532 | return -1; |
---|
1533 | |
---|
1534 | if ( roar_buffer_get_data(ss->outputbuffer, &bufdata) == -1 ) { |
---|
1535 | roar_buffer_free(ss->outputbuffer); |
---|
1536 | ss->outputbuffer = NULL; |
---|
1537 | return -1; |
---|
1538 | } |
---|
1539 | |
---|
1540 | memset(bufdata, 0, len); |
---|
1541 | |
---|
1542 | if ( buf != NULL ) |
---|
1543 | *buf = ss->outputbuffer; |
---|
1544 | |
---|
1545 | return 0; |
---|
1546 | } |
---|
1547 | |
---|
1548 | int stream_outputbuffer_destroy(int id) { |
---|
1549 | int ret; |
---|
1550 | register struct roar_stream_server * ss; |
---|
1551 | |
---|
1552 | _CHECK_SID(id); |
---|
1553 | |
---|
1554 | if ( (ss = g_streams[id]) == NULL ) |
---|
1555 | return -1; |
---|
1556 | |
---|
1557 | if ( ss->outputbuffer != NULL ) { |
---|
1558 | ret = roar_buffer_free(ss->outputbuffer); |
---|
1559 | ss->outputbuffer = NULL; |
---|
1560 | return ret; |
---|
1561 | } |
---|
1562 | |
---|
1563 | return 0; |
---|
1564 | } |
---|
1565 | |
---|
1566 | int stream_prethru_add(int id, struct roar_buffer * buf) { |
---|
1567 | register struct roar_stream_server * ss; |
---|
1568 | |
---|
1569 | _CHECK_SID(id); |
---|
1570 | |
---|
1571 | if ( (ss = g_streams[id]) == NULL ) |
---|
1572 | return -1; |
---|
1573 | |
---|
1574 | if ( ss->prethru == NULL ) { |
---|
1575 | ss->prethru = buf; |
---|
1576 | return 0; |
---|
1577 | } |
---|
1578 | |
---|
1579 | if ( roar_buffer_add(ss->prethru, buf) == -1 ) { |
---|
1580 | return -1; |
---|
1581 | } |
---|
1582 | |
---|
1583 | return 0; |
---|
1584 | } |
---|
1585 | |
---|
1586 | int stream_prethru_add_data(int id, void ** buf, size_t len) { |
---|
1587 | struct roar_buffer * buffer; |
---|
1588 | |
---|
1589 | _CHECK_SID(id); |
---|
1590 | |
---|
1591 | if ( roar_buffer_new(&buffer, len) == -1 ) |
---|
1592 | return -1; |
---|
1593 | |
---|
1594 | if ( roar_buffer_get_data(buffer, buf) == -1 ) { |
---|
1595 | roar_buffer_free(buffer); |
---|
1596 | return -1; |
---|
1597 | } |
---|
1598 | |
---|
1599 | if ( stream_prethru_add(id, buffer) == -1 ) { |
---|
1600 | roar_buffer_free(buffer); |
---|
1601 | return -1; |
---|
1602 | } |
---|
1603 | |
---|
1604 | return 0; |
---|
1605 | } |
---|
1606 | |
---|
1607 | int stream_prethru_destroy(int id) { |
---|
1608 | int ret; |
---|
1609 | register struct roar_stream_server * ss; |
---|
1610 | |
---|
1611 | _CHECK_SID(id); |
---|
1612 | |
---|
1613 | if ( (ss = g_streams[id]) == NULL ) |
---|
1614 | return -1; |
---|
1615 | |
---|
1616 | if ( ss->prethru != NULL ) { |
---|
1617 | ret = roar_buffer_free(ss->prethru); |
---|
1618 | ss->prethru = NULL; |
---|
1619 | return ret; |
---|
1620 | } |
---|
1621 | |
---|
1622 | return 0; |
---|
1623 | } |
---|
1624 | |
---|
1625 | int stream_prethru_send(int dst, int src) { |
---|
1626 | struct roar_stream_server * dst_ss, * src_ss; |
---|
1627 | struct roar_buffer * bufbuf; |
---|
1628 | void * bufdata; |
---|
1629 | size_t buflen; |
---|
1630 | |
---|
1631 | ROAR_DBG("stream_prethru_send(dst=%i, src=%i) = ?", dst, src); |
---|
1632 | |
---|
1633 | _CHECK_SID(dst); |
---|
1634 | _CHECK_SID(src); |
---|
1635 | |
---|
1636 | if ( (dst_ss = g_streams[dst]) == NULL ) |
---|
1637 | return -1; |
---|
1638 | |
---|
1639 | if ( (src_ss = g_streams[src]) == NULL ) |
---|
1640 | return -1; |
---|
1641 | |
---|
1642 | bufbuf = src_ss->prethru; |
---|
1643 | |
---|
1644 | ROAR_DBG("stream_prethru_send(dst=%i, src=%i): prethru buffer at %p", dst, src, bufbuf); |
---|
1645 | |
---|
1646 | while (bufbuf != NULL) { |
---|
1647 | ROAR_DBG("stream_prethru_send(dst=%i, src=%i): looping with buffer at %p", dst, src, bufbuf); |
---|
1648 | |
---|
1649 | if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 ) |
---|
1650 | return -1; |
---|
1651 | |
---|
1652 | if ( roar_buffer_get_len(bufbuf, &buflen) == -1 ) |
---|
1653 | return -1; |
---|
1654 | |
---|
1655 | if ( stream_vio_s_write(dst_ss, bufdata, buflen) != buflen ) |
---|
1656 | return -1; |
---|
1657 | |
---|
1658 | if ( roar_buffer_get_next(bufbuf, &bufbuf) == -1 ) |
---|
1659 | return -1; |
---|
1660 | } |
---|
1661 | |
---|
1662 | ROAR_DBG("stream_prethru_send(dst=%i, src=%i) = 0", dst, src); |
---|
1663 | return 0; |
---|
1664 | } |
---|
1665 | |
---|
1666 | int streams_check (int id) { |
---|
1667 | int fh; |
---|
1668 | ssize_t req, realreq, done; |
---|
1669 | struct roar_stream * s; |
---|
1670 | struct roar_stream_server * ss; |
---|
1671 | struct roar_buffer * b; |
---|
1672 | char * buf; |
---|
1673 | // char tmp; |
---|
1674 | |
---|
1675 | _CHECK_SID(id); |
---|
1676 | |
---|
1677 | ROAR_DBG("streams_check(id=%i) = ?", id); |
---|
1678 | |
---|
1679 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
1680 | |
---|
1681 | if ( (fh = s->fh) == -1 ) |
---|
1682 | return 0; |
---|
1683 | |
---|
1684 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
1685 | return 0; |
---|
1686 | |
---|
1687 | switch (s->dir) { |
---|
1688 | case ROAR_DIR_LIGHT_IN: |
---|
1689 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
1690 | return light_check_stream(id); |
---|
1691 | #else |
---|
1692 | streams_delete(id); |
---|
1693 | return -1; |
---|
1694 | #endif |
---|
1695 | break; |
---|
1696 | case ROAR_DIR_MIDI_IN: |
---|
1697 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
1698 | return midi_check_stream(id); |
---|
1699 | #else |
---|
1700 | streams_delete(id); |
---|
1701 | return -1; |
---|
1702 | #endif |
---|
1703 | break; |
---|
1704 | case ROAR_DIR_RAW_IN: |
---|
1705 | #ifndef ROAR_WITHOUT_DCOMP_RAW |
---|
1706 | return raw_check_stream(id); |
---|
1707 | #else |
---|
1708 | streams_delete(id); |
---|
1709 | return -1; |
---|
1710 | #endif |
---|
1711 | break; |
---|
1712 | case ROAR_DIR_RDTCS_IN: |
---|
1713 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
1714 | return rdtcs_check_stream(id); |
---|
1715 | #else |
---|
1716 | streams_delete(id); |
---|
1717 | return -1; |
---|
1718 | #endif |
---|
1719 | break; |
---|
1720 | case ROAR_DIR_PLAY: |
---|
1721 | case ROAR_DIR_BIDIR: |
---|
1722 | break; |
---|
1723 | default: |
---|
1724 | /* |
---|
1725 | ROAR_WARN("streams_check(id=%i): Read event on non input stream of type/dir %s", id, roar_dir2str(s->dir)); |
---|
1726 | errno = 0; |
---|
1727 | req = stream_vio_s_read(ss, &tmp, 1); |
---|
1728 | ROAR_DBG("streams_check(id=%i): stream_vio_s_read(ss, &tmp, 1) = %li // errno=%s(%i)", id, req, strerror(errno), errno); |
---|
1729 | if ( req == 1 ) { |
---|
1730 | ROAR_ERR("streams_check(id=%i): Client violates protocol, got one byte of data on output stream, kicking stream"); |
---|
1731 | streams_delete(id); |
---|
1732 | return -1; |
---|
1733 | } |
---|
1734 | */ |
---|
1735 | return 0; |
---|
1736 | break; |
---|
1737 | } |
---|
1738 | |
---|
1739 | ROAR_DBG("streams_check(id=%i): fh = %i", id, fh); |
---|
1740 | |
---|
1741 | /* |
---|
1742 | ROAR_DBG("streams_check(id=%i): ROAR_OUTPUT_BUFFER_SAMPLES=%i, s->info.channels=%i, s->info.bits=%i, s->info.rat=%i, g_sa->rate=%i", id, ROAR_OUTPUT_BUFFER_SAMPLES, s->info.channels, s->info.bits, s->info.rate, g_sa->rate); |
---|
1743 | */ |
---|
1744 | |
---|
1745 | req = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // optimal size |
---|
1746 | // req = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * (s->info.bits / 8) * ((float)s->info.rate/g_sa->rate); |
---|
1747 | req += ss->need_extra; // bytes left we sould get.... |
---|
1748 | |
---|
1749 | ROAR_DBG("streams_check(id=%i): asking for %i bytes", id, req); |
---|
1750 | |
---|
1751 | if ( roar_buffer_new(&b, req) == -1 ) { |
---|
1752 | ROAR_ERR("streams_check(*): Can not alloc buffer space!"); |
---|
1753 | ROAR_DBG("streams_check(*) = -1"); |
---|
1754 | return -1; |
---|
1755 | } |
---|
1756 | |
---|
1757 | roar_buffer_get_data(b, (void **)&buf); |
---|
1758 | |
---|
1759 | ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id); |
---|
1760 | ROAR_DBG("streams_check(id=%i): asking for %i bytes", id, req); |
---|
1761 | |
---|
1762 | if ( ss->codecfilter == -1 ) { |
---|
1763 | realreq = req; |
---|
1764 | /* |
---|
1765 | req = read(fh, buf, req); |
---|
1766 | if ( req < realreq ) { // we can do this as the stream is in nonblocking mode! |
---|
1767 | if ( (realreq = read(fh, buf+req, realreq-req)) > 0 ) |
---|
1768 | req += realreq; |
---|
1769 | } |
---|
1770 | */ |
---|
1771 | done = 0; |
---|
1772 | while (req > 0 && done != realreq) { |
---|
1773 | if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 ) |
---|
1774 | done += req; |
---|
1775 | } |
---|
1776 | req = done; |
---|
1777 | |
---|
1778 | roar_buffer_get_data(b, (void **)&buf); |
---|
1779 | } else { |
---|
1780 | req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req); |
---|
1781 | } |
---|
1782 | |
---|
1783 | if ( req > 0 ) { |
---|
1784 | ROAR_DBG("streams_check(id=%i): got %i bytes", id, req); |
---|
1785 | |
---|
1786 | roar_buffer_set_len(b, req); |
---|
1787 | |
---|
1788 | if ( stream_add_buffer(id, b) != -1 ) |
---|
1789 | return 0; |
---|
1790 | |
---|
1791 | ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id); |
---|
1792 | roar_buffer_free(b); |
---|
1793 | } else { |
---|
1794 | ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno)); |
---|
1795 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
1796 | if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :) |
---|
1797 | #else |
---|
1798 | if ( errno != EAGAIN ) { |
---|
1799 | #endif |
---|
1800 | ROAR_DBG("streams_check(id=%i): EOF!", id); |
---|
1801 | streams_delete(id); |
---|
1802 | ROAR_DBG("streams_check(id=%i) = 0", id); |
---|
1803 | } |
---|
1804 | roar_buffer_free(b); |
---|
1805 | return 0; |
---|
1806 | } |
---|
1807 | |
---|
1808 | |
---|
1809 | ROAR_DBG("streams_check(id=%i) = -1", id); |
---|
1810 | return -1; |
---|
1811 | } |
---|
1812 | |
---|
1813 | |
---|
1814 | #define _return(x) return (x) |
---|
1815 | int streams_send_mon (int id) { |
---|
1816 | // int fh; |
---|
1817 | struct roar_stream * s; |
---|
1818 | struct roar_stream_server * ss; |
---|
1819 | struct roar_buffer * bufbuf = NULL; |
---|
1820 | struct roar_remove_state removalstate; |
---|
1821 | void * ip; |
---|
1822 | void * obuf; |
---|
1823 | int olen; |
---|
1824 | int is_the_same = 1; |
---|
1825 | int is_vol_eq = 1; |
---|
1826 | int antiecho = 0; |
---|
1827 | ssize_t ret; |
---|
1828 | |
---|
1829 | _CHECK_SID(id); |
---|
1830 | |
---|
1831 | ROAR_DBG("streams_send_mon(id=%i) = ?", id); |
---|
1832 | |
---|
1833 | s = ROAR_STREAM((ss = g_streams[id])); |
---|
1834 | |
---|
1835 | /* |
---|
1836 | if ( (fh = s->fh) == -1 ) |
---|
1837 | return 0; |
---|
1838 | */ |
---|
1839 | |
---|
1840 | if ( !ss->ready ) |
---|
1841 | return 0; |
---|
1842 | |
---|
1843 | if ( g_config->jumbo_mtu ) |
---|
1844 | roar_vio_sync(ss->viop); |
---|
1845 | |
---|
1846 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
1847 | return 0; |
---|
1848 | |
---|
1849 | switch (s->dir) { |
---|
1850 | case ROAR_DIR_LIGHT_OUT: |
---|
1851 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
1852 | return light_send_stream(id); |
---|
1853 | #else |
---|
1854 | streams_delete(id); |
---|
1855 | return -1; |
---|
1856 | #endif |
---|
1857 | break; |
---|
1858 | case ROAR_DIR_MIDI_OUT: |
---|
1859 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
1860 | return midi_send_stream(id); |
---|
1861 | #else |
---|
1862 | streams_delete(id); |
---|
1863 | return -1; |
---|
1864 | #endif |
---|
1865 | break; |
---|
1866 | case ROAR_DIR_RDTCS_OUT: |
---|
1867 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
1868 | return rdtcs_send_stream(id); |
---|
1869 | #else |
---|
1870 | streams_delete(id); |
---|
1871 | return -1; |
---|
1872 | #endif |
---|
1873 | break; |
---|
1874 | |
---|
1875 | case ROAR_DIR_COMPLEX_OUT: |
---|
1876 | // send a tick: |
---|
1877 | if ( ss->codecfilter != -1 ) { |
---|
1878 | if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, NULL, 0) == 0 ) |
---|
1879 | if ( ss->state != ROAR_STREAMSTATE_OLD ) { |
---|
1880 | ROAR_INFO("streams_send_mon(id=%i): stream state: %s->old", ROAR_DBG_INFO_VERBOSE, id, roar_streamstate2str(ss->state)); |
---|
1881 | } |
---|
1882 | ss->state = ROAR_STREAMSTATE_OLD; |
---|
1883 | } |
---|
1884 | return 0; |
---|
1885 | break; |
---|
1886 | |
---|
1887 | case ROAR_DIR_OUTPUT: |
---|
1888 | if ( g_standby ) |
---|
1889 | return 0; |
---|
1890 | case ROAR_DIR_MONITOR: |
---|
1891 | case ROAR_DIR_BIDIR: |
---|
1892 | break; |
---|
1893 | |
---|
1894 | default: |
---|
1895 | return 0; |
---|
1896 | break; |
---|
1897 | } |
---|
1898 | |
---|
1899 | |
---|
1900 | ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, s->fh); |
---|
1901 | |
---|
1902 | if ( s->info.channels != g_sa->channels || s->info.bits != g_sa->bits || |
---|
1903 | s->info.rate != g_sa->rate || s->info.codec != g_sa->codec ) |
---|
1904 | is_the_same = 0; |
---|
1905 | |
---|
1906 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) { |
---|
1907 | is_vol_eq = need_vol_change(g_sa->channels, &(ss->mixer)) ? 0 : 1; |
---|
1908 | } |
---|
1909 | |
---|
1910 | if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) ) |
---|
1911 | antiecho = 1; |
---|
1912 | |
---|
1913 | if ( !is_the_same || !is_vol_eq || antiecho ) { |
---|
1914 | olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len |
---|
1915 | // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here |
---|
1916 | if ( stream_outputbuffer_request(id, &bufbuf, ROAR_OUTPUT_CALC_OUTBUFSIZE_MAX(&(s->info), g_sa)) == -1 ) |
---|
1917 | return -1; |
---|
1918 | |
---|
1919 | if ( roar_buffer_get_data(bufbuf, &obuf) == -1 ) { |
---|
1920 | _return(-1); |
---|
1921 | } |
---|
1922 | |
---|
1923 | ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen); |
---|
1924 | } else { |
---|
1925 | obuf = g_output_buffer; |
---|
1926 | olen = g_output_buffer_len; |
---|
1927 | } |
---|
1928 | |
---|
1929 | ip = g_output_buffer; |
---|
1930 | |
---|
1931 | if ( antiecho ) { |
---|
1932 | ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho); |
---|
1933 | if ( roar_remove_init(&removalstate) == -1 ) { |
---|
1934 | _return(-1); |
---|
1935 | } |
---|
1936 | |
---|
1937 | ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho); |
---|
1938 | if ( roar_remove_so(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->bits, &removalstate) == -1 ) { |
---|
1939 | ROAR_DBG("streams_send_mon(id=%i): anti echo failed", id); |
---|
1940 | _return(-1); |
---|
1941 | } |
---|
1942 | ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho); |
---|
1943 | } |
---|
1944 | |
---|
1945 | if ( !is_vol_eq ) { |
---|
1946 | if ( roar_amp_pcm(obuf, g_sa->bits, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->channels, &(ss->mixer)) == -1 ) { |
---|
1947 | _return(-1); |
---|
1948 | } |
---|
1949 | |
---|
1950 | ip = obuf; |
---|
1951 | } |
---|
1952 | |
---|
1953 | if ( !is_the_same ) { |
---|
1954 | if ( roar_conv(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) { |
---|
1955 | _return(-1); |
---|
1956 | } |
---|
1957 | } |
---|
1958 | |
---|
1959 | errno = 0; |
---|
1960 | |
---|
1961 | if ( ss->codecfilter == -1 ) { |
---|
1962 | ROAR_DBG("streams_send_mon(id=%i): not a CF stream", id); |
---|
1963 | if ( s->fh == -1 && roar_vio_get_fh(&(ss->vio)) == -1 ) { |
---|
1964 | ROAR_DBG("streams_send_mon(id=%i) = 0", id); |
---|
1965 | _return(0); |
---|
1966 | } |
---|
1967 | |
---|
1968 | ROAR_DBG("streams_send_mon(id=%i) = ?", id); |
---|
1969 | |
---|
1970 | if ( (ret = stream_vio_s_write(ss, obuf, olen)) == olen ) { |
---|
1971 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
1972 | if ( ss->state != ROAR_STREAMSTATE_OLD ) { |
---|
1973 | ROAR_INFO("streams_send_mon(id=%i): stream state: %s->old", ROAR_DBG_INFO_VERBOSE, id, roar_streamstate2str(ss->state)); |
---|
1974 | } |
---|
1975 | ss->state = ROAR_STREAMSTATE_OLD; |
---|
1976 | ROAR_DBG("streams_send_mon(id=%i) = 0", id); |
---|
1977 | _return(0); |
---|
1978 | } |
---|
1979 | |
---|
1980 | ROAR_DBG("streams_send_mon(id=%i) = ?", id); |
---|
1981 | |
---|
1982 | if ( ret > 0 && errno == 0 ) { |
---|
1983 | 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); |
---|
1984 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), ret)*s->info.channels); |
---|
1985 | if ( ss->state != ROAR_STREAMSTATE_OLD ) { |
---|
1986 | ROAR_INFO("streams_send_mon(id=%i): stream state: %s->old", ROAR_DBG_INFO_VERBOSE, id, roar_streamstate2str(ss->state)); |
---|
1987 | } |
---|
1988 | ss->state = ROAR_STREAMSTATE_OLD; |
---|
1989 | _return(0); |
---|
1990 | } |
---|
1991 | } else { |
---|
1992 | errno = 0; |
---|
1993 | if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen) |
---|
1994 | == olen ) { |
---|
1995 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
1996 | if ( ss->state != ROAR_STREAMSTATE_OLD ) { |
---|
1997 | ROAR_INFO("streams_send_mon(id=%i): stream state: %s->old", ROAR_DBG_INFO_VERBOSE, id, roar_streamstate2str(ss->state)); |
---|
1998 | } |
---|
1999 | ss->state = ROAR_STREAMSTATE_OLD; |
---|
2000 | _return(0); |
---|
2001 | } else { // we cann't retry on codec filetered streams |
---|
2002 | if ( errno != EAGAIN ) { |
---|
2003 | streams_delete(id); |
---|
2004 | _return(-1); |
---|
2005 | } |
---|
2006 | } |
---|
2007 | } |
---|
2008 | |
---|
2009 | if ( errno == EAGAIN ) { |
---|
2010 | // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to |
---|
2011 | // make any gapes in any output because of this |
---|
2012 | |
---|
2013 | #ifdef ROAR_HAVE_USLEEP |
---|
2014 | usleep(100); // 0.1ms |
---|
2015 | #endif |
---|
2016 | |
---|
2017 | if ( stream_vio_s_write(ss, obuf, olen) == olen ) { |
---|
2018 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
2019 | if ( ss->state != ROAR_STREAMSTATE_OLD ) { |
---|
2020 | ROAR_INFO("streams_send_mon(id=%i): stream state: %s->old", ROAR_DBG_INFO_VERBOSE, id, roar_streamstate2str(ss->state)); |
---|
2021 | } |
---|
2022 | ss->state = ROAR_STREAMSTATE_OLD; |
---|
2023 | _return(0); |
---|
2024 | } else if ( errno == EAGAIN ) { |
---|
2025 | ROAR_WARN("streams_send_mon(id=%i): Can not send data to client: %s", id, strerror(errno)); |
---|
2026 | _return(0); |
---|
2027 | } |
---|
2028 | } |
---|
2029 | |
---|
2030 | // ug... error... delete stream! |
---|
2031 | |
---|
2032 | streams_delete(id); |
---|
2033 | |
---|
2034 | _return(-1); |
---|
2035 | } |
---|
2036 | #undef _return |
---|
2037 | |
---|
2038 | int streams_send_filter(int id) { |
---|
2039 | int fh; |
---|
2040 | int have = 0; |
---|
2041 | int len; |
---|
2042 | struct roar_stream * s; |
---|
2043 | struct roar_stream_server * ss; |
---|
2044 | |
---|
2045 | _CHECK_SID(id); |
---|
2046 | |
---|
2047 | ROAR_DBG("streams_send_filter(id=%i) = ?", id); |
---|
2048 | |
---|
2049 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
2050 | |
---|
2051 | if ( (fh = s->fh) == -1 ) |
---|
2052 | return 0; |
---|
2053 | |
---|
2054 | if ( s->dir != ROAR_DIR_FILTER ) |
---|
2055 | return 0; |
---|
2056 | |
---|
2057 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
2058 | return 0; |
---|
2059 | |
---|
2060 | |
---|
2061 | ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh); |
---|
2062 | |
---|
2063 | if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) { |
---|
2064 | while ( have < g_output_buffer_len ) { |
---|
2065 | if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) { |
---|
2066 | streams_delete(id); |
---|
2067 | return -1; |
---|
2068 | } |
---|
2069 | have += len; |
---|
2070 | } |
---|
2071 | return 0; |
---|
2072 | } |
---|
2073 | |
---|
2074 | // ug... error... delete stream! |
---|
2075 | |
---|
2076 | streams_delete(id); |
---|
2077 | |
---|
2078 | return -1; |
---|
2079 | } |
---|
2080 | |
---|
2081 | |
---|
2082 | // VIO: |
---|
2083 | |
---|
2084 | ssize_t stream_vio_read (int stream, void *buf, size_t count) { |
---|
2085 | _CHECK_SID(stream); |
---|
2086 | |
---|
2087 | return stream_vio_s_read(g_streams[stream], buf, count); |
---|
2088 | } |
---|
2089 | |
---|
2090 | ssize_t stream_vio_write(int stream, void *buf, size_t count) { |
---|
2091 | _CHECK_SID(stream); |
---|
2092 | |
---|
2093 | return stream_vio_s_write(g_streams[stream], buf, count); |
---|
2094 | } |
---|
2095 | |
---|
2096 | |
---|
2097 | ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) { |
---|
2098 | void * orig_buf = buf; |
---|
2099 | size_t len = 0; |
---|
2100 | ssize_t r = -1; |
---|
2101 | int i; |
---|
2102 | |
---|
2103 | errno = 0; |
---|
2104 | |
---|
2105 | if ( !stream ) |
---|
2106 | return -1; |
---|
2107 | |
---|
2108 | //roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
2109 | |
---|
2110 | if ( ! stream->vio.read ) |
---|
2111 | return -1; |
---|
2112 | |
---|
2113 | while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) { |
---|
2114 | len += r; |
---|
2115 | buf += r; |
---|
2116 | count -= r; |
---|
2117 | if ( count == 0 ) |
---|
2118 | break; |
---|
2119 | } |
---|
2120 | |
---|
2121 | if ( len == 0 && r == -1 ) |
---|
2122 | return -1; |
---|
2123 | |
---|
2124 | if ( streams_thru_num ) { |
---|
2125 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
2126 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == ROAR_STREAM(stream)->id ) { |
---|
2127 | if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) { |
---|
2128 | if ( g_streams[i]->ready ) { |
---|
2129 | if ( stream_vio_write(i, orig_buf, len) != len ) |
---|
2130 | streams_delete(i); |
---|
2131 | |
---|
2132 | if ( g_streams[i] != NULL ) { |
---|
2133 | if ( g_streams[i]->state != ROAR_STREAMSTATE_OLD ) { |
---|
2134 | ROAR_INFO("stream_vio_s_read(*): (stream: %i) stream state: %s->old", ROAR_DBG_INFO_VERBOSE, i, roar_streamstate2str(g_streams[i]->state)); |
---|
2135 | } |
---|
2136 | g_streams[i]->state = ROAR_STREAMSTATE_OLD; |
---|
2137 | } |
---|
2138 | } |
---|
2139 | } |
---|
2140 | } |
---|
2141 | } |
---|
2142 | } |
---|
2143 | |
---|
2144 | return len; |
---|
2145 | } |
---|
2146 | |
---|
2147 | ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) { |
---|
2148 | int i; |
---|
2149 | |
---|
2150 | errno = 0; |
---|
2151 | |
---|
2152 | if ( !stream ) |
---|
2153 | return -1; |
---|
2154 | |
---|
2155 | /* |
---|
2156 | if ( roar_vio_get_fh(&(stream->vio)) == -1 && ROAR_STREAM(stream)->fh != -1 ) |
---|
2157 | roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
2158 | */ |
---|
2159 | |
---|
2160 | // ROAR_WARN("stream_vio_s_write(*): writing..."); |
---|
2161 | |
---|
2162 | if ( streams_thru_num ) { |
---|
2163 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
2164 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == ROAR_STREAM(stream)->id ) { |
---|
2165 | if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) { |
---|
2166 | if ( g_streams[i]->ready ) { |
---|
2167 | if ( g_streams[i]->state == ROAR_STREAMSTATE_NEW ) { |
---|
2168 | if ( streams_get_flag(i, ROAR_FLAG_PRETHRU) == 1 ) { |
---|
2169 | if ( stream_prethru_send(i, ROAR_STREAM(stream)->id) == -1 ) { |
---|
2170 | streams_delete(i); |
---|
2171 | } |
---|
2172 | } |
---|
2173 | } |
---|
2174 | |
---|
2175 | if ( stream_vio_write(i, buf, count) != count ) { |
---|
2176 | streams_delete(i); |
---|
2177 | } |
---|
2178 | |
---|
2179 | if ( g_streams[i] != NULL ) { |
---|
2180 | if ( g_streams[i]->state != ROAR_STREAMSTATE_OLD ) { |
---|
2181 | ROAR_INFO("stream_vio_s_write(*): (stream: %i) stream state: %s->old", ROAR_DBG_INFO_VERBOSE, i, roar_streamstate2str(g_streams[i]->state)); |
---|
2182 | } |
---|
2183 | g_streams[i]->state = ROAR_STREAMSTATE_OLD; |
---|
2184 | } |
---|
2185 | } |
---|
2186 | } |
---|
2187 | } |
---|
2188 | } |
---|
2189 | } |
---|
2190 | |
---|
2191 | if ( g_config->jumbo_mtu ) { |
---|
2192 | if ( stream->viop != &(stream->jumbo) ) { |
---|
2193 | if ( roar_vio_open_jumbo(&(stream->jumbo), &(stream->vio), g_config->jumbo_mtu) != -1 ) { |
---|
2194 | // if that works we continue using the jumbo vio, |
---|
2195 | // in case it didn't we dont, just use normal VIO. |
---|
2196 | stream->viop = &(stream->jumbo); |
---|
2197 | } |
---|
2198 | } |
---|
2199 | } |
---|
2200 | |
---|
2201 | return roar_vio_write(stream->viop, buf, count); |
---|
2202 | } |
---|
2203 | |
---|
2204 | //ll |
---|