source: roaraudio/roard/streams.c @ 2779:bbc1d711c495

Last change on this file since 2779:bbc1d711c495 was 2766:579ea97dbcbf, checked in by phi, 15 years ago

use socket fh on win32

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