source: roaraudio/roard/streams.c @ 3543:af952b03e304

Last change on this file since 3543:af952b03e304 was 3542:4a10d68e4c70, checked in by phi, 14 years ago

support to set channel map

File size: 44.4 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);
[3542]320     roardsp_chanmap_calc(&(ss->chanmap), ROARDSP_CHANMAP_MAP, 0); // MAP and INVMAP are the same when in==out
[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
[3542]417int streams_get_ssdir  (int id) {
418 struct roar_stream_server * ss;
419
420 _CHECK_SID(id);
421
422 if ( (ss = g_streams[id]) == NULL )
423  return -1;
424
425 switch (ROAR_STREAM(ss)->dir) {
426  case ROAR_DIR_PLAY:
427  case ROAR_DIR_MIDI_IN:
428  case ROAR_DIR_LIGHT_IN:
429  case ROAR_DIR_RAW_IN:
430  case ROAR_DIR_COMPLEX_IN:
431    return STREAM_DIR_IN;
432   break;
433  case ROAR_DIR_RECORD:
434  case ROAR_DIR_MONITOR:
435  case ROAR_DIR_OUTPUT:
436  case ROAR_DIR_MIDI_OUT:
437  case ROAR_DIR_LIGHT_OUT:
438  case ROAR_DIR_RAW_OUT:
439  case ROAR_DIR_COMPLEX_OUT:
440    return STREAM_DIR_OUT;
441   break;
442  case ROAR_DIR_MIXING:
443    return STREAM_DIR_NONE;
444   break;
445  case ROAR_DIR_FILTER:
446  case ROAR_DIR_BIDIR:
447    return STREAM_DIR_BIDIR;
448   break;
449  case ROAR_DIR_THRU:
450    return streams_get_ssdir(ROAR_STREAM(ss)->pos_rel_id);
451   break;
452 }
453
454 return -1;
455}
456
[2593]457#define _err() streams_delete(id); return -1;
[2594]458int streams_new_virtual (int parent, struct roar_stream_server ** stream) {
[2593]459 struct roar_stream_server * parent_ss, * ss;
460 struct roar_stream        * parent_s , *  s;
461 int id = -1;
462 int client, dir;
463
464 if ( streams_get(parent, &parent_ss) == -1 )
465  return -1;
466
467 if ( (client = streams_get_client(parent)) == -1 )
468  return -1;
469
470 if ( (dir = streams_get_dir(parent)) == -1 )
471  return -1;
472
473 if ( (id = streams_new()) == -1 ) {
474  return -1;
475 }
476
[2597]477 if ( client_stream_add(client, id) == -1 ) {
[2593]478  _err();
479 }
480
481 if ( streams_get(id, &ss) == -1 ) {
482  _err();
483 }
484
485 if ( streams_set_dir(id, dir, 1) == -1 ) {
486  _err();
487 }
488
489 s        = ROAR_STREAM(       ss);
490 parent_s = ROAR_STREAM(parent_ss);
491
[2596]492 s->pos_rel_id = parent;
[2593]493
[2625]494 if ( streams_set_rawflag(id, ROAR_FLAG_VIRTUAL) == -1 ) {
[2593]495  _err();
496 }
497
[2596]498 if ( stream != NULL )
[2594]499  *stream = ss;
500
[2593]501 return id;
502}
503#undef _err
504
[0]505int streams_set_fh     (int id, int fh) {
[1243]506 struct roar_stream_server * ss;
[1913]507 struct roar_stream        * s;
[51]508 int dir;
[2361]509 int nonblock = 1;
[51]510
[2734]511 _CHECK_SID(id);
512
[1913]513 if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL )
[0]514  return -1;
515
[2816]516 dir = ROAR_STREAM(ss)->dir;
517
[1913]518 ROAR_DBG("streams_set_fh(id=%i): g_streams[id]->id=%i", id, s->id);
[491]519
[1913]520 s->fh = fh;
[0]521
[1243]522 ROAR_DBG("streams_set_fh(id=%i, fh=%i): driverID=%i", id, fh, ss->driver_id);
523
[2766]524 if ( ss->driver_id == -1 && fh != -2 ) {
525#ifndef ROAR_TARGET_WIN32
[1243]526  roar_vio_set_fh(&(ss->vio), fh);
[2766]527#else
528  roar_vio_open_fh_socket(&(ss->vio), fh);
529#endif
530 }
[1243]531
[2816]532 ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh);
533
[2840]534 switch (dir) {
535  case ROAR_DIR_THRU:
536  case ROAR_DIR_RAW_IN:
537  case ROAR_DIR_RAW_OUT:
538   break;
539  default:
540    if ( codecfilter_open(&(ss->codecfilter_inst), &(ss->codecfilter), NULL,
541                     s->info.codec, ss) == -1 ) {
542     streams_delete(id); // TODO: FIXME: is this correct? shoudn't we return -1 in any case here?
543     return -1;
544    }
545   break;
[571]546 }
[268]547
[2816]548 ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh);
549
[1610]550 if ( fh == -2 ) {
551  ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh);
552  if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_READ_FH, &fh) == -1 ) {
553   fh = -2;
554  } else {
555   ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh);
556   if ( fh < 0 ) {
557    fh = -2;
558   } else {
[1913]559    s->fh = fh;
[1610]560   }
561  }
562 }
563
[2816]564 ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh);
565
[1610]566 if ( fh == -1 || fh == -2 ) { // yes, this is valid, indecats full vio!
[1913]567  ss->ready = 1;
[2613]568  ss->state = ROAR_STREAMSTATE_NEW;
[634]569  return 0;
570 }
571
[2816]572 ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh);
[968]573
[2816]574// roar_socket_recvbuf(fh, ROAR_OUTPUT_CALC_OUTBUFSIZE( &(ROAR_STREAM(g_streams[id])->info) )); // set recv buffer to minimum
[51]575
[1912]576 switch (dir) {
577  case ROAR_DIR_MONITOR:
578  case ROAR_DIR_RECORD:
579  case ROAR_DIR_OUTPUT:
580  case ROAR_DIR_MIDI_OUT:
581  case ROAR_DIR_LIGHT_OUT:
[2248]582  case ROAR_DIR_RAW_OUT:
[1912]583    ROAR_SHUTDOWN(fh, SHUT_RD);
584   break;
[51]585 }
586
[2337]587 if ( dir >= ROAR_DIR_DIRIDS )
588  return -1;
589
590 if ( g_config->streams[dir].flags & ROAR_FLAG_SYNC ) {
[2361]591  switch (dir) {
592   case ROAR_DIR_BRIDGE:
593   case ROAR_DIR_MIDI_OUT:
594    break;
595   default:
596     nonblock = 0;
597    break;
598  }
599 }
600
601
602 if ( !nonblock ) {
[1913]603  ss->ready = 1;
[2613]604  ss->state = ROAR_STREAMSTATE_NEW;
[2816]605
606  ROAR_DBG("streams_set_fh(id=%i, fh=%i) = 0", id, fh);
[0]607  return 0;
608 } else {
[2764]609#ifndef ROAR_TARGET_WIN32
[1913]610  if ( roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK) == -1 )
611   return -1;
[2764]612#endif
[1913]613
614  ss->ready = 1;
[2613]615  ss->state = ROAR_STREAMSTATE_NEW;
[2816]616
617  ROAR_DBG("streams_set_fh(id=%i, fh=%i) = 0", id, fh);
[1913]618  return 0;
[0]619 }
620}
621
[66]622int streams_get_fh     (int id) {
[2734]623 _CHECK_SID(id);
[66]624
[609]625 return ROAR_STREAM(g_streams[id])->fh;
[66]626}
[0]627
[2606]628int streams_set_null_io(int id) {
629 struct roar_stream_server * ss;
630 struct roar_stream        * s;
631
[2734]632 _CHECK_SID(id);
633
[2606]634 if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL )
635  return -1;
636
637 s->fh = -1;
638
639 return 0;
640}
641
[0]642int streams_get    (int id, struct roar_stream_server ** stream) {
[2734]643 _CHECK_SID(id);
[0]644
645 *stream = g_streams[id];
646
647 return 0;
648}
649
[377]650int streams_set_socktype (int id, int socktype) {
[2734]651 _CHECK_SID(id);
[377]652
653 g_streams[id]->socktype = socktype;
654
655 return 0;
656}
657
658int streams_get_socktype (int id) {
[2734]659 _CHECK_SID(id);
[377]660
661 return g_streams[id]->socktype;
662}
[0]663
[643]664int streams_set_primary (int id, int prim) {
[2734]665 _CHECK_SID(id);
[643]666
667 g_streams[id]->primary = prim;
668
669 return 0;
670}
671
672int streams_mark_primary (int id) {
673 return streams_set_primary(id, 1);
674}
[1029]675
[1116]676int streams_set_sync     (int id, int sync) {
[1117]677 int fh = streams_get_fh(id);
[1116]678
[2734]679 _CHECK_SID(id);
680
[1117]681 if ( fh != -1 ) {
682  if ( roar_socket_nonblock(fh, sync ? ROAR_SOCKET_BLOCK : ROAR_SOCKET_NONBLOCK) == -1 )
683   return -1;
[1116]684
[1498]685#ifdef ROAR_FDATASYNC
[1171]686  ROAR_FDATASYNC(fh);
[1498]687#endif
[1125]688
689  return 0;
[1117]690 } else {
691  return roar_vio_nonblock(&(g_streams[id]->vio), sync);
692 }
[1116]693}
694
[1928]695int streams_set_mmap (int id, int reset) {
696 int use = !reset;
697
[2734]698 _CHECK_SID(id);
[1928]699
700 return roar_vio_ctl(&(g_streams[id]->vio), ROAR_VIO_CTL_SET_UMMAP, &use);
701}
702
[1029]703int streams_set_flag     (int id, int flag) {
[2626]704 int parent;
705
[2734]706 _CHECK_SID(id);
[1029]707
[2952]708 if ( flag & ROAR_FLAG_IMMUTABLE )
709  flag |= ROAR_FLAG_PRIMARY;
710
[1926]711 if ( flag & ROAR_FLAG_MMAP )
[1928]712  if ( streams_set_mmap(id, 0) == -1 )
713   flag -= ROAR_FLAG_MMAP;
[1926]714
[1043]715 if ( flag & ROAR_FLAG_PRIMARY ) {
716  streams_set_primary(id, 1);
717  flag -= ROAR_FLAG_PRIMARY;
718 }
719
[2626]720 if ( flag & ROAR_FLAG_VIRTUAL ) {
721  if ( (parent = ROAR_STREAM(g_streams[id])->pos_rel_id) == -1 )
722   return -1;
723
724  if ( streams_ctl(parent, ROAR_CODECFILTER_CTL_VIRTUAL_NEW|ROAR_STREAM_CTL_TYPE_INT, &id) == -1 ) {
725//   flag -= ROAR_FLAG_VIRTUAL;
726   return -1;
727  }
[2705]728
729  if ( client_stream_move(streams_get_client(parent), id) == -1 ) {
730   return -1;
731  }
[2626]732 }
733
[1116]734 if ( flag & ROAR_FLAG_SYNC ) {
[1908]735  switch (ROAR_STREAM(g_streams[id])->dir) {
736   // for this stream types the flag is used in the subsystem:
737   case ROAR_DIR_BRIDGE:
738   case ROAR_DIR_MIDI_OUT:
739    break;
740
741   // normal behavor (vio blocking):
742   default:
[2341]743     // the fh is updated as soon as the fh get ready in case the default ask to set sync
[2342]744     if ( !g_streams[id]->ready && !(g_config->streams[ROAR_STREAM(g_streams[id])->dir].flags & ROAR_FLAG_SYNC) ) {
[2341]745      if ( streams_set_sync(id, 1) == -1 )
746       flag -= ROAR_FLAG_SYNC;
747     }
[1908]748  }
[1116]749 }
750
[1585]751 if ( flag & ROAR_FLAG_HWMIXER ) { // currently not supported -> ignored
[1590]752  g_streams[id]->flags |= flag;
753  if ( streams_set_mixer(id) == -1 ) {
754   g_streams[id]->flags -= flag;
755   return -1;
756  }
[1585]757 }
758
[2417]759 if ( flag & ROAR_FLAG_RECSOURCE ) {
760  if ( streams_recsource_id != -1 ) {
761   if ( streams_reset_flag(streams_recsource_id, ROAR_FLAG_RECSOURCE) == -1 )
762    return -1;
763  }
764
765  streams_recsource_id = id;
766 }
767
[1029]768 g_streams[id]->flags |= flag;
769
[1498]770#ifdef ROAR_SUPPORT_META
[1043]771 if ( flag & ROAR_FLAG_META )
772  stream_meta_finalize(id);
[1498]773#endif
[1043]774
[1029]775 return 0;
776}
777
[2625]778int streams_set_rawflag  (int id, int flag) {
[2734]779 _CHECK_SID(id);
[2625]780
781 g_streams[id]->flags |= flag;
782
783 return 0;
784}
785
[1029]786int streams_reset_flag   (int id, int flag) {
[2734]787 _CHECK_SID(id);
[1029]788
[2952]789 if ( g_streams[id]->flags & ROAR_FLAG_IMMUTABLE ) {
790  flag |= ROAR_FLAG_PRIMARY;
791  flag -= ROAR_FLAG_PRIMARY;
792 }
793
[2417]794 if ( flag & ROAR_FLAG_RECSOURCE )
795  if ( streams_recsource_id == id )
796   streams_recsource_id = -1;
797
[1928]798 if ( flag & ROAR_FLAG_MMAP )
799  if ( streams_set_mmap(id, 1) == -1 )
800   flag -= ROAR_FLAG_MMAP;
801
[1043]802 if ( flag & ROAR_FLAG_PRIMARY ) {
803  streams_set_primary(id, 0);
804  flag -= ROAR_FLAG_PRIMARY;
805 }
806
[1116]807 if ( flag & ROAR_FLAG_SYNC ) {
[2264]808  // we refuse to reset the flag on FILTER streams
809  if ( streams_get_dir(id) == ROAR_DIR_FILTER ) {
810//   flags -= ROAR_FLAG_SYNC;
811   return -1;
812  } else {
813   streams_set_sync(id, 0);
814  }
[1116]815 }
816
[1029]817 g_streams[id]->flags |= flag;
818 g_streams[id]->flags -= flag;
819
820 return 0;
821}
822
[1042]823int streams_get_flag     (int id, int flag) {
[2734]824 _CHECK_SID(id);
[1042]825
826 return g_streams[id]->flags & flag ? 1 : 0;
827}
828
[1842]829int streams_set_name     (int id, char * name) {
830 char * str;
831
[2734]832 _CHECK_SID(id);
[1842]833
834 if ( (str = strdup(name)) == NULL )
835  return -1;
836
837 if ( g_streams[id]->name != NULL )
838  free(g_streams[id]->name);
839
840 g_streams[id]->name = str;
[1845]841
842 return 0;
[1842]843}
844
845char * streams_get_name  (int id) {
[2734]846 _CHECK_SID_RET(id, NULL);
[1842]847
848 return g_streams[id]->name;
849}
850
851
[1223]852int streams_calc_delay    (int id) {
[1142]853 struct roar_stream_server * ss;
[1151]854 struct roar_stream        * s;
[1142]855 register uint_least32_t d = 0;
856 uint_least32_t t[1];
[1151]857 uint64_t       tmp;
[1142]858
[2734]859 _CHECK_SID(id);
860
[1151]861 if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL )
[1142]862  return -1;
863
[3212]864 // mixer store there value in ss->delay directly
865 if ( s->dir == ROAR_DIR_MIXING )
866  return 0;
867
[3216]868 if ( ss->mixer_stream != id ) {
869  if ( streams_calc_delay(ss->mixer_stream) != -1 ) {
870   d += g_streams[ss->mixer_stream]->delay; // in case we can calc the delay
871                                            // the stream must exist, so no check here
872  }
873 }
874
[1142]875 if ( ss->codecfilter != -1 ) {
876  if ( codecfilter_delay(ss->codecfilter_inst, ss->codecfilter, t) != -1 )
877   d += *t;
878 }
879
[1151]880 if ( ss->vio.ctl != NULL ) {
881  if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_DELAY, t) != -1 ) { // *t is in byte
[1223]882   ROAR_DBG("streams_calc_delay(id=%i): VIO delay in byte: %i", id, *t);
[1151]883   tmp = *t;
884   tmp *= 1000000; // musec per sec
885   tmp /= s->info.rate * s->info.channels * (s->info.bits/8);
[2387]886   ROAR_DBG("streams_calc_delay(id=%i): VIO delay in musec: %llu", id, tmp);
[1151]887
888   d += tmp;
889  }
890 }
891
[1223]892 ROAR_DBG("streams_calc_delay(id=%i): delay in musec: %i", id, d);
[1151]893
[1142]894 ss->delay = d;
895
896 return 0;
897}
898
[1590]899int streams_set_mixer    (int id) {
900 struct roar_stream_server * ss;
[2416]901 struct roar_stream_server * pmss;
902 int i;
903 int subsys;
[1590]904
[2734]905 _CHECK_SID(id);
906
[1590]907 if ( (ss = g_streams[id]) == NULL )
908  return -1;
909
[2416]910 if ( streams_get_flag(id, ROAR_FLAG_PASSMIXER) == 1 ) {
911  if ( (subsys = streams_get_subsys(id)) == -1 )
912   return -1;
913
914  for (i = 0; i < ROAR_STREAMS_MAX; i++) {
915   if ( (pmss = g_streams[i]) != NULL ) {
916    if ( streams_get_flag(i, ROAR_FLAG_PASSMIXER) == 1 ) {
917     if ( streams_get_subsys(i) == subsys ) {
918      memcpy(&(pmss->mixer), &(ss->mixer), sizeof(struct roar_mixer_settings));
919
920      // update hwmixers and the like but do not set mixer value recrusivly.
921      streams_reset_flag(i, ROAR_FLAG_PASSMIXER);
922      streams_set_mixer(i);
923      streams_set_flag(i, ROAR_FLAG_PASSMIXER);
924     }
925    }
926   }
927  }
928 }
929
[1590]930 if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) )
931  return 0;
932
933 if ( ss->driver_id == -1 )
934  return 0;
935
936 return driver_set_volume(id, &(ss->mixer));
937}
938
[3542]939int streams_set_map      (int id, char * map, size_t len) {
940 struct roar_stream_server * ss;
941 int ssdir;
942
943 _CHECK_SID(id);
944
945 if ( (ss = g_streams[id]) == NULL )
946  return -1;
947
948 ssdir = streams_get_ssdir(id);
949 if ( ssdir != STREAM_DIR_IN && ssdir != STREAM_DIR_OUT )
950  return -1;
951
952 if ( map != NULL ) {
953  if ( ROAR_STREAM(ss)->info.channels != len )
954   return -1;
955
956  memcpy(ss->chanmap.in, map, len);
957 }
958
959 switch (ssdir) {
960  case STREAM_DIR_IN:
961    roardsp_chanmap_calc(&(ss->chanmap), ROARDSP_CHANMAP_MAP, 0);
962   break;
963  case STREAM_DIR_OUT:
964    roardsp_chanmap_calc(&(ss->chanmap), ROARDSP_CHANMAP_INVMAP, 0);
965   break;
966 }
967
968 return 0;
969}
970
[1224]971int streams_ctl          (int id, int_least32_t cmd, void * data) {
972 struct roar_stream_server * ss;
973 int_least32_t comp;
974
[2734]975 _CHECK_SID(id);
976
[1224]977 if ( (ss = g_streams[id]) == NULL )
978  return -1;
979
980 comp = cmd & ROAR_STREAM_CTL_COMPMASK;
981
982 cmd &= ~comp;
983
[1239]984 ROAR_DBG("streams_ctl(id=%i, cmd=?, data=%p): comp=0x%.8x, cmd=0x%.4x", id, data, comp, cmd);
[1238]985
[1224]986 switch (comp) {
987  case ROAR_STREAM_CTL_COMP_BASE:
988   break;
989  case ROAR_STREAM_CTL_COMP_CF:
990    return codecfilter_ctl(ss->codecfilter_inst, ss->codecfilter, cmd, data);
991   break;
992  case ROAR_STREAM_CTL_COMP_DRV:
993   break;
994  default:
995   return -1;
996 }
997
998 return -1;
999}
1000
[0]1001int streams_get_outputbuffer  (int id, void ** buffer, size_t size) {
[2734]1002 _CHECK_SID(id);
[0]1003
1004 // output buffer size does never change.
1005 if ( g_streams[id]->output != NULL ) {
1006  *buffer = g_streams[id]->output;
1007  return 0;
1008 }
1009
1010 if ( (g_streams[id]->output = malloc(size)) == NULL ) {
1011  ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno));
1012  return -1;
1013 }
1014
1015 *buffer = g_streams[id]->output;
1016
1017 return 0;
1018}
1019
[2061]1020int streams_fill_mixbuffer2 (int id, struct roar_audio_info * info) {
1021 size_t   outlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(info);
1022 void   * outdata;
1023 size_t   inlen;
1024 size_t   inlen_got;
1025 void   * indata = NULL;
[2091]1026 size_t   buflen;
1027 void   * bufdata = NULL;
1028 struct roar_buffer * bufbuf = NULL;
[2061]1029 int      is_the_same = 0;
1030 struct roar_audio_info    * stream_info;
1031 struct roar_stream        * s;
1032 struct roar_stream_server * ss;
1033
[2734]1034 _CHECK_SID(id);
1035
[2061]1036 if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL )
1037  return -1;
1038
1039 // set up stream_info
1040 stream_info = &(s->info);
1041
1042 // calc todo_in
1043 inlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info);
1044
[2091]1045 buflen = ROAR_OUTPUT_CALC_OUTBUFSIZE_MAX(info, stream_info);
1046
[2747]1047 ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=%p{...}): inlen=%lu, buflen=%lu", id, info, (unsigned long)inlen, (unsigned long)buflen);
1048
[2061]1049 if ( inlen == 0 ) {
1050  ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=%p{...}): inlen == 0, this should not happen!", id, info);
1051  return -1;
1052 }
1053
1054 if ( streams_get_outputbuffer(id, &outdata, outlen) == -1 ) {
1055  return -1;
1056 }
1057
1058 if ( outdata == NULL ) {
1059  return -1;
1060 }
1061
1062 ROAR_DBG("streams_fill_mixbuffer2(*): outdata=%p, len=%i->%i (in->out)", outdata, inlen, outlen);
1063
1064 is_the_same = stream_info->rate     == info->rate     && stream_info->bits  == info->bits &&
1065               stream_info->channels == info->channels && stream_info->codec == info->codec;
1066
1067 ROAR_DBG("streams_fill_mixbuffer2(*): is_the_same=%i", is_the_same);
1068
[2091]1069 if ( !is_the_same && buflen > outlen ) {
1070/*
[2061]1071  // this is not supported at the moment
1072  memset(outdata, 0, outlen);
1073  return -1;
[2091]1074*/
1075
1076  if ( roar_buffer_new(&bufbuf, buflen) == -1 )
1077   return -1;
1078
1079  if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 )
1080   return -1;
[2102]1081  indata  = bufdata;
[2061]1082 } else {
[2091]1083  indata  = outdata;
1084  bufdata = outdata;
[2061]1085 }
1086
1087 inlen_got = inlen;
1088
[2102]1089 ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=...): inlen_got=%u", id, inlen_got);
1090
[2061]1091 if ( stream_shift_out_buffer(id, indata, &inlen_got) == -1 ) {
1092  if ( ss->is_new ) {
1093   ss->pre_underruns++;
1094  } else {
[2088]1095   ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=...): underrun in stream", id);
[2061]1096   ss->post_underruns++;
1097  }
1098  memset(outdata, 0, outlen);
1099  return 0;
1100 }
1101
[2387]1102 ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=...): inlen_got=%u", id, inlen_got);
1103
[2061]1104 if ( ss->is_new ) {
[2613]1105  ss->state = ROAR_STREAMSTATE_OLD;
[2971]1106  ROAR_INFO("streams_fill_mixbuffer2(id=%i, info=...): stream state: new->old", ROAR_DBG_INFO_VERBOSE, id);
[2061]1107 }
1108
1109 ss->is_new = 0;
1110
1111 if ( is_the_same ) {
1112  if ( indata != outdata )
1113   memcpy(outdata, indata, inlen);
1114
1115  if ( inlen < outlen )
1116   memset(outdata+inlen, 0, outlen-inlen);
1117 } else {
[2095]1118//  if ( roar_conv(outdata, indata, (8*inlen_got*info->rate)/(stream_info->rate * stream_info->bits), stream_info, info) == -1 ) {
[2387]1119  ROAR_DBG("streams_fill_mixbuffer2(*): CALL roar_conv2(*)...");
[2095]1120  if ( roar_conv2(bufdata, indata, inlen, stream_info, info, buflen) == -1 ) {
[2091]1121   if ( bufbuf != NULL )
1122    roar_buffer_free(bufbuf);
[2062]1123   return -1;
1124  }
1125
1126//  memset(outdata, 0, outlen);
[2061]1127 }
1128
[2091]1129 if ( bufbuf != NULL ) {
1130  memcpy(outdata, bufdata, outlen);
1131  roar_buffer_free(bufbuf);
1132 }
1133
[2414]1134 if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) && !streams_get_flag(id, ROAR_FLAG_PASSMIXER) ) {
[2938]1135  ROAR_DBG("streams_fill_mixbuffer2(*): CALL roar_amp_pcm(*)...");
1136  if ( roar_amp_pcm(outdata, info->bits, outdata, 8*outlen / info->bits, info->channels, &(ss->mixer)) == -1 )
[2079]1137   return -1;
1138 }
1139
[2153]1140 if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) ) {
[2747]1141  ROAR_DBG("streams_fill_mixbuffer2(*): Calcing antiecho...");
[2153]1142  // we can ignore errors here:
[2389]1143  if ( stream_outputbuffer_request(id, &bufbuf, buflen) == 0 ) {
[2153]1144   if ( roar_buffer_get_data(bufbuf, &bufdata) != -1 )
1145    memcpy(bufdata, outdata, outlen);
1146  }
1147 }
1148
[2623]1149 s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(info, outlen)*info->channels);
[2153]1150
[2747]1151 ROAR_DBG("streams_fill_mixbuffer2(*) = 0");
[2062]1152 return 0;
[2061]1153}
1154
[491]1155
[0]1156int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) {
[84]1157 static void * bufs[ROAR_STREAMS_MAX+1];
[0]1158 int i;
1159 int have = 0;
[2458]1160 int dir;
[0]1161
1162 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
1163  if ( g_streams[i] != NULL ) {
[2458]1164   dir = streams_get_dir(i);
1165
1166   switch (dir) {
1167    case ROAR_DIR_PLAY:
1168    case ROAR_DIR_BIDIR:
1169     break;
[2459]1170    case ROAR_DIR_BRIDGE:
1171      if ( g_streams[i]->buffer == NULL )
1172       continue;
1173     break;
[2458]1174    default:
1175      continue;
1176   }
[0]1177
[2730]1178   if ( streams_get_flag(i, ROAR_FLAG_PAUSE) )
1179    continue;
1180
[0]1181   if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) {
1182    ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i);
1183    ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round.");
1184    continue;
1185   }
[2061]1186   if ( streams_fill_mixbuffer2(i, info) == -1 ) {
[0]1187    ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i);
1188    continue;
1189   }
1190
[139]1191//   printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
1192
[0]1193   ROAR_DBG("streams_get_mixbuffers(*):  bufs[have] = %p", bufs[have]);
1194   ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]);
1195
[1887]1196   if ( !streams_get_flag(i, ROAR_FLAG_MUTE) )
1197    have++; // we have a new stream!
[0]1198  }
1199 }
1200
1201 bufs[have] = NULL;
[139]1202 //printf("D: bufs[have=%i] = %p\n", have, bufs[have]);
[0]1203
1204 ROAR_DBG("streams_get_mixbuffers(*): have = %i", have);
1205
1206 *bufferlist = bufs;
[547]1207 return have;
[0]1208}
1209
1210
1211int stream_add_buffer  (int id, struct roar_buffer * buf) {
1212 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
1213
[2734]1214 _CHECK_SID(id);
[0]1215
1216 if ( g_streams[id]->buffer == NULL ) {
1217  g_streams[id]->buffer = buf;
1218  ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf);
1219  return 0;
1220 }
1221
1222 ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf);
1223 return roar_buffer_add(g_streams[id]->buffer, buf);
1224}
1225
[2061]1226int stream_shift_out_buffer   (int id, void * data, size_t * len) {
[2734]1227 _CHECK_SID(id);
[2061]1228
1229 if ( g_streams[id]->buffer == NULL )
1230  return -1;
1231
1232 return roar_buffer_shift_out(&(g_streams[id]->buffer), data, len);
1233}
1234
[0]1235int stream_shift_buffer   (int id, struct roar_buffer ** buf) {
1236 struct roar_buffer * next;
1237
[2734]1238 _CHECK_SID(id);
[0]1239
1240 if ( g_streams[id]->buffer == NULL ) {
1241  *buf = NULL;
1242  return 0;
1243 }
1244
1245 roar_buffer_get_next(g_streams[id]->buffer, &next);
1246
1247 *buf                  = g_streams[id]->buffer;
1248 g_streams[id]->buffer = next;
1249
1250 return 0;
1251}
1252int stream_unshift_buffer (int id, struct roar_buffer *  buf) {
[2734]1253 _CHECK_SID(id);
[0]1254
1255 if ( g_streams[id]->buffer == NULL ) {
1256  g_streams[id]->buffer = buf;
1257  return 0;
1258 }
1259
[271]1260 buf->next = NULL;
1261
[0]1262 roar_buffer_add(buf, g_streams[id]->buffer);
1263
1264 g_streams[id]->buffer = buf;
1265
1266 return 0;
1267}
1268
[2151]1269int stream_outputbuffer_request(int id, struct roar_buffer ** buf, size_t len) {
1270 register struct roar_stream_server *  ss;
1271 size_t buflen;
[2159]1272 void * bufdata;
[2151]1273 int ret;
1274
[2734]1275 _CHECK_SID(id);
1276
[2151]1277 if ( (ss = g_streams[id]) == NULL )
1278  return -1;
1279
1280 if ( buf != NULL ) /* just be be sure */
1281  *buf = NULL;
1282
1283 if ( ss->outputbuffer != NULL ) {
1284  if ( roar_buffer_get_len(ss->outputbuffer, &buflen) == 0 ) {
1285   if ( buflen == len ) {
1286    if ( buf != NULL )
1287     *buf = ss->outputbuffer;
1288    return 0;
1289   } else if ( buflen > len ) {
1290    if ( roar_buffer_set_len(ss->outputbuffer, len) == 0 ) {
1291     if ( buf != NULL )
1292      *buf = ss->outputbuffer;
1293     return 0;
1294    }
1295   }
1296  }
1297
1298  // if the buffer is not suitable:
1299
1300  ret = roar_buffer_free(ss->outputbuffer);
1301  ss->outputbuffer = NULL;
1302  if ( ret == -1 )
1303   return ret;
1304 }
1305
1306 if ( roar_buffer_new(&(ss->outputbuffer), len) == -1 )
1307  return -1;
1308
[2159]1309 if ( roar_buffer_get_data(ss->outputbuffer, &bufdata) == -1 ) {
1310  roar_buffer_free(ss->outputbuffer);
1311  ss->outputbuffer = NULL;
1312  return -1;
1313 }
1314
1315 memset(bufdata, 0, len);
1316
[2151]1317 if ( buf != NULL )
1318  *buf = ss->outputbuffer;
1319
1320 return 0;
1321}
1322
1323int stream_outputbuffer_destroy(int id) {
1324 int ret;
1325 register struct roar_stream_server *  ss;
1326
[2734]1327 _CHECK_SID(id);
1328
[2151]1329 if ( (ss = g_streams[id]) == NULL )
1330  return -1;
1331
1332 if ( ss->outputbuffer != NULL ) {
1333  ret = roar_buffer_free(ss->outputbuffer);
1334  ss->outputbuffer = NULL;
1335  return ret;
1336 }
1337
1338 return 0;
1339}
1340
[2816]1341int stream_prethru_add(int id, struct roar_buffer * buf) {
1342 register struct roar_stream_server *  ss;
1343
1344 _CHECK_SID(id);
1345
1346 if ( (ss = g_streams[id]) == NULL )
1347  return -1;
1348
1349 if ( ss->prethru == NULL ) {
1350  ss->prethru = buf;
1351  return 0;
1352 }
1353
1354 if ( roar_buffer_add(ss->prethru, buf) == -1 ) {
1355  return -1;
1356 }
1357
1358 return 0;
1359}
1360
1361int stream_prethru_add_data(int id, void ** buf, size_t len) {
1362 struct roar_buffer * buffer;
1363
1364 _CHECK_SID(id);
1365
1366 if ( roar_buffer_new(&buffer, len) == -1 )
1367  return -1;
1368
1369 if ( roar_buffer_get_data(buffer, buf) == -1 ) {
1370  roar_buffer_free(buffer);
1371  return -1;
1372 }
1373
1374 if ( stream_prethru_add(id, buffer) == -1 ) {
1375  roar_buffer_free(buffer);
1376  return -1;
1377 }
1378
1379 return 0;
1380}
1381
1382int stream_prethru_destroy(int id) {
1383 int ret;
1384 register struct roar_stream_server *  ss;
1385
1386 _CHECK_SID(id);
1387
1388 if ( (ss = g_streams[id]) == NULL )
1389  return -1;
1390
1391 if ( ss->prethru != NULL ) {
1392  ret = roar_buffer_free(ss->prethru);
1393  ss->prethru = NULL;
1394  return ret;
1395 }
1396
1397 return 0;
1398}
1399
1400int stream_prethru_send(int dst, int src) {
1401 struct roar_stream_server *  dst_ss, * src_ss;
1402 struct roar_buffer * bufbuf;
1403 void * bufdata;
1404 size_t buflen;
1405
1406 ROAR_DBG("stream_prethru_send(dst=%i, src=%i) = ?", dst, src);
1407
1408 _CHECK_SID(dst);
1409 _CHECK_SID(src);
1410
1411 if ( (dst_ss = g_streams[dst]) == NULL )
1412  return -1;
1413
1414 if ( (src_ss = g_streams[src]) == NULL )
1415  return -1;
1416
1417 bufbuf = src_ss->prethru;
1418
1419 ROAR_DBG("stream_prethru_send(dst=%i, src=%i): prethru buffer at %p", dst, src, bufbuf);
1420
1421 while (bufbuf != NULL) {
1422  ROAR_DBG("stream_prethru_send(dst=%i, src=%i): looping with buffer at %p", dst, src, bufbuf);
1423
1424  if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 )
1425   return -1;
1426
1427  if ( roar_buffer_get_len(bufbuf, &buflen) == -1 )
1428   return -1;
1429
1430  if ( stream_vio_s_write(dst_ss, bufdata, buflen) != buflen )
1431   return -1;
1432
1433  if ( roar_buffer_get_next(bufbuf, &bufbuf) == -1 )
1434   return -1;
1435 }
1436
1437 ROAR_DBG("stream_prethru_send(dst=%i, src=%i) = 0", dst, src);
1438 return 0;
1439}
1440
[0]1441int streams_check  (int id) {
1442 int fh;
[508]1443 ssize_t req, realreq, done;
[0]1444 struct roar_stream        *   s;
1445 struct roar_stream_server *  ss;
1446 struct roar_buffer        *   b;
1447 char                      * buf;
[2248]1448// char                        tmp;
[0]1449
[2734]1450 _CHECK_SID(id);
[0]1451
1452 ROAR_DBG("streams_check(id=%i) = ?", id);
1453
[609]1454 s = ROAR_STREAM(ss = g_streams[id]);
[0]1455
1456 if ( (fh = s->fh) == -1 )
1457  return 0;
1458
[1821]1459 if ( streams_get_flag(id, ROAR_FLAG_PAUSE) )
[0]1460  return 0;
1461
[1821]1462 switch (s->dir) {
1463  case ROAR_DIR_LIGHT_IN:
[2493]1464#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1821]1465    return light_check_stream(id);
[2493]1466#else
1467    streams_delete(id);
1468    return -1;
1469#endif
[1821]1470   break;
[1845]1471  case ROAR_DIR_MIDI_IN:
[2493]1472#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1845]1473    return midi_check_stream(id);
[2493]1474#else
1475    streams_delete(id);
1476    return -1;
1477#endif
[1845]1478   break;
[2237]1479  case ROAR_DIR_RAW_IN:
[2493]1480#ifndef ROAR_WITHOUT_DCOMP_RAW
[2237]1481    return raw_check_stream(id);
[2493]1482#else
1483    streams_delete(id);
1484    return -1;
1485#endif
[2237]1486   break;
[2721]1487  case ROAR_DIR_RDTCS_IN:
1488#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1489    return rdtcs_check_stream(id);
1490#else
1491    streams_delete(id);
1492    return -1;
1493#endif
1494   break;
[1821]1495  case ROAR_DIR_PLAY:
1496  case ROAR_DIR_BIDIR:
1497   break;
1498  default:
[2248]1499/*
1500    ROAR_WARN("streams_check(id=%i): Read event on non input stream of type/dir %s", id, roar_dir2str(s->dir));
1501    errno = 0;
1502    req = stream_vio_s_read(ss, &tmp, 1);
1503    ROAR_DBG("streams_check(id=%i): stream_vio_s_read(ss, &tmp, 1) = %li // errno=%s(%i)", id, req, strerror(errno), errno);
1504    if ( req == 1 ) {
1505     ROAR_ERR("streams_check(id=%i): Client violates protocol, got one byte of data on output stream, kicking stream");
1506     streams_delete(id);
1507     return -1;
1508    }
1509*/
[1821]1510    return 0;
1511   break;
1512 }
[1585]1513
[0]1514 ROAR_DBG("streams_check(id=%i): fh = %i", id, fh);
1515
[2387]1516/*
1517 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);
1518*/
1519
1520 req  = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // optimal size
1521// req  = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * (s->info.bits / 8) * ((float)s->info.rate/g_sa->rate);
[0]1522 req += ss->need_extra; // bytes left we sould get....
1523
[2387]1524 ROAR_DBG("streams_check(id=%i): asking for %i bytes", id, req);
1525
[0]1526 if ( roar_buffer_new(&b, req) == -1 ) {
1527  ROAR_ERR("streams_check(*): Can not alloc buffer space!");
1528  ROAR_DBG("streams_check(*) = -1");
1529  return -1;
1530 }
1531
1532 roar_buffer_get_data(b, (void **)&buf);
1533
1534 ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id);
[2387]1535 ROAR_DBG("streams_check(id=%i): asking for %i bytes", id, req);
[0]1536
[270]1537 if ( ss->codecfilter == -1 ) {
[508]1538  realreq = req;
1539/*
[270]1540  req = read(fh, buf, req);
[508]1541  if ( req < realreq ) { // we can do this as the stream is in nonblocking mode!
1542   if ( (realreq = read(fh, buf+req, realreq-req)) > 0 )
1543    req += realreq;
1544  }
1545*/
1546  done = 0;
1547  while (req > 0 && done != realreq) {
[881]1548   if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 )
[508]1549    done += req;
1550  }
1551  req = done;
[1837]1552
1553  roar_buffer_get_data(b, (void **)&buf);
[270]1554 } else {
1555  req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req);
1556 }
1557
1558 if ( req > 0 ) {
[0]1559  ROAR_DBG("streams_check(id=%i): got %i bytes", id, req);
1560
1561  roar_buffer_set_len(b, req);
1562
1563  if ( stream_add_buffer(id, b) != -1 )
1564   return 0;
1565
1566  ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id);
1567  roar_buffer_free(b);
1568 } else {
[272]1569  ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno));
1570#ifdef ROAR_HAVE_LIBVORBISFILE
1571  if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :)
1572#else
1573  if ( errno != EAGAIN ) {
1574#endif
1575   ROAR_DBG("streams_check(id=%i): EOF!", id);
1576   streams_delete(id);
1577   ROAR_DBG("streams_check(id=%i) = 0", id);
1578  }
[334]1579  roar_buffer_free(b);
[0]1580  return 0;
1581 }
1582
1583
1584 ROAR_DBG("streams_check(id=%i) = -1", id);
1585 return -1;
1586}
1587
1588
[2152]1589#define _return(x) return (x)
[0]1590int streams_send_mon   (int id) {
[936]1591// int fh;
[0]1592 struct roar_stream        *   s;
1593 struct roar_stream_server *  ss;
[2146]1594 struct roar_buffer        *  bufbuf = NULL;
[2155]1595 struct roar_remove_state     removalstate;
[2148]1596 void  * ip;
[1157]1597 void  * obuf;
1598 int     olen;
[2150]1599 int     is_the_same     = 1;
1600 int     is_vol_eq       = 1;
[2155]1601 int     antiecho        = 0;
[1157]1602 ssize_t ret;
[0]1603
[2734]1604 _CHECK_SID(id);
[0]1605
1606 ROAR_DBG("streams_send_mon(id=%i) = ?", id);
1607
[585]1608 s = ROAR_STREAM((ss = g_streams[id]));
[0]1609
[934]1610/*
[0]1611 if ( (fh = s->fh) == -1 )
1612  return 0;
[934]1613*/
[0]1614
[1914]1615 if ( !ss->ready )
1616  return 0;
[930]1617
[3042]1618 if ( g_config->jumbo_mtu )
1619  roar_vio_sync(ss->viop);
1620
[1585]1621 if ( streams_get_flag(id, ROAR_FLAG_PAUSE) )
1622  return 0;
1623
[1821]1624 switch (s->dir) {
1625  case ROAR_DIR_LIGHT_OUT:
[2493]1626#ifndef ROAR_WITHOUT_DCOMP_LIGHT
[1821]1627    return light_send_stream(id);
[2493]1628#else
1629    streams_delete(id);
1630    return -1;
1631#endif
[1821]1632   break;
[1845]1633  case ROAR_DIR_MIDI_OUT:
[2493]1634#ifndef ROAR_WITHOUT_DCOMP_MIDI
[1845]1635    return midi_send_stream(id);
[2493]1636#else
1637    streams_delete(id);
1638    return -1;
1639#endif
[1845]1640   break;
[2721]1641  case ROAR_DIR_RDTCS_OUT:
1642#ifndef ROAR_WITHOUT_DCOMP_RDTCS
1643    return rdtcs_send_stream(id);
1644#else
1645    streams_delete(id);
1646    return -1;
1647#endif
1648   break;
[2694]1649
1650  case ROAR_DIR_COMPLEX_OUT:
1651    // send a tick:
1652    if ( ss->codecfilter != -1 ) {
1653     if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, NULL, 0) == 0 )
1654      ss->state = ROAR_STREAMSTATE_OLD;
1655    }
1656    return 0;
1657   break;
1658
[1821]1659  case ROAR_DIR_OUTPUT:
1660    if ( g_standby )
1661     return 0;
1662  case ROAR_DIR_MONITOR:
1663  case ROAR_DIR_BIDIR:
1664   break;
1665
1666  default:
1667    return 0;
1668   break;
1669 }
1670
1671
[1042]1672 ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, s->fh);
[0]1673
[625]1674 if ( s->info.channels != g_sa->channels || s->info.bits  != g_sa->bits ||
[2147]1675      s->info.rate     != g_sa->rate     || s->info.codec != g_sa->codec  )
1676  is_the_same = 0;
1677
[2150]1678 if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) {
1679  is_vol_eq = need_vol_change(g_sa->channels, &(ss->mixer)) ? 0 : 1;
1680 }
1681
[2155]1682 if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) )
1683  antiecho = 1;
1684
1685 if ( !is_the_same || !is_vol_eq || antiecho ) {
[625]1686  olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len
1687                                                  // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here
[2390]1688  if ( stream_outputbuffer_request(id, &bufbuf, ROAR_OUTPUT_CALC_OUTBUFSIZE_MAX(&(s->info), g_sa)) == -1 )
[625]1689   return -1;
1690
[2146]1691  if ( roar_buffer_get_data(bufbuf, &obuf) == -1 ) {
1692   _return(-1);
1693  }
1694
[625]1695  ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen);
[2147]1696 } else {
1697  obuf = g_output_buffer;
1698  olen = g_output_buffer_len;
1699 }
[625]1700
[2148]1701 ip = g_output_buffer;
1702
[2155]1703 if ( antiecho ) {
[2397]1704  ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho);
[2158]1705  if ( roar_remove_init(&removalstate) == -1 ) {
[2155]1706   _return(-1);
[2158]1707  }
1708
[2397]1709  ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho);
[2158]1710  if ( roar_remove_so(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->bits, &removalstate) == -1 ) {
[2397]1711   ROAR_DBG("streams_send_mon(id=%i): anti echo failed", id);
[2158]1712   _return(-1);
1713  }
[2397]1714  ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho);
[2155]1715 }
1716
[2150]1717 if ( !is_vol_eq ) {
[2938]1718  if ( roar_amp_pcm(obuf, g_sa->bits, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->channels, &(ss->mixer)) == -1 ) {
[2150]1719   _return(-1);
1720  }
1721
1722  ip = obuf;
1723 }
1724
[2147]1725 if ( !is_the_same ) {
[2148]1726  if ( roar_conv(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) {
[2146]1727   _return(-1);
[625]1728  }
1729 }
1730
[585]1731 errno = 0;
1732
1733 if ( ss->codecfilter == -1 ) {
[1042]1734  ROAR_DBG("streams_send_mon(id=%i): not a CF stream", id);
[2146]1735  if ( s->fh == -1 && roar_vio_get_fh(&(ss->vio)) == -1 ) {
[2705]1736   ROAR_DBG("streams_send_mon(id=%i) = 0", id);
[2146]1737   _return(0);
1738  }
[1014]1739
[2705]1740  ROAR_DBG("streams_send_mon(id=%i) = ?", id);
1741
[1157]1742  if ( (ret = stream_vio_s_write(ss, obuf, olen)) == olen ) {
[981]1743   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
[2635]1744   ss->state = ROAR_STREAMSTATE_OLD;
[2705]1745   ROAR_DBG("streams_send_mon(id=%i) = 0", id);
[2146]1746   _return(0);
[625]1747  }
[1157]1748
[2705]1749  ROAR_DBG("streams_send_mon(id=%i) = ?", id);
1750
[1157]1751  if ( ret > 0 && errno == 0 ) {
[1231]1752   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]1753   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), ret)*s->info.channels);
[2635]1754   ss->state = ROAR_STREAMSTATE_OLD;
[2146]1755   _return(0);
[1157]1756  }
[585]1757 } else {
[1012]1758  errno = 0;
[625]1759  if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen)
1760            == olen ) {
[981]1761   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
[2635]1762   ss->state = ROAR_STREAMSTATE_OLD;
[2146]1763   _return(0);
[585]1764  } else { // we cann't retry on codec filetered streams
[1012]1765   if ( errno != EAGAIN ) {
1766    streams_delete(id);
[2146]1767    _return(-1);
[1012]1768   }
[585]1769  }
1770 }
[0]1771
[129]1772 if ( errno == EAGAIN ) {
1773  // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to
1774  // make any gapes in any output because of this
1775
[1750]1776#ifdef ROAR_HAVE_USLEEP
[129]1777  usleep(100); // 0.1ms
[1750]1778#endif
[129]1779
[881]1780  if ( stream_vio_s_write(ss, obuf, olen) == olen ) {
[981]1781   s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels);
[2635]1782   ss->state = ROAR_STREAMSTATE_OLD;
[2146]1783   _return(0);
[1015]1784  } else if ( errno == EAGAIN ) {
1785   ROAR_WARN("streams_send_mon(id=%i): Can not send data to client: %s", id, strerror(errno));
[2146]1786   _return(0);
[625]1787  }
[129]1788 }
1789
[0]1790 // ug... error... delete stream!
1791
1792 streams_delete(id);
1793
[2146]1794 _return(-1);
[0]1795}
[2146]1796#undef _return
[0]1797
1798int streams_send_filter(int id) {
1799 int fh;
[127]1800 int have = 0;
1801 int len;
[0]1802 struct roar_stream        *   s;
1803 struct roar_stream_server *  ss;
1804
[2734]1805 _CHECK_SID(id);
[0]1806
1807 ROAR_DBG("streams_send_filter(id=%i) = ?", id);
1808
[609]1809 s = ROAR_STREAM(ss = g_streams[id]);
[0]1810
1811 if ( (fh = s->fh) == -1 )
1812  return 0;
1813
1814 if ( s->dir != ROAR_DIR_FILTER )
1815  return 0;
1816
[1585]1817 if ( streams_get_flag(id, ROAR_FLAG_PAUSE) )
1818  return 0;
1819
1820
[0]1821 ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh);
1822
[881]1823 if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) {
[127]1824  while ( have < g_output_buffer_len ) {
[881]1825   if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) {
[127]1826    streams_delete(id);
1827    return -1;
1828   }
1829   have += len;
[0]1830  }
[127]1831  return 0;
[0]1832 }
1833
1834 // ug... error... delete stream!
1835
1836 streams_delete(id);
1837
1838 return -1;
1839}
1840
[596]1841
1842// VIO:
1843
1844ssize_t stream_vio_read (int stream, void *buf, size_t count) {
[2734]1845 _CHECK_SID(stream);
[596]1846
[2734]1847 return stream_vio_s_read(g_streams[stream], buf, count);
[596]1848}
1849
1850ssize_t stream_vio_write(int stream, void *buf, size_t count) {
[2734]1851 _CHECK_SID(stream);
[596]1852
[2734]1853 return stream_vio_s_write(g_streams[stream], buf, count);
[596]1854}
1855
1856
1857ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) {
[2253]1858 void    * orig_buf = buf;
1859  size_t   len      =  0;
1860 ssize_t   r        = -1;
1861 int       i;
[739]1862
[596]1863 errno = 0;
1864
1865 if ( !stream )
1866  return -1;
1867
[1613]1868 //roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh);
[881]1869
[596]1870 if ( ! stream->vio.read )
1871  return -1;
1872
[881]1873 while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) {
[739]1874  len   += r;
1875  buf   += r;
1876  count -= r;
1877  if ( count == 0 )
1878   break;
1879 }
1880
[740]1881 if ( len == 0 && r == -1 )
1882  return -1;
1883
[2816]1884 if ( streams_thru_num ) {
1885  for (i = 0; i < ROAR_STREAMS_MAX; i++) {
1886   if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == ROAR_STREAM(stream)->id ) {
1887    if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) {
1888     if ( g_streams[i]->ready ) {
[2258]1889      if ( stream_vio_write(i, orig_buf, len) != len )
1890       streams_delete(i);
[2252]1891
[2816]1892      if ( g_streams[i] != NULL )
1893       g_streams[i]->state = ROAR_STREAMSTATE_OLD;
1894     }
1895    }
1896   }
1897  }
1898 }
1899
[739]1900 return len;
[596]1901}
1902
1903ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) {
[2259]1904 int i;
1905
[596]1906 errno = 0;
1907
1908 if ( !stream )
1909  return -1;
1910
[1613]1911/*
[934]1912 if ( roar_vio_get_fh(&(stream->vio)) == -1 && ROAR_STREAM(stream)->fh != -1 )
1913  roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh);
[1613]1914*/
[934]1915
1916// ROAR_WARN("stream_vio_s_write(*): writing...");
[596]1917
[2816]1918 if ( streams_thru_num ) {
1919  for (i = 0; i < ROAR_STREAMS_MAX; i++) {
1920   if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == ROAR_STREAM(stream)->id ) {
1921    if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) {
1922     if ( g_streams[i]->ready ) {
1923      if ( g_streams[i]->state == ROAR_STREAMSTATE_NEW ) {
1924       if ( streams_get_flag(i, ROAR_FLAG_PRETHRU) == 1 ) {
[3042]1925        if ( stream_prethru_send(i, ROAR_STREAM(stream)->id) == -1 ) {
[2816]1926         streams_delete(i);
1927        }
1928       }
1929      }
1930
1931      if ( stream_vio_write(i, buf, count) != count ) {
[2259]1932       streams_delete(i);
[2816]1933      }
1934
1935      if ( g_streams[i] != NULL )
1936       g_streams[i]->state = ROAR_STREAMSTATE_OLD;
1937     }
1938    }
1939   }
1940  }
1941 }
[2259]1942
[3042]1943 if ( g_config->jumbo_mtu ) {
1944  if ( stream->viop != &(stream->jumbo) ) {
1945   if ( roar_vio_open_jumbo(&(stream->jumbo), &(stream->vio), g_config->jumbo_mtu) != -1 ) {
1946    // if that works we continue using the jumbo vio,
1947    // in case it didn't we dont, just use normal VIO.
1948    stream->viop = &(stream->jumbo);
1949   }
1950  }
1951 }
1952
1953 return roar_vio_write(stream->viop, buf, count);
[596]1954}
1955
[0]1956//ll
Note: See TracBrowser for help on using the repository browser.