source: roaraudio/roard/streams.c @ 2835:191b93956b80

Last change on this file since 2835:191b93956b80 was 2835:191b93956b80, checked in by phi, 15 years ago

return error on cf open failed correctly

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