1 | //vio_stream.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2012 |
---|
5 | * |
---|
6 | * This file is part of libroar 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 | * libroar 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, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | * NOTE for everyone want's to change something and send patches: |
---|
25 | * read README and HACKING! There a addition information on |
---|
26 | * the license of this document you need to read before you send |
---|
27 | * any patches. |
---|
28 | * |
---|
29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
30 | * or libpulse*: |
---|
31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
34 | */ |
---|
35 | |
---|
36 | #include "libroar.h" |
---|
37 | |
---|
38 | static ssize_t _vio_stream_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
39 | return roar_vio_read(roar_get_connection_vio2(vio->inst), buf, count); |
---|
40 | } |
---|
41 | |
---|
42 | static ssize_t _vio_stream_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
43 | return roar_vio_write(roar_get_connection_vio2(vio->inst), buf, count); |
---|
44 | } |
---|
45 | |
---|
46 | static roar_off_t _vio_stream_lseek (struct roar_vio_calls * vio, roar_off_t offset, int whence) { |
---|
47 | return roar_vio_lseek(roar_get_connection_vio2(vio->inst), offset, whence); |
---|
48 | } |
---|
49 | static int _vio_stream_sync (struct roar_vio_calls * vio) { |
---|
50 | return roar_vio_sync(roar_get_connection_vio2(vio->inst)); |
---|
51 | } |
---|
52 | static int _vio_stream_ctl (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) { |
---|
53 | if (vio == NULL) { |
---|
54 | roar_err_set(ROAR_ERROR_FAULT); |
---|
55 | return -1; |
---|
56 | } |
---|
57 | |
---|
58 | if (cmd == -1) { |
---|
59 | roar_err_set(ROAR_ERROR_INVAL); |
---|
60 | return -1; |
---|
61 | } |
---|
62 | |
---|
63 | switch (cmd) { |
---|
64 | case ROAR_VIO_CTL_GET_NAME: |
---|
65 | if ( data == NULL ) { |
---|
66 | roar_err_set(ROAR_ERROR_FAULT); |
---|
67 | return -1; |
---|
68 | } |
---|
69 | |
---|
70 | *(char**)data = "stream"; |
---|
71 | return 0; |
---|
72 | break; |
---|
73 | case ROAR_VIO_CTL_GET_NEXT: |
---|
74 | *(struct roar_vio_calls **)data = roar_get_connection_vio2(vio->inst); |
---|
75 | return 0; |
---|
76 | break; |
---|
77 | case ROAR_VIO_CTL_SET_NEXT: |
---|
78 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
79 | return -1; |
---|
80 | break; |
---|
81 | case ROAR_VIO_CTL_NONBLOCK: |
---|
82 | return roar_vio_ctl(roar_get_connection_vio2(vio->inst), ROAR_VIO_CTL_NONBLOCK, data); |
---|
83 | break; |
---|
84 | } |
---|
85 | |
---|
86 | return roar_vio_ctl(roar_get_connection_vio2(vio->inst), cmd, data); |
---|
87 | } |
---|
88 | |
---|
89 | static int _vio_stream_close (struct roar_vio_calls * vio) { |
---|
90 | roar_vio_close(roar_get_connection_vio2(vio->inst)); |
---|
91 | roar_mm_free(vio->inst); |
---|
92 | |
---|
93 | return 0; |
---|
94 | } |
---|
95 | |
---|
96 | int roar_vio_simple_stream (struct roar_vio_calls * calls, |
---|
97 | uint32_t rate, uint32_t channels, uint32_t bits, uint32_t codec, |
---|
98 | const char * server, int dir, const char * name, int mixer) { |
---|
99 | struct roar_connection * con = NULL; |
---|
100 | struct roar_stream stream; |
---|
101 | int err; |
---|
102 | |
---|
103 | if ( calls == NULL ) { |
---|
104 | roar_err_set(ROAR_ERROR_FAULT); |
---|
105 | return -1; |
---|
106 | } |
---|
107 | |
---|
108 | if ( roar_stream_new(&stream, rate, channels, bits, codec) == -1 ) |
---|
109 | return -1; |
---|
110 | |
---|
111 | con = roar_mm_malloc(sizeof(struct roar_connection)); |
---|
112 | if ( con == NULL ) |
---|
113 | return -1; |
---|
114 | |
---|
115 | memset(con, 0, sizeof(struct roar_connection)); |
---|
116 | |
---|
117 | if ( roar_simple_connect(con, server, name) == -1 ) { |
---|
118 | err = roar_error; |
---|
119 | roar_mm_free(con); |
---|
120 | roar_error = err; |
---|
121 | return -1; |
---|
122 | } |
---|
123 | |
---|
124 | if ( roar_stream_connect(con, &stream, dir, mixer) == -1 ) { |
---|
125 | err = roar_error; |
---|
126 | roar_disconnect(con); |
---|
127 | roar_mm_free(con); |
---|
128 | roar_error = err; |
---|
129 | return -1; |
---|
130 | } |
---|
131 | |
---|
132 | if ( roar_stream_exec(con, &stream) == -1 ) { |
---|
133 | err = roar_error; |
---|
134 | roar_disconnect(con); |
---|
135 | roar_mm_free(con); |
---|
136 | roar_error = err; |
---|
137 | return -1; |
---|
138 | } |
---|
139 | |
---|
140 | roar_vio_clear_calls(calls); |
---|
141 | |
---|
142 | calls->inst = con; |
---|
143 | calls->read = _vio_stream_read; |
---|
144 | calls->write = _vio_stream_write; |
---|
145 | calls->lseek = _vio_stream_lseek; |
---|
146 | calls->sync = _vio_stream_sync; |
---|
147 | calls->ctl = _vio_stream_ctl; |
---|
148 | calls->close = _vio_stream_close; |
---|
149 | |
---|
150 | if ( dir == ROAR_DIR_PLAY ) { |
---|
151 | roar_vio_shutdown(calls, SHUT_RD); |
---|
152 | } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) { |
---|
153 | roar_vio_shutdown(calls, SHUT_WR); |
---|
154 | } |
---|
155 | |
---|
156 | return 0; |
---|
157 | } |
---|
158 | |
---|
159 | static int _roar_simple_new_stream_obj (struct roar_connection * con, struct roar_stream * s, uint32_t rate, uint32_t channels, uint32_t bits, uint32_t codec, int dir, int mixer) { |
---|
160 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
161 | char file[80] = {0}; |
---|
162 | int fh = -1, listen = -1; |
---|
163 | static int count = 0; |
---|
164 | int port = 0; |
---|
165 | #if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_LIBDNET) |
---|
166 | int opt = 1; |
---|
167 | #endif |
---|
168 | #ifdef ROAR_HAVE_IPV4 |
---|
169 | struct sockaddr_in socket_addr; |
---|
170 | socklen_t len = sizeof(struct sockaddr_in); |
---|
171 | #else |
---|
172 | struct sockaddr socket_addr; |
---|
173 | socklen_t len = sizeof(struct sockaddr); |
---|
174 | #endif |
---|
175 | #ifdef ROAR_HAVE_SELECT |
---|
176 | int confh; |
---|
177 | fd_set fds; |
---|
178 | struct timeval timeout = {10, 0}; |
---|
179 | struct roar_message mes; |
---|
180 | #endif |
---|
181 | #ifdef ROAR_HAVE_UNIX |
---|
182 | int socks[2]; // for socketpair() |
---|
183 | #endif |
---|
184 | struct roar_sockname sockname; |
---|
185 | |
---|
186 | // make valgrind happy |
---|
187 | memset(&socket_addr, 0, sizeof(socket_addr)); |
---|
188 | #ifdef ROAR_HAVE_SELECT |
---|
189 | memset(&mes, 0, sizeof(mes)); |
---|
190 | #endif |
---|
191 | |
---|
192 | ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = ?", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer); |
---|
193 | |
---|
194 | if ( config != NULL ) { |
---|
195 | if ( config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_USE_EXECED ) { |
---|
196 | return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer); |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = ?", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer); |
---|
201 | |
---|
202 | roar_libroar_nowarn(); |
---|
203 | if ( roar_vio_ctl(roar_get_connection_vio2(con), ROAR_VIO_CTL_GET_SOCKNAME, &sockname) == -1 ) { |
---|
204 | roar_libroar_warn(); |
---|
205 | #ifdef ROAR_OS_OPENBSD |
---|
206 | sockname.type = ROAR_SOCKET_TYPE_UNIX; |
---|
207 | #else |
---|
208 | ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = -1", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer); |
---|
209 | |
---|
210 | return -1; |
---|
211 | #endif |
---|
212 | } |
---|
213 | roar_libroar_warn(); |
---|
214 | |
---|
215 | ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = ?", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer); |
---|
216 | |
---|
217 | if ( sockname.type == ROAR_SOCKET_TYPE_DECNET ) { |
---|
218 | if ( roar_socket_get_local_nodename() ) { |
---|
219 | snprintf(file, 24, "%s::roar$TMP%04x%02x", roar_socket_get_local_nodename(), getpid(), count++); |
---|
220 | } else { |
---|
221 | return -1; |
---|
222 | } |
---|
223 | #ifdef ROAR_HAVE_IPV4 |
---|
224 | } else { |
---|
225 | strncpy(file, inet_ntoa(socket_addr.sin_addr), sizeof(file) - 1); |
---|
226 | #endif |
---|
227 | } |
---|
228 | |
---|
229 | if ( sockname.type != ROAR_SOCKET_TYPE_UNIX ) { |
---|
230 | if ( (listen = roar_socket_listen(sockname.type, file, port)) == -1 ) { |
---|
231 | return -1; |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | if ( sockname.type == ROAR_SOCKET_TYPE_INET ) { |
---|
236 | #ifdef ROAR_HAVE_IPV4 |
---|
237 | setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, (void*)&opt, sizeof(int)); |
---|
238 | |
---|
239 | len = sizeof(struct sockaddr_in); |
---|
240 | if ( getsockname(listen, (struct sockaddr *)&socket_addr, &len) == -1 ) { |
---|
241 | return -1; |
---|
242 | } |
---|
243 | port = ROAR_NET2HOST16(socket_addr.sin_port); |
---|
244 | ROAR_DBG("roar_simple_new_stream_obj(*): port=%i", port); |
---|
245 | #else |
---|
246 | return -1; |
---|
247 | #endif |
---|
248 | } else if ( sockname.type == ROAR_SOCKET_TYPE_DECNET ) { |
---|
249 | #ifdef ROAR_HAVE_LIBDNET |
---|
250 | setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)); |
---|
251 | #else |
---|
252 | return -1; |
---|
253 | #endif |
---|
254 | } |
---|
255 | |
---|
256 | if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) { |
---|
257 | return -1; |
---|
258 | } |
---|
259 | |
---|
260 | if ( roar_stream_connect(con, s, dir, mixer) == -1 ) { |
---|
261 | return -1; |
---|
262 | } |
---|
263 | |
---|
264 | if ( sockname.type != ROAR_SOCKET_TYPE_UNIX ) { |
---|
265 | #ifdef ROAR_HAVE_SELECT |
---|
266 | if ( roar_stream_connect_to_ask(con, s, sockname.type, file, port) != -1 ) { |
---|
267 | |
---|
268 | FD_ZERO(&fds); |
---|
269 | FD_SET(listen, &fds); |
---|
270 | |
---|
271 | confh = roar_get_connection_fh(con); |
---|
272 | |
---|
273 | if ( confh != -1 ) { |
---|
274 | FD_SET(confh, &fds); |
---|
275 | } |
---|
276 | |
---|
277 | if ( select((confh > listen ? confh : listen) + 1, &fds, NULL, NULL, &timeout) < 1 ) { |
---|
278 | close(listen); |
---|
279 | |
---|
280 | // we don't need to check the content as we know it failed... |
---|
281 | if ( roar_recv_message(con, &mes, NULL) == -1 ) |
---|
282 | return -1; |
---|
283 | |
---|
284 | if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 ) |
---|
285 | return -1; |
---|
286 | |
---|
287 | return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer); |
---|
288 | } |
---|
289 | |
---|
290 | if ( FD_ISSET(listen, &fds) ) { |
---|
291 | if ( (fh = accept(listen, NULL, NULL)) != -1 ) { |
---|
292 | /* errr, do we need we any error handling here? */ |
---|
293 | } |
---|
294 | |
---|
295 | if ( roar_recv_message(con, &mes, NULL) == -1 ) { |
---|
296 | if ( fh != -1 ) |
---|
297 | close(fh); |
---|
298 | fh = -1; |
---|
299 | } else if ( mes.cmd != ROAR_CMD_OK ) { |
---|
300 | if ( fh != -1 ) |
---|
301 | close(fh); |
---|
302 | fh = -1; |
---|
303 | } |
---|
304 | } else { |
---|
305 | // we don't need to check the content as we know it failed... |
---|
306 | if ( roar_recv_message(con, &mes, NULL) == -1 ) { |
---|
307 | close(listen); |
---|
308 | return -1; |
---|
309 | } |
---|
310 | |
---|
311 | if ( mes.cmd != ROAR_CMD_OK ) { |
---|
312 | close(listen); |
---|
313 | if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 ) |
---|
314 | return -1; |
---|
315 | |
---|
316 | return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer); |
---|
317 | } else { // seems like we have a positive reply. So we retry the listen socket: |
---|
318 | FD_ZERO(&fds); |
---|
319 | FD_SET(listen, &fds); |
---|
320 | timeout.tv_sec = 0; |
---|
321 | timeout.tv_usec = 128000L; |
---|
322 | fh = -1; |
---|
323 | if ( select(listen + 1, &fds, NULL, NULL, &timeout) > 0 ) { |
---|
324 | if ( (fh = accept(listen, NULL, NULL)) == -1 ) { |
---|
325 | close(listen); |
---|
326 | if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 ) |
---|
327 | return -1; |
---|
328 | |
---|
329 | return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer); |
---|
330 | } |
---|
331 | } |
---|
332 | } |
---|
333 | } |
---|
334 | } |
---|
335 | |
---|
336 | close(listen); |
---|
337 | #else |
---|
338 | return -1; |
---|
339 | #endif |
---|
340 | } else { // this is sockname.type == ROAR_SOCKET_TYPE_UNIX |
---|
341 | #ifdef ROAR_HAVE_UNIX |
---|
342 | if ( socketpair(AF_UNIX, SOCK_STREAM, 0, socks) == -1 ) { |
---|
343 | roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors |
---|
344 | // as we return -1 in both whys |
---|
345 | return -1; |
---|
346 | } |
---|
347 | |
---|
348 | if ( roar_stream_passfh(con, s, socks[0]) == -1 ) { |
---|
349 | roar_kick(con, ROAR_OT_STREAM, s->id); // we do not need to check for errors |
---|
350 | // as we return -1 anyway. |
---|
351 | close(socks[0]); |
---|
352 | close(socks[1]); |
---|
353 | return -1; |
---|
354 | } |
---|
355 | |
---|
356 | close(socks[0]); |
---|
357 | fh = socks[1]; |
---|
358 | #else |
---|
359 | roar_kick(con, ROAR_OT_STREAM, s->id); |
---|
360 | return -1; |
---|
361 | #endif |
---|
362 | } |
---|
363 | |
---|
364 | if ( fh != -1 ) { |
---|
365 | if ( dir == ROAR_DIR_PLAY ) { |
---|
366 | (void)ROAR_SHUTDOWN(fh, SHUT_RD); |
---|
367 | } else if ( dir == ROAR_DIR_MONITOR || dir == ROAR_DIR_RECORD ) { |
---|
368 | (void)ROAR_SHUTDOWN(fh, SHUT_WR); |
---|
369 | } |
---|
370 | } |
---|
371 | |
---|
372 | s->fh = fh; |
---|
373 | |
---|
374 | ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = %i", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer, fh); |
---|
375 | return fh; |
---|
376 | } |
---|
377 | |
---|
378 | int roar_vio_simple_new_stream_obj (struct roar_vio_calls * calls, |
---|
379 | struct roar_connection * con, |
---|
380 | struct roar_stream * s, |
---|
381 | uint32_t rate, uint32_t channels, uint32_t bits, uint32_t codec, |
---|
382 | int dir, int mixer) { |
---|
383 | struct roar_stream stream; |
---|
384 | int fh; |
---|
385 | |
---|
386 | ROAR_DBG("roar_vio_simple_new_stream_obj(*) = ?"); |
---|
387 | |
---|
388 | if ( calls == NULL ) { |
---|
389 | roar_err_set(ROAR_ERROR_FAULT); |
---|
390 | return -1; |
---|
391 | } |
---|
392 | |
---|
393 | if ( s == NULL ) |
---|
394 | s = &stream; |
---|
395 | |
---|
396 | if ( (fh = _roar_simple_new_stream_obj(con, s, rate, channels, bits, codec, dir, mixer)) == -1 ) { |
---|
397 | ROAR_DBG("roar_vio_simple_new_stream_obj(*) = -1"); |
---|
398 | return -1; |
---|
399 | } |
---|
400 | |
---|
401 | ROAR_DBG("roar_vio_simple_new_stream_obj(*): fh=%i", fh); |
---|
402 | |
---|
403 | return roar_vio_open_fh_socket(calls, fh); |
---|
404 | } |
---|
405 | |
---|
406 | //ll |
---|