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