source: roaraudio/roard/streams.c @ 2764:306e6068d475

Last change on this file since 2764:306e6068d475 was 2764:306e6068d475, checked in by phi, 15 years ago

do try to set non-blocking on win32

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