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