source: roaraudio/roard/streams.c @ 2721:c3d01f76c247

Last change on this file since 2721:c3d01f76c247 was 2721:c3d01f76c247, checked in by phi, 15 years ago

added stubs for rdtcs_check_stream() and rdtcs_send_stream()

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