source: roaraudio/roard/streams.c @ 3556:e489a789acf7

Last change on this file since 3556:e489a789acf7 was 3556:e489a789acf7, checked in by phi, 14 years ago

install map depending on direction

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