source: roaraudio/roard/streams.c @ 3539:1fd530767d7b

Last change on this file since 3539:1fd530767d7b was 3539:1fd530767d7b, checked in by phi, 14 years ago

support to read channel map from server

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