1 | //stream.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2011 |
---|
5 | * The code (may) include prototypes and comments (and maybe |
---|
6 | * other code fragements) from OpenBSD's sndio. |
---|
7 | * See 'Copyright for sndio' below for more information on |
---|
8 | * code fragments taken from OpenBSD's sndio. |
---|
9 | * |
---|
10 | * --- Copyright for sndio --- |
---|
11 | * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org> |
---|
12 | * |
---|
13 | * Permission to use, copy, modify, and distribute this software for any |
---|
14 | * purpose with or without fee is hereby granted, provided that the above |
---|
15 | * copyright notice and this permission notice appear in all copies. |
---|
16 | * |
---|
17 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
---|
18 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
---|
19 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
---|
20 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
---|
21 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
---|
22 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
---|
23 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
24 | * --- End of Copyright for sndio --- |
---|
25 | * |
---|
26 | * This file is part of libroaresd a part of RoarAudio, |
---|
27 | * a cross-platform sound system for both, home and professional use. |
---|
28 | * See README for details. |
---|
29 | * |
---|
30 | * This file is free software; you can redistribute it and/or modify |
---|
31 | * it under the terms of the GNU General Public License version 3 |
---|
32 | * as published by the Free Software Foundation. |
---|
33 | * |
---|
34 | * RoarAudio is distributed in the hope that it will be useful, |
---|
35 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
36 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
37 | * GNU General Public License for more details. |
---|
38 | * |
---|
39 | * You should have received a copy of the GNU General Public License |
---|
40 | * along with this software; see the file COPYING. If not, write to |
---|
41 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
42 | * Boston, MA 02110-1301, USA. |
---|
43 | * |
---|
44 | * NOTE for everyone want's to change something and send patches: |
---|
45 | * read README and HACKING! There a addition information on |
---|
46 | * the license of this document you need to read before you send |
---|
47 | * any patches. |
---|
48 | */ |
---|
49 | |
---|
50 | #define ROAR_USE_OWN_SNDIO_HDL |
---|
51 | #include "libroarsndio.h" |
---|
52 | |
---|
53 | // this is void because we do not care about if this failed... |
---|
54 | static void send_vol_event (struct sio_hdl * hdl) { |
---|
55 | int channels; |
---|
56 | struct roar_mixer_settings mixer; |
---|
57 | int i; |
---|
58 | unsigned vol; |
---|
59 | |
---|
60 | // in case of no event handler we do not send the event at all ;) |
---|
61 | if ( hdl->on_vol == NULL ) |
---|
62 | return; |
---|
63 | |
---|
64 | if ( roar_get_vol(&(hdl->con), roar_stream_get_id(&(hdl->stream)), &mixer, &channels) == -1 ) |
---|
65 | return; |
---|
66 | |
---|
67 | ROAR_DBG("send_vol_event(*): channels=%i", channels); |
---|
68 | |
---|
69 | switch (channels) { |
---|
70 | case 1: |
---|
71 | ROAR_DBG("send_vol_event(*): mixer.scale=%i, mixer.mixer={%i,...}", mixer.scale, mixer.mixer[0]); |
---|
72 | vol = mixer.mixer[0] * SIO_MAXVOL; |
---|
73 | vol /= mixer.scale; |
---|
74 | ROAR_DBG("send_vol_event(*): vol=%u", vol); |
---|
75 | break; |
---|
76 | case 2: |
---|
77 | vol = (mixer.mixer[0] + mixer.mixer[1]) * SIO_MAXVOL / mixer.scale / 2; |
---|
78 | break; |
---|
79 | default: |
---|
80 | vol = 0; |
---|
81 | for (i = 0; i < channels; i++) |
---|
82 | vol += mixer.mixer[i]; |
---|
83 | |
---|
84 | vol /= channels; |
---|
85 | vol *= SIO_MAXVOL; |
---|
86 | vol /= mixer.scale; |
---|
87 | break; |
---|
88 | } |
---|
89 | |
---|
90 | ROAR_DBG("send_vol_event(*): vol=%u", vol); |
---|
91 | hdl->on_vol(hdl->on_vol_arg, vol); |
---|
92 | } |
---|
93 | |
---|
94 | #define _i(x) (hdl->info.x) |
---|
95 | int sio_start (struct sio_hdl * hdl) { |
---|
96 | |
---|
97 | // TODO: FIXME: use full VIO support here, not fh->vio! |
---|
98 | |
---|
99 | if ( hdl == NULL ) |
---|
100 | return 0; |
---|
101 | |
---|
102 | if ( hdl->stream_opened ) |
---|
103 | return 0; |
---|
104 | |
---|
105 | if ( roar_vio_simple_new_stream_obj(&(hdl->svio), &(hdl->con), &(hdl->stream), |
---|
106 | _i(rate), _i(channels), _i(bits), _i(codec), hdl->dir) == -1 ) |
---|
107 | return 0; |
---|
108 | |
---|
109 | ROAR_DBG("sio_start(hdl=%p): rate=%i, channels=%i, bits=%i, codec=%i", hdl, _i(rate), _i(channels), _i(bits), _i(codec)); |
---|
110 | |
---|
111 | if ( hdl->nonblock ) { |
---|
112 | if ( roar_vio_nonblock(&(hdl->svio), ROAR_SOCKET_NONBLOCK) == -1 ) { |
---|
113 | roar_vio_close(&(hdl->svio)); |
---|
114 | return 0; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | send_vol_event(hdl); |
---|
119 | |
---|
120 | hdl->stream_opened = 1; |
---|
121 | hdl->ioerror = 0; |
---|
122 | |
---|
123 | return 1; |
---|
124 | } |
---|
125 | #undef _i |
---|
126 | |
---|
127 | int sio_stop (struct sio_hdl * hdl) { |
---|
128 | |
---|
129 | if ( hdl == NULL ) |
---|
130 | return 0; |
---|
131 | |
---|
132 | if ( !hdl->stream_opened ) |
---|
133 | return 0; |
---|
134 | |
---|
135 | roar_vio_close(&(hdl->svio)); |
---|
136 | |
---|
137 | hdl->stream_opened = -1; |
---|
138 | |
---|
139 | return 1; |
---|
140 | } |
---|
141 | |
---|
142 | size_t sio_read (struct sio_hdl * hdl, void * addr, size_t nbytes) { |
---|
143 | ssize_t ret; |
---|
144 | |
---|
145 | if ( hdl == NULL ) |
---|
146 | return 0; |
---|
147 | |
---|
148 | if ( !hdl->stream_opened ) |
---|
149 | return 0; |
---|
150 | |
---|
151 | if ( (ret = roar_vio_read(&(hdl->svio), addr, nbytes)) < 0 ) { |
---|
152 | hdl->ioerror = 1; |
---|
153 | return 0; |
---|
154 | } |
---|
155 | |
---|
156 | if ( hdl->nonblock ) |
---|
157 | hdl->ioerror = 0; |
---|
158 | |
---|
159 | return ret; |
---|
160 | } |
---|
161 | size_t sio_write (struct sio_hdl * hdl, const void * addr, size_t nbytes) { |
---|
162 | ssize_t ret; |
---|
163 | |
---|
164 | if ( hdl == NULL ) |
---|
165 | return 0; |
---|
166 | |
---|
167 | if ( !hdl->stream_opened ) |
---|
168 | return 0; |
---|
169 | |
---|
170 | if ( (ret = roar_vio_write(&(hdl->svio), (void*) addr, nbytes)) < 0 ) { |
---|
171 | hdl->ioerror = 1; |
---|
172 | return 0; |
---|
173 | } |
---|
174 | |
---|
175 | if ( hdl->nonblock ) |
---|
176 | hdl->ioerror = 0; |
---|
177 | |
---|
178 | if ( hdl->on_move != NULL ) { |
---|
179 | hdl->on_move(hdl->on_move_arg, 8*ret/(hdl->info.channels * hdl->info.bits)); |
---|
180 | } |
---|
181 | |
---|
182 | return ret; |
---|
183 | } |
---|
184 | |
---|
185 | //ll |
---|