source: roaraudio/libroarsndio/stream.c @ 3237:ea0a2c70c0cd

Last change on this file since 3237:ea0a2c70c0cd was 3237:ea0a2c70c0cd, checked in by phi, 14 years ago

support nonblocking mode

File size: 3.9 KB
Line 
1//stream.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009
5 *  The code (may) include prototypes and comments (and maybe
6 *  other code fragements) from EsounD.
7 *  They are mostly copyrighted by Eric B. Mitchell (aka 'Ricdude)
8 *  <ericmit@ix.netcom.com>. For more information see AUTHORS.esd.
9 *
10 *  This file is part of libroaresd a part of RoarAudio,
11 *  a cross-platform sound system for both, home and professional use.
12 *  See README for details.
13 *
14 *  This file is free software; you can redistribute it and/or modify
15 *  it under the terms of the GNU General Public License version 3
16 *  as published by the Free Software Foundation.
17 *
18 *  RoarAudio is distributed in the hope that it will be useful,
19 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 *  GNU General Public License for more details.
22 *
23 *  You should have received a copy of the GNU General Public License
24 *  along with this software; see the file COPYING.  If not, write to
25 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 *
27 *  NOTE for everyone want's to change something and send patches:
28 *  read README and HACKING! There a addition information on
29 *  the license of this document you need to read before you send
30 *  any patches.
31 */
32
33#define ROAR_USE_OWN_SNDIO_HDL
34#include "libroarsndio.h"
35
36// this is void because we do not care about if this failed...
37static void send_vol_event (struct sio_hdl * hdl) {
38 int channels;
39 struct roar_mixer_settings mixer;
40 int i;
41 unsigned vol;
42
43 // in case of no event handler we do not send the event at all ;)
44 if ( hdl->on_vol == NULL )
45  return;
46
47 if ( roar_get_vol(&(hdl->con), roar_stream_get_id(&(hdl->stream)), &mixer, &channels) == -1 )
48  return;
49
50 ROAR_DBG("send_vol_event(*): channels=%i", channels);
51
52 switch (channels) {
53  case 1:
54    ROAR_DBG("send_vol_event(*): mixer.scale=%i, mixer.mixer={%i,...}", mixer.scale, mixer.mixer[0]);
55    vol  = mixer.mixer[0] * SIO_MAXVOL;
56    vol /= mixer.scale;
57    ROAR_DBG("send_vol_event(*): vol=%u", vol);
58   break;
59  case 2:
60    vol = (mixer.mixer[0] + mixer.mixer[1]) * SIO_MAXVOL / mixer.scale / 2;
61   break;
62  default:
63    vol = 0;
64    for (i = 0; i < channels; i++)
65     vol += mixer.mixer[i];
66
67    vol /= channels;
68    vol *= SIO_MAXVOL;
69    vol /= mixer.scale;
70   break;
71 }
72
73 ROAR_DBG("send_vol_event(*): vol=%u", vol);
74 hdl->on_vol(hdl->on_vol_arg, vol);
75}
76
77#define _i(x) (hdl->info.x)
78int    sio_start  (struct sio_hdl * hdl) {
79
80 // TODO: FIXME: use full VIO support here, not fh->vio!
81
82 if ( hdl == NULL )
83  return 0;
84
85 if ( hdl->stream_opened )
86  return 0;
87
88 if ( roar_vio_simple_new_stream_obj(&(hdl->svio), &(hdl->con), &(hdl->stream),
89                                     _i(rate), _i(channels), _i(bits), _i(codec), hdl->dir) == -1 )
90  return 0;
91
92 ROAR_DBG("sio_start(hdl=%p): rate=%i, channels=%i, bits=%i, codec=%i", hdl, _i(rate), _i(channels), _i(bits), _i(codec));
93
94 if ( hdl->nonblock ) {
95  if ( roar_vio_nonblock(&(hdl->svio), ROAR_SOCKET_NONBLOCK) == -1 ) {
96   roar_vio_close(&(hdl->svio));
97   return 0;
98  }
99 }
100
101 send_vol_event(hdl);
102
103 hdl->stream_opened = 1;
104
105 return 1;
106}
107#undef _i
108
109int    sio_stop   (struct sio_hdl * hdl) {
110
111 if ( hdl == NULL )
112  return 0;
113
114 if ( !hdl->stream_opened )
115  return 0;
116
117 roar_vio_close(&(hdl->svio));
118
119 hdl->stream_opened = -1;
120
121 return 1;
122}
123
124size_t sio_read   (struct sio_hdl * hdl, void * addr, size_t nbytes) {
125 ssize_t ret;
126
127 if ( hdl == NULL )
128  return 0;
129
130 if ( !hdl->stream_opened )
131  return 0;
132
133 if ( (ret = roar_vio_read(&(hdl->svio), addr, nbytes)) < 0 )
134  return 0;
135
136 return ret;
137}
138size_t sio_write  (struct sio_hdl * hdl, const void * addr, size_t nbytes) {
139 ssize_t ret;
140
141 if ( hdl == NULL )
142  return 0;
143
144 if ( !hdl->stream_opened )
145  return 0;
146
147 if ( (ret = roar_vio_write(&(hdl->svio), (void*) addr, nbytes)) < 0 )
148  return 0;
149
150 if ( hdl->on_move != NULL ) {
151  hdl->on_move(hdl->on_move_arg, 8*ret/(hdl->info.channels * hdl->info.bits));
152 }
153
154 return ret;
155}
156
157//ll
Note: See TracBrowser for help on using the repository browser.