source: roaraudio/roard/streams.c @ 3063:955233719a84

Last change on this file since 3063:955233719a84 was 3063:955233719a84, checked in by phi, 14 years ago

use memory functions from libroar, not libc, fixed a small memory leak

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