source: roaraudio/roard/streams.c @ 3517:1a3218a3fc5b

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

updated license headers, FSF moved office

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